Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 177 lines (162 sloc) 6.367 kb
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
1 package flare.util.palette
2 {
3 import flare.util.Colors;
4
5 /**
6 * Palette for color values, including utility methods for generating
7 * both categorical and ordinal color palettes.
8 */
9 public class ColorPalette extends Palette
10 {
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
11 private var _keyframes:Array;
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
12
13 /** Keyframes at which color values change in the palette. Useful
14 * for configuring gradient paint fills. */
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
15 public function get keyframes():Array { return _keyframes; }
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
16
17 /**
18 * Creates a new ColorPalette.
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
19 * @param colors an array of colors defining the palette
20 * @param keyframes array of keyframes of color interpolations
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
21 */
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
22 public function ColorPalette(colors:Array=null, keyframes:Array=null) {
23 _values = colors;
24 _keyframes = keyframes;
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
25 }
26
27 /**
28 * Retrieves the color corresponding to input interpolation fraction.
29 * @param f an interpolation fraction
30 * @return the color corresponding to the input fraction
31 */
32 public function getColor(v:Number):uint
33 {
34 if (_values==null || _values.length==0)
35 return 0;
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
36 return _values[uint(Math.round(v*(_values.length-1)))];
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
37 }
38
39 /**
40 * Retrieves the color corresponding to the input array index.
41 * @param idx an integer index. The actual index value used is
42 * the modulo of the input index by the length of the palette.
43 * @return the color in the palette at the given index
44 */
45 public function getColorByIndex(idx:int):uint
46 {
47 if (_values == null || _values.length == 0 || idx < 0)
48 return 0;
49 else
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
50 return _values[idx % _values.length];
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
51 }
52
53 // --------------------------------------------------------------------
54
55 /** Default size of generated color palettes. */
56 public static const DEFAULT_SIZE:int = 64;
57 /** A set of 10 colors for encoding category values. */
58 public static const CATEGORY_COLORS_10:/*uint*/Array = [
59 0xFF1F77B4, 0xFFFF7F0E, 0xFF2CA02C, 0xFFD62728, 0xFF9467BD,
60 0xFF8C564B, 0xFFE377C2, 0xFF7F7F7F, 0xFFBCBD22, 0xFF17BECF
61 ];
62 /** A set of 20 colors for encoding category values. Includes
63 * the colors of <code>CATEGORY_COLORS_10</code> plus lighter
64 * shades of each. */
65 public static const CATEGORY_COLORS_20:/*uint*/Array = [
66 0xFF1F77B4, 0xFFAEC7E8, 0xFFFF7F0E, 0xFFFFBB78, 0xFF2CA02C,
67 0xFF98DF8A, 0xFFD62728, 0xFFFF9896, 0xFF9467BD, 0xFFC5B0D5,
68 0xFF8C564B, 0xFFC49C94, 0xFFE377C2, 0xFFF7B6D2, 0xFF7F7F7F,
69 0xFFC7C7C7, 0xFFBCBD22, 0xFFDBDB8D, 0xFF17BECF, 0xFF9EDAE5
70 ];
71 /** An alternative set of 19 colors for encoding category values. */
72 public static const CATEGORY_COLORS_ALT_19:/*uint*/Array = [
73 0xff9C9EDE, 0xff7375B5, 0xff4A5584, 0xffCEDB9C, 0xffB5CF6B,
74 0xff8CA252, 0xff637939, 0xffE7CB94, 0xffE7BA52, 0xffBD9E39,
75 0xff8C6D31, 0xffE7969C, 0xffD6616B, 0xffAD494A, 0xff843C39,
76 0xffDE9ED6, 0xffCE6DBD, 0xffA55194, 0xff7B4173
77 ];
78
79
80 /**
81 * Generates a categorical color palette
82 * @param size the number of colors to include
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
83 * @param colors an array of category colors to use. If null, a
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
84 * default category color palette will be used.
85 * @param alpha the alpha value for this palette's colors
86 * @return the categorical color palette
87 */
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
88 public static function category(size:int=20, colors:Array=null,
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
89 alpha:Number=1.0):ColorPalette
90 {
91 if (colors==null)
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
92 colors = size<=10 ? CATEGORY_COLORS_10 : CATEGORY_COLORS_20;
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
93 var a:uint = uint(255 * alpha) % 256;
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
94 var cm:Array = new Array(size);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
95 for (var i:uint=0; i<size; ++i) {
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
96 cm[i] = Colors.setAlpha(colors[i % colors.length], a);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
97 }
98 return new ColorPalette(cm);
99 }
100
101 /**
102 * Generates a color palette that uses a "cool", blue-heavy color scheme.
103 * @param size the size of the color palette
104 * @return the color palette
105 */
106 public static function cool(size:int=DEFAULT_SIZE):ColorPalette
107 {
108 return ramp(0xff00ffff, 0xffff00ff, size);
109 }
110
111 /**
112 * Generates a color palette that moves from black to red to yellow
113 * to white.
114 * @param size the size of the color palette
115 * @return the color palette
116 */
117 public static function hot(size:int=DEFAULT_SIZE):ColorPalette
118 {
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
119 var cm:Array = new Array(size), r:Number, g:Number, b:Number;
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
120 var n:int = int(2*size/8);
121
122 for (var i:uint=0; i<size; i++) {
123 r = i<n ? (i+1)/n : 1;
124 g = i<n ? 0 : (i<2*n ? (i-n)/n : 1);
125 b = i<2*n ? 0 : (i-2*n)/(size-2*n);
126 cm[i] = Colors.rgba(255*r, 255*g, 255*b);
127 }
128 var f:Number = 1/4;
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
129 return new ColorPalette(cm, [0, f, 2*f, 1]);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
130 }
131
132 /**
133 * Generates a color palette that "ramps" from one color to another.
134 * @param min the color corresponding to the minimum scale value
135 * @param max the color corresponding to the maximum scale value
136 * @param size the size of the color palette
137 * @return the color palette
138 */
139 public static function ramp(min:uint=0xfff1eef6, max:uint=0xff045a8d,
140 size:int=DEFAULT_SIZE):ColorPalette
141 {
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
142 var cm:Array = new Array(size);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
143 for (var i:uint=0; i<size; ++i) {
144 cm[i] = Colors.interpolate(min, max, i/(size-1));
145 }
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
146 return new ColorPalette(cm, [0,1]);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
147 }
148
149 /**
150 * Generates a color palette of color ramps diverging from a central
151 * value.
152 * @param min the color corresponding to the minimum scale value
153 * @param mid the color corresponding to the central scale value
154 * @param max the color corresponding to the maximum scale value
155 * @param f an interpolation fraction specifying the position of the
156 * central value
157 * @param size the size of the color palette
158 * @return the color palette
159 */
160 public static function diverging(min:uint=0xffd73027,
161 mid:uint=0xffffffbf, max:uint=0xff1a9850,
162 f:Number=0.5, size:int=DEFAULT_SIZE):ColorPalette
163 {
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
164 var cm:Array = new Array(size);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
165 var mp:int = int(f*size), i:uint, j:uint;
166 for (i=0; i<mp; ++i) {
167 cm[i] = Colors.interpolate(min, mid, i/mp);
168 }
169 mp = size - mp - 1;
170 for (j=0; i<size; ++i, ++j) {
171 cm[i] = Colors.interpolate(mid, max, j/mp);
172 }
748f676 wjwillett Updated version that incorporates new changes/fixes generated by Thomas ...
wjwillett authored
173 return new ColorPalette(cm, [0,f,1]);
870994f wjwillett First commit of Flare to the official Flare GitHub repository.
wjwillett authored
174 }
175
176 } // end of class ColorPalette
177 }
Something went wrong with that request. Please try again.