001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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 java.awt.Color;
019:
020: /**
021: * A ColorPalette with additional ColorBrewer information (suitability data and colour selection).
022: *
023: * @author James Macgill
024: * @author Cory Horner, Refractions Research Inc.
025: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/brewer/src/main/java/org/geotools/brewer/color/BrewerPalette.java $
026: */
027: public class BrewerPalette extends ColorPalette {
028: private PaletteSuitability suitability;
029: private SampleScheme sampler;
030: private PaletteType type;
031:
032: /**
033: * Creates a new instance of BrewerPalette
034: */
035: public BrewerPalette() {
036: }
037:
038: /**
039: * Getter for property type.
040: *
041: * @return Value of property type.
042: */
043: public PaletteType getType() {
044: return this .type;
045: }
046:
047: /**
048: * Sets the type of palette.
049: *
050: * @param type new palette type
051: */
052: public void setType(PaletteType type) {
053: this .type = type;
054: }
055:
056: public Color getColor(int index, int length) {
057: return getColors(length)[index];
058: }
059:
060: /**
061: * Getter for the colour count
062: *
063: * @return the most colours this palette currently supports
064: */
065: public int getMaxColors() {
066: int countSampler = sampler.getMaxCount();
067: int numColors = getCount();
068:
069: //return the lesser of countSampler and numColors
070: if (countSampler < numColors) {
071: return countSampler;
072: } else {
073: return numColors;
074: }
075: }
076:
077: /**
078: * Getter for the colour count
079: *
080: * @return the minimum number of colours this palette currently supports
081: */
082: public int getMinColors() {
083: return sampler.getMinCount();
084: }
085:
086: /**
087: * Obtains a set of colours from the palette.
088: */
089: public Color[] getColors(int length) {
090: if (length < 2) {
091: length = 2; //if they ask for 1 colour, give them 2 instead of crashing
092: }
093:
094: int[] lookup = sampler.getSampleScheme(length);
095: Color[] colors = getColors();
096: Color[] result = new Color[length];
097:
098: for (int i = 0; i < length; i++) {
099: result[i] = colors[lookup[i]];
100: }
101:
102: return result;
103: }
104:
105: public PaletteSuitability getPaletteSuitability() {
106: return suitability;
107: }
108:
109: public void setPaletteSuitability(PaletteSuitability suitability) {
110: this .suitability = suitability;
111: }
112:
113: public SampleScheme getColorScheme() {
114: return sampler;
115: }
116:
117: public void setColorScheme(SampleScheme scheme) {
118: this.sampler = scheme;
119: }
120: }
|