001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.brewer.color;
017:
018: //import edu.psu.geovista.colorbrewer.OriginalColor;
019: import java.awt.Color;
020: import java.util.List;
021: import org.geotools.feature.Feature;
022: import org.geotools.filter.Expression;
023: import org.geotools.filter.FilterFactory;
024: import org.geotools.filter.FilterFactoryFinder;
025: import org.geotools.filter.FunctionExpression;
026: import org.geotools.filter.FunctionExpressionImpl;
027: import org.geotools.filter.LiteralExpression;
028: import org.geotools.filter.function.ClassificationFunction;
029:
030: /**
031: *
032: * @author James Macgill
033: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/brewer/src/main/java/org/geotools/brewer/color/PaletteFunction.java $
034: */
035: public class PaletteFunction extends FunctionExpressionImpl implements
036: FunctionExpression {
037: ClassificationFunction classifier;
038: String paletteName;
039: FilterFactory ff;
040:
041: /** Creates a new instance of PaletteFunction */
042: public PaletteFunction() {
043: this (FilterFactoryFinder.createFilterFactory());
044: }
045:
046: public PaletteFunction(FilterFactory factory) {
047: super ("Palette");
048: ff = factory;
049: }
050:
051: public void setFilterFactory(FilterFactory factory) {
052: ff = factory;
053: }
054:
055: public int getArgCount() {
056: return 2;
057: }
058:
059: public void setParameters(List args) {
060: super .setParameters(args);
061: classifier = (ClassificationFunction) getExpression(0);
062: paletteName = ((LiteralExpression) getExpression(1))
063: .getLiteral().toString();
064: }
065:
066: public Expression getEvaluationExpression() {
067: return classifier.getExpression();
068: }
069:
070: public void setEvaluationExpression(Expression e) {
071: classifier.setExpression(e);
072: }
073:
074: public ClassificationFunction getClassifier() {
075: return classifier;
076: }
077:
078: public void setClassifier(ClassificationFunction cf) {
079: classifier = cf;
080: }
081:
082: public int getNumberOfClasses() {
083: return classifier.getNumberOfClasses();
084: }
085:
086: public void setNumberOfClasses(int i) {
087: classifier.setNumberOfClasses(i);
088: }
089:
090: public String getPaletteName() {
091: return paletteName;
092: }
093:
094: public void setPaletteName(String s) {
095: paletteName = s;
096: }
097:
098: private String intToHex(int i) {
099: String prelim = Integer.toHexString(i);
100:
101: while (prelim.length() < 2) {
102: prelim = "0" + prelim;
103: }
104:
105: if (prelim.length() > 2) {
106: prelim = prelim.substring(0, 1);
107: }
108:
109: return prelim;
110: }
111:
112: public Object evaluate(Feature feature) {
113: int classNum = classifier.getNumberOfClasses();
114: ColorBrewer brewer = new ColorBrewer();
115: int klass = ((Integer) classifier.evaluate(feature)).intValue();
116:
117: BrewerPalette pal = brewer.getPalette(paletteName);
118: Color[] colors = pal.getColors(classNum);
119: String color = "#" + intToHex(colors[klass].getRed())
120: + intToHex(colors[klass].getGreen())
121: + intToHex(colors[klass].getBlue());
122:
123: return color;
124: }
125:
126: public String toString() {
127: return "Color Brewer palette";
128: }
129: }
|