Skip to content
This repository
branch: master
file 177 lines (162 sloc) 6.367 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
package flare.util.palette
{
import flare.util.Colors;

/**
* Palette for color values, including utility methods for generating
* both categorical and ordinal color palettes.
*/
public class ColorPalette extends Palette
{
private var _keyframes:Array;

/** Keyframes at which color values change in the palette. Useful
* for configuring gradient paint fills. */
public function get keyframes():Array { return _keyframes; }

/**
* Creates a new ColorPalette.
* @param colors an array of colors defining the palette
* @param keyframes array of keyframes of color interpolations
*/
public function ColorPalette(colors:Array=null, keyframes:Array=null) {
_values = colors;
_keyframes = keyframes;
}

/**
* Retrieves the color corresponding to input interpolation fraction.
* @param f an interpolation fraction
* @return the color corresponding to the input fraction
*/
public function getColor(v:Number):uint
{
if (_values==null || _values.length==0)
return 0;
return _values[uint(Math.round(v*(_values.length-1)))];
}

/**
* Retrieves the color corresponding to the input array index.
* @param idx an integer index. The actual index value used is
* the modulo of the input index by the length of the palette.
* @return the color in the palette at the given index
*/
public function getColorByIndex(idx:int):uint
{
if (_values == null || _values.length == 0 || idx < 0)
return 0;
else
return _values[idx % _values.length];
}

// --------------------------------------------------------------------

/** Default size of generated color palettes. */
public static const DEFAULT_SIZE:int = 64;
/** A set of 10 colors for encoding category values. */
public static const CATEGORY_COLORS_10:/*uint*/Array = [
0xFF1F77B4, 0xFFFF7F0E, 0xFF2CA02C, 0xFFD62728, 0xFF9467BD,
0xFF8C564B, 0xFFE377C2, 0xFF7F7F7F, 0xFFBCBD22, 0xFF17BECF
];
/** A set of 20 colors for encoding category values. Includes
* the colors of <code>CATEGORY_COLORS_10</code> plus lighter
* shades of each. */
public static const CATEGORY_COLORS_20:/*uint*/Array = [
0xFF1F77B4, 0xFFAEC7E8, 0xFFFF7F0E, 0xFFFFBB78, 0xFF2CA02C,
0xFF98DF8A, 0xFFD62728, 0xFFFF9896, 0xFF9467BD, 0xFFC5B0D5,
0xFF8C564B, 0xFFC49C94, 0xFFE377C2, 0xFFF7B6D2, 0xFF7F7F7F,
0xFFC7C7C7, 0xFFBCBD22, 0xFFDBDB8D, 0xFF17BECF, 0xFF9EDAE5
];
/** An alternative set of 19 colors for encoding category values. */
public static const CATEGORY_COLORS_ALT_19:/*uint*/Array = [
0xff9C9EDE, 0xff7375B5, 0xff4A5584, 0xffCEDB9C, 0xffB5CF6B,
0xff8CA252, 0xff637939, 0xffE7CB94, 0xffE7BA52, 0xffBD9E39,
0xff8C6D31, 0xffE7969C, 0xffD6616B, 0xffAD494A, 0xff843C39,
0xffDE9ED6, 0xffCE6DBD, 0xffA55194, 0xff7B4173
];


/**
* Generates a categorical color palette
* @param size the number of colors to include
* @param colors an array of category colors to use. If null, a
* default category color palette will be used.
* @param alpha the alpha value for this palette's colors
* @return the categorical color palette
*/
public static function category(size:int=20, colors:Array=null,
alpha:Number=1.0):ColorPalette
{
if (colors==null)
colors = size<=10 ? CATEGORY_COLORS_10 : CATEGORY_COLORS_20;
var a:uint = uint(255 * alpha) % 256;
var cm:Array = new Array(size);
for (var i:uint=0; i<size; ++i) {
cm[i] = Colors.setAlpha(colors[i % colors.length], a);
}
return new ColorPalette(cm);
}

/**
* Generates a color palette that uses a "cool", blue-heavy color scheme.
* @param size the size of the color palette
* @return the color palette
*/
public static function cool(size:int=DEFAULT_SIZE):ColorPalette
{
return ramp(0xff00ffff, 0xffff00ff, size);
}

/**
* Generates a color palette that moves from black to red to yellow
* to white.
* @param size the size of the color palette
* @return the color palette
*/
public static function hot(size:int=DEFAULT_SIZE):ColorPalette
{
var cm:Array = new Array(size), r:Number, g:Number, b:Number;
var n:int = int(2*size/8);

for (var i:uint=0; i<size; i++) {
r = i<n ? (i+1)/n : 1;
g = i<n ? 0 : (i<2*n ? (i-n)/n : 1);
b = i<2*n ? 0 : (i-2*n)/(size-2*n);
cm[i] = Colors.rgba(255*r, 255*g, 255*b);
}
var f:Number = 1/4;
return new ColorPalette(cm, [0, f, 2*f, 1]);
}

/**
* Generates a color palette that "ramps" from one color to another.
* @param min the color corresponding to the minimum scale value
* @param max the color corresponding to the maximum scale value
* @param size the size of the color palette
* @return the color palette
*/
public static function ramp(min:uint=0xfff1eef6, max:uint=0xff045a8d,
size:int=DEFAULT_SIZE):ColorPalette
{
var cm:Array = new Array(size);
for (var i:uint=0; i<size; ++i) {
cm[i] = Colors.interpolate(min, max, i/(size-1));
}
return new ColorPalette(cm, [0,1]);
}

/**
* Generates a color palette of color ramps diverging from a central
* value.
* @param min the color corresponding to the minimum scale value
* @param mid the color corresponding to the central scale value
* @param max the color corresponding to the maximum scale value
* @param f an interpolation fraction specifying the position of the
* central value
* @param size the size of the color palette
* @return the color palette
*/
public static function diverging(min:uint=0xffd73027,
mid:uint=0xffffffbf, max:uint=0xff1a9850,
f:Number=0.5, size:int=DEFAULT_SIZE):ColorPalette
{
var cm:Array = new Array(size);
var mp:int = int(f*size), i:uint, j:uint;
for (i=0; i<mp; ++i) {
cm[i] = Colors.interpolate(min, mid, i/mp);
}
mp = size - mp - 1;
for (j=0; i<size; ++i, ++j) {
cm[i] = Colors.interpolate(mid, max, j/mp);
}
return new ColorPalette(cm, [0,f,1]);
}

} // end of class ColorPalette
}
Something went wrong with that request. Please try again.