/* Copyright (C) 2014 A.Vontobel
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The license is in LICENCE-GPL.txt.
* If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
*/
package net.sf.drawj2d.cmd;
import java.awt.Color;
import org.hecl.*;
import java.util.ArrayList;
import java.util.Arrays;
/**
* PenCmd implements the "pen" command, which sets color and pen width.
*/
public class PenCmd implements Command {
API api;
boolean debug = false;
public PenCmd (API api) {
this.api = api;
}
@Override
public Thing cmdCode(Interp interp, Thing[] argv) throws HeclException {
assert argv.length > 0;
if (debug) System.out.println(argv[0].toString());
if (argv.length == 1) {
api.pen();
} else {
assert argv.length > 1;
ArrayList singleargv = splitArgv(argv);
for (Thing arg : singleargv) {
if (NumberThing.isNumber(arg)) {
float size = NumberThing.asNumber(arg).floatValue();
api.pen(size);
}
else {
String str = StringThing.get(arg);
int linetype = getLineType(str);
if (linetype > 0) {
api.pen(linetype);
} else {
Color color = getColor(str);
api.pen(color);
}
}
}
}
return null;
}
private int getLineType(String str) {
String s = str.toLowerCase();
if (s.equals("dashed")) return API.DASHED;
if (s.equals("dotted")) return API.DOTTED;
if (s.equals("dashdotted")) return API.DASHDOTTED;
if (s.equals("solid")) return API.SOLID;
return -1;
}
private Color getColor(String colorstring) {
String c = colorstring.toLowerCase();
Color color = Color.black;
if (c.equals("black") || c.equals("k")) color = Color.black;
else if (c.equals("blue") || c.equals("b")) color = Color.blue;
else if (c.equals("cyan") || c.equals("c")) color = Color.cyan;
else if (c.equals("darkgray") || c.equals("d")) color = Color.darkGray;
else if (c.equals("gray") || c.equals("a")) color = Color.gray;
else if (c.equals("green") || c.equals("g")) color = Color.green;
else if (c.equals("lightgray") || c.equals("l")) color = Color.lightGray;
else if (c.equals("magenta") || c.equals("m")) color = Color.magenta;
else if (c.equals("orange") || c.equals("o")) color = Color.orange;
else if (c.equals("pink") || c.equals("p")) color = Color.pink;
else if (c.equals("red") || c.equals("r")) color = Color.red;
else if (c.equals("white") || c.equals("w")) color = Color.white;
else if (c.equals("yellow") || c.equals("y")) color = Color.yellow;
else if (c.equals("brown")) color = new Color(0x87431D);
else if (c.equals("darkorange")) color = new Color(0xFF6600);
else try { // rgb hex value e.g. "0x87431D"
color = Color.decode(colorstring);
} catch (NumberFormatException nfe) {
if (debug) System.out.println(nfe.getMessage());
// black
}
return color;
}
/** Make a list of arguments (without command in argv[0]).
* Split List arguments to single arguments. */
private ArrayList splitArgv(Thing[] argv) throws HeclException {
ArrayList singleargv = new ArrayList(argv.length);
for (int i = 1; i < argv.length; i++) {
Thing[] currentargv = ListThing.getArray(argv[i]);
singleargv.addAll(Arrays.asList(currentargv));
}
return singleargv;
}
}
Ads help maintain this website.