-
Notifications
You must be signed in to change notification settings - Fork 0
/
palette.js
1501 lines (1417 loc) · 58.8 KB
/
palette.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @license
*
* Colour Palette Generator script.
* Copyright (c) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Furthermore, ColorBrewer colour schemes are covered by the following:
*
* Copyright (c) 2002 Cynthia Brewer, Mark Harrower, and
* The Pennsylvania State University.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions as source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. The end-user documentation included with the redistribution, if any,
* must include the following acknowledgment: "This product includes color
* specifications and designs developed by Cynthia Brewer
* (http://colorbrewer.org/)." Alternately, this acknowledgment may appear
* in the software itself, if and wherever such third-party
* acknowledgments normally appear.
*
* 4. The name "ColorBrewer" must not be used to endorse or promote products
* derived from this software without prior written permission. For written
* permission, please contact Cynthia Brewer at [email protected].
*
* 5. Products derived from this software may not be called "ColorBrewer",
* nor may "ColorBrewer" appear in their name, without prior written
* permission of Cynthia Brewer.
*
* Furthermore, Solarized colour schemes are covered by the following:
*
* Copyright (c) 2011 Ethan Schoonover
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
'use strict';
var palette = (function() {
var proto = Array.prototype;
var slice = function(arr, opt_begin, opt_end) {
return proto.slice.apply(arr, proto.slice.call(arguments, 1));
};
var extend = function(arr, arr2) {
return proto.push.apply(arr, arr2);
};
var function_type = typeof function() {};
var INF = 1000000000; // As far as we're concerned, that's infinity. ;)
/**
* Generate a colour palette from given scheme.
*
* If scheme argument is not a function it is passed to palettes.listSchemes
* function (along with the number argument). This may result in an array
* of more than one available scheme. If that is the case, scheme at
* opt_index position is taken.
*
* This allows using different palettes for different data without having to
* name the schemes specifically, for example:
*
* palette_for_foo = palette('sequential', 10, 0);
* palette_for_bar = palette('sequential', 10, 1);
* palette_for_baz = palette('sequential', 10, 2);
*
* @param {!palette.SchemeType|string|palette.Palette} scheme Scheme to
* generate palette for. Either a function constructed with
* palette.Scheme object, or anything that palette.listSchemes accepts
* as name argument.
* @param {number} number Number of colours to return. If negative, absolute
* value is taken and colours will be returned in reverse order.
* @param {number=} opt_index If scheme is a name of a group or an array and
* results in more than one scheme, index of the scheme to use. The
* index wraps around.
* @param {...*} varargs Additional arguments to pass to palette or colour
* generator (if the chosen scheme uses those).
* @return {Array<string>} Array of abs(number) 'RRGGBB' strings or null if
* no matching scheme was found.
*/
var palette = function(scheme, number, opt_index, varargs) {
number |= 0;
if (number == 0) {
return [];
}
if (typeof scheme !== function_type) {
var arr = palette.listSchemes(
/** @type {string|palette.Palette} */ (scheme), number);
if (!arr.length) {
return null;
}
scheme = arr[(opt_index || 0) % arr.length];
}
var args = slice(arguments, 2);
args[0] = number;
return scheme.apply(scheme, args);
};
/**
* Returns a callable colour scheme object.
*
* Just after being created, the scheme has no colour palettes and no way of
* generating any, thus generate method will return null. To turn scheme
* into a useful object, addPalette, addPalettes or setColorFunction methods
* need to be used.
*
* To generate a colour palette with given number colours using function
* returned by this method, just call it with desired number of colours.
*
* Since this function *returns* a callable object, it must *not* be used
* with the new operator.
*
* @param {string} name Name of the scheme.
* @param {string|!Array<string>=} opt_groups A group name or list of
* groups the scheme should be categorised under. Three typical groups
* to use are 'qualitative', 'sequential' and 'diverging', but any
* groups may be created.
* @return {!palette.SchemeType} A colour palette generator function, which
* in addition has methods and properties like a regular object. Think
* of it as a callable object.
*/
palette.Scheme = function(name, opt_groups) {
/**
* A map from a number to a colour palettes with given number of colours.
* @type {!Object<number, palette.Palette>}
*/
var palettes = {};
/**
* The biggest palette in palettes map.
* @type {number}
*/
var palettes_max = 0;
/**
* The smallest palette in palettes map.
* @type {number}
*/
var palettes_min = INF;
var makeGenerator = function() {
if (arguments.length <= 1) {
return self.color_func.bind(self);
} else {
var args = slice(arguments);
return function(x) {
args[0] = x;
return self.color_func.apply(self, args);
};
}
};
/**
* Generate a colour palette from the scheme.
*
* If there was a palette added with addPalette (or addPalettes) with
* enough colours, that palette will be used. Otherwise, if colour
* function has been set using setColorFunction method, that function will
* be used to generate the palette. Otherwise null is returned.
*
* @param {number} number Number of colours to return. If negative,
* absolute value is taken and colours will be returned in reverse
* order.
* @param {...*} varargs Additional arguments to pass to palette or colour
* generator (if the chosen scheme uses those).
*/
var self = function(number, varargs) {
number |= 0;
if (!number) {
return [];
}
var _number = number;
number = Math.abs(number);
if (number <= palettes_max) {
for (var i = Math.max(number, palettes_min); !(i in palettes); ++i) {
/* nop */
}
var colors = palettes[i];
if (i > number) {
var take_head =
'shrinking_takes_head' in colors ?
colors.shrinking_takes_head : self.shrinking_takes_head;
if (take_head) {
colors = colors.slice(0, number);
i = number;
} else {
return palette.generate(
function(x) { return colors[Math.round(x)]; },
_number, 0, colors.length - 1);
}
}
colors = colors.slice();
if (_number < 0) {
colors.reverse();
}
return colors;
} else if (self.color_func) {
return palette.generate(makeGenerator.apply(self, arguments),
_number, 0, 1, self.color_func_cyclic);
} else {
return null;
}
};
/**
* The name of the palette.
* @type {string}
*/
self.scheme_name = name;
/**
* A list of groups the palette belongs to.
* @type {!Array<string>}
*/
self.groups = opt_groups ?
typeof opt_groups === 'string' ? [opt_groups] : opt_groups : [];
/**
* The biggest palette this scheme can generate.
* @type {number}
*/
self.max = 0;
/**
* The biggest palette this scheme can generate that is colour-blind
* friendly.
* @type {number}
*/
self.cbf_max = INF;
/**
* Adds a colour palette to the colour scheme.
*
* @param {palette.Palette} palette An array of 'RRGGBB' strings
* representing the palette to add.
* @param {boolean=} opt_is_cbf Whether the palette is colourblind friendly.
*/
self.addPalette = function(palette, opt_is_cbf) {
var len = palette.length;
if (len) {
palettes[len] = palette;
palettes_min = Math.min(palettes_min, len);
palettes_max = Math.max(palettes_max, len);
self.max = Math.max(self.max, len);
if (!opt_is_cbf && len != 1) {
self.cbf_max = Math.min(self.cbf_max, len - 1);
}
}
};
/**
* Adds number of colour palettes to the colour scheme.
*
* @param {palette.PalettesList} palettes A map or an array of colour
* palettes to add. If map, i.e. object, is used, properties should
* use integer property names.
* @param {number=} opt_max Size of the biggest palette in palettes set.
* If not set, palettes must have a length property which will be used.
* @param {number=} opt_cbf_max Size of the biggest palette which is still
* colourblind friendly. 1 by default.
*/
self.addPalettes = function(palettes, opt_max, opt_cbf_max) {
opt_max = opt_max || palettes.length;
for (var i = 0; i < opt_max; ++i) {
if (i in palettes) {
self.addPalette(palettes[i], true);
}
}
self.cbf_max = Math.min(self.cbf_max, opt_cbf_max || 1);
};
/**
* Enable shrinking palettes taking head of the list of colours.
*
* When user requests n-colour palette but the smallest palette added with
* addPalette (or addPalettes) is m-colour one (where n < m), n colours
* across the palette will be returned. For example:
* var ex = palette.Scheme('ex');
* ex.addPalette(['000000', 'bcbcbc', 'ffffff']);
* var pal = ex(2);
* // pal == ['000000', 'ffffff']
*
* This works for palettes where the distance between colours is
* correlated to distance in the palette array, which is true in gradients
* such as the one above.
*
* To turn this feature off shrinkByTakingHead can be set to true either
* for all palettes in the scheme (if opt_idx is not given) or for palette
* with given number of colours only. In general, setting the option for
* given palette overwrites whatever has been set for the scheme. The
* default, as described above, is false.
*
* Alternatively, the feature can be enabled by setting shrinking_takes_head
* property for the palette Array or the scheme object.
*
* For example, all of the below give equivalent results:
* var pal = ['ff0000', '00ff00', '0000ff'];
*
* var ex = palette.Scheme('ex');
* ex.addPalette(pal); // ex(2) == ['ff0000', '0000ff']
* ex.shrinkByTakingHead(true); // ex(2) == ['ff0000', '00ff00']
*
* ex = palette.Scheme('ex');
* ex.addPalette(pal); // ex(2) == ['ff0000', '0000ff']
* ex.shrinkByTakingHead(true, 3); // ex(2) == ['ff0000', '00ff00']
*
* ex = palette.Scheme('ex');
* ex.addPalette(pal);
* ex.addPalette(pal); // ex(2) == ['ff0000', '0000ff']
* pal.shrinking_takes_head = true; // ex(2) == ['ff0000', '00ff00']
*
* @param {boolean} enabled Whether to enable or disable the “shrinking
* takes head” feature. It is disabled by default.
* @param {number=} opt_idx If given, the “shrinking takes head” option
* for palette with given number of colours is set. If such palette
* does not exist, nothing happens.
*/
self.shrinkByTakingHead = function(enabled, opt_idx) {
if (opt_idx !== void(0)) {
if (opt_idx in palettes) {
palettes[opt_idx].shrinking_takes_head = !!enabled;
}
} else {
self.shrinking_takes_head = !!enabled;
}
};
/**
* Sets a colour generation function of the colour scheme.
*
* The function must accept a singe number argument whose value can be from
* 0.0 to 1.0, and return a colour as an 'RRGGBB' string. This function
* will be used when generating palettes, i.e. if 11-colour palette is
* requested, this function will be called with arguments 0.0, 0.1, …, 1.0.
*
* If the palette generated by the function is colourblind friendly,
* opt_is_cbf should be set to true.
*
* In some cases, it is not desirable to reach 1.0 when generating
* a palette. This happens for hue-rainbows where the 0–1 range corresponds
* to a 0°–360° range in hues, and since hue at 0° is the same as at 360°,
* it's desired to stop short the end of the range when generating
* a palette. To accomplish this, opt_cyclic should be set to true.
*
* @param {palette.ColorFunction} func A colour generator function.
* @param {boolean=} opt_is_cbf Whether palette generate with the function
* is colour-blind friendly.
* @param {boolean=} opt_cyclic Whether colour at 0.0 is the same as the
* one at 1.0.
*/
self.setColorFunction = function(func, opt_is_cbf, opt_cyclic) {
self.color_func = func;
self.color_func_cyclic = !!opt_cyclic;
self.max = INF;
if (!opt_is_cbf && self.cbf_max === INF) {
self.cbf_max = 1;
}
};
self.color = function(x, varargs) {
if (self.color_func) {
return self.color_func.apply(this, arguments);
} else {
return null;
}
};
return self;
};
/**
* Creates a new palette.Scheme and initialises it by calling addPalettes
* method with the rest of the arguments.
*
* @param {string} name Name of the scheme.
* @param {string|!Array<string>} groups A group name or list of group
* names the scheme belongs to.
* @param {!Object<number, palette.Palette>|!Array<palette.Palette>}
* palettes A map or an array of colour palettes to add. If map, i.e.
* object, is used, properties should use integer property names.
* @param {number=} opt_max Size of the biggest palette in palettes set.
* If not set, palettes must have a length property which will be used.
* @param {number=} opt_cbf_max Size of the biggest palette which is still
* colourblind friendly. 1 by default.
* @return {!palette.SchemeType} A colour palette generator function, which
* in addition has methods and properties like a regular object. Think
* of it as a callable object.
*/
palette.Scheme.fromPalettes = function(name, groups,
palettes, opt_max, opt_cbf_max) {
var scheme = palette.Scheme(name, groups);
scheme.addPalettes.apply(scheme, slice(arguments, 2));
return scheme;
};
/**
* Creates a new palette.Scheme and initialises it by calling
* setColorFunction method with the rest of the arguments.
*
* @param {string} name Name of the scheme.
* @param {string|!Array<string>} groups A group name or list of group
* names the scheme belongs to.
* @param {palette.ColorFunction} func A colour generator function.
* @param {boolean=} opt_is_cbf Whether palette generate with the function
* is colour-blind friendly.
* @param {boolean=} opt_cyclic Whether colour at 0.0 is the same as the
* one at 1.0.
* @return {!palette.SchemeType} A colour palette generator function, which
* in addition has methods and properties like a regular object. Think
* of it as a callable object.
*/
palette.Scheme.withColorFunction = function(name, groups,
func, opt_is_cbf, opt_cyclic) {
var scheme = palette.Scheme(name, groups);
scheme.setColorFunction.apply(scheme, slice(arguments, 2));
return scheme;
};
/**
* A map of registered schemes. Maps a scheme or group name to a list of
* scheme objects. Property name is either 'n-<name>' for single scheme
* names or 'g-<name>' for scheme group names.
*
* @type {!Object<string, !Array<!Object>>}
*/
var registered_schemes = {};
/**
* Registers a new colour scheme.
*
* @param {!palette.SchemeType} scheme The scheme to add.
*/
palette.register = function(scheme) {
registered_schemes['n-' + scheme.scheme_name] = [scheme];
scheme.groups.forEach(function(g) {
(registered_schemes['g-' + g] =
registered_schemes['g-' + g] || []).push(scheme);
});
(registered_schemes['g-all'] =
registered_schemes['g-all'] || []).push(scheme);
};
/**
* List all schemes that match given name and number of colours.
*
* name argument can be either a string or an array of strings. In the
* former case, the function acts as if the argument was an array with name
* as a single argument (i.e. “palette.listSchemes('foo')” is exactly the same
* as “palette.listSchemes(['foo'])”).
*
* Each name can be either name of a palette (e.g. 'tol-sq' for Paul Tol's
* sequential palette), or a name of a group (e.g. 'sequential' for all
* sequential palettes). Name can therefore map to a single scheme or
* several schemes.
*
* Furthermore, name can be suffixed with '-cbf' to indicate that only
* schemes that are colourblind friendly should be returned. For example,
* 'rainbow' returns a HSV rainbow scheme, but because it is not colourblind
* friendly, 'rainbow-cbf' returns no schemes.
*
* Some schemes may produce colourblind friendly palettes for some number of
* colours. For example ColorBrewer's Dark2 scheme is colourblind friendly
* if no more than 3 colours are generated. If opt_number is not specified,
* 'qualitative-cbf' will include 'cb-Dark2' but if opt_number is given as,
* say, 5 it won't.
*
* Name can also be 'all' which will return all registered schemes.
* Naturally, 'all-cbf' will return all colourblind friendly schemes.
*
* Schemes are added to the library using palette.register. Schemes are
* created using palette.Scheme function. By default, the following schemes
* are available:
*
* Name Description
* -------------- -----------------------------------------------------
* tol Paul Tol's qualitative scheme, cbf, max 12 colours.
* tol-dv Paul Tol's diverging scheme, cbf.
* tol-sq Paul Tol's sequential scheme, cbf.
* tol-rainbow Paul Tol's qualitative scheme, cbf.
*
* rainbow A rainbow palette.
*
* cb-YlGn ColorBrewer sequential schemes.
* cb-YlGnBu
* cb-GnBu
* cb-BuGn
* cb-PuBuGn
* cb-PuBu
* cb-BuPu
* cb-RdPu
* cb-PuRd
* cb-OrRd
* cb-YlOrRd
* cb-YlOrBr
* cb-Purples
* cb-Blues
* cb-Greens
* cb-Oranges
* cb-Reds
* cb-Greys
*
* cb-PuOr ColorBrewer diverging schemes.
* cb-BrBG
* cb-PRGn
* cb-PiYG
* cb-RdBu
* cb-RdGy
* cb-RdYlBu
* cb-Spectral
* cb-RdYlGn
*
* cb-Accent ColorBrewer qualitative schemes.
* cb-Dark2
* cb-Paired
* cb-Pastel1
* cb-Pastel2
* cb-Set1
* cb-Set2
* cb-Set3
*
* sol-base Solarized base colours.
* sol-accent Solarized accent colours.
*
* The following groups are also available by default:
*
* Name Description
* -------------- -----------------------------------------------------
* all All registered schemes.
* sequential All sequential schemes.
* diverging All diverging schemes.
* qualitative All qualitative schemes.
* cb-sequential All ColorBrewer sequential schemes.
* cb-diverging All ColorBrewer diverging schemes.
* cb-qualitative All ColorBrewer qualitative schemes.
*
* You can read more about Paul Tol's palettes at http://www.sron.nl/~pault/.
* You can read more about ColorBrewer at http://colorbrewer2.org.
*
* @param {string|!Array<string>} name A name of a colour scheme, of
* a group of colour schemes, or an array of any of those.
* @param {number=} opt_number When requesting only colourblind friendly
* schemes, number of colours the scheme must provide generating such
* that the palette is still colourblind friendly. 2 by default.
* @return {!Array<!palette.SchemeType>} An array of colour scheme objects
* matching the criteria. Sorted by scheme name.
*/
palette.listSchemes = function(name, opt_number) {
if (!opt_number) {
opt_number = 2;
} else if (opt_number < 0) {
opt_number = -opt_number;
}
var ret = [];
(typeof name === 'string' ? [name] : name).forEach(function(n) {
var cbf = n.substring(n.length - 4) === '-cbf';
if (cbf) {
n = n.substring(0, n.length - 4);
}
var schemes =
registered_schemes['g-' + n] ||
registered_schemes['n-' + n] ||
[];
for (var i = 0, scheme; (scheme = schemes[i]); ++i) {
if ((cbf ? scheme.cbf : scheme.max) >= opt_number) {
ret.push(scheme);
}
}
});
ret.sort(function(a, b) {
return a.scheme_name >= b.scheme_name ?
a.scheme_name > b.scheme_name ? 1 : 0 : -1;
});
return ret;
};
/**
* Generates a palette using given colour generating function.
*
* The color_func callback must accept a singe number argument whose value
* can vary from 0.0 to 1.0 (or in general from opt_start to opt_end), and
* return a colour as an 'RRGGBB' string. This function will be used when
* generating palettes, i.e. if 11-colour palette is requested, this
* function will be called with arguments 0.0, 0.1, …, 1.0.
*
* In some cases, it is not desirable to reach 1.0 when generating
* a palette. This happens for hue-rainbows where the 0–1 range corresponds
* to a 0°–360° range in hues, and since hue at 0° is the same as at 360°,
* it's desired to stop short the end of the range when generating
* a palette. To accomplish this, opt_cyclic should be set to true.
*
* opt_start and opt_end may be used to change the range the colour
* generation function is called with. opt_end may be less than opt_start
* which will case to traverse the range in reverse. Another way to reverse
* the palette is requesting negative number of colours. The two methods do
* not always lead to the same results (especially if opt_cyclic is set).
*
* @param {palette.ColorFunction} color_func A colours generating callback
* function.
* @param {number} number Number of colours to generate in the palette. If
* number is negative, colours in the palette will be reversed. If only
* one colour is requested, colour at opt_start will be returned.
* @param {number=} opt_start Optional starting point for the palette
* generation function. Zero by default.
* @param {number=} opt_end Optional ending point for the palette generation
* function. One by default.
* @param {boolean=} opt_cyclic If true, function will assume colour at
* point opt_start is the same as one at opt_end.
* @return {palette.Palette} An array of 'RRGGBB' colours.
*/
palette.generate = function(color_func, number, opt_start, opt_end,
opt_cyclic) {
if (Math.abs(number) < 1) {
return [];
}
opt_start = opt_start === void(0) ? 0 : opt_start;
opt_end = opt_end === void(0) ? 1 : opt_end;
if (Math.abs(number) < 2) {
return [color_func(opt_start)];
}
var i = Math.abs(number);
var x = opt_start;
var ret = [];
var step = (opt_end - opt_start) / (opt_cyclic ? i : (i - 1));
for (; --i >= 0; x += step) {
ret.push(color_func(x));
}
if (number < 0) {
ret.reverse();
}
return ret;
};
/**
* Clamps value to [0, 1] range.
* @param {number} v Number to limit value of.
* @return {number} If v is inside of [0, 1] range returns v, otherwise
* returns 0 or 1 depending which side of the range v is closer to.
*/
var clamp = function(v) {
return v > 0 ? (v < 1 ? v : 1) : 0;
};
/**
* Converts r, g, b triple into RRGGBB hex representation.
* @param {number} r Red value of the colour in the range [0, 1].
* @param {number} g Green value of the colour in the range [0, 1].
* @param {number} b Blue value of the colour in the range [0, 1].
* @return {string} A lower-case RRGGBB representation of the colour.
*/
palette.rgbColor = function(r, g, b) {
return [r, g, b].map(function(v) {
v = Number(Math.round(clamp(v) * 255)).toString(16);
return v.length == 1 ? '0' + v : v;
}).join('');
};
/**
* Converts a linear r, g, b triple into RRGGBB hex representation.
* @param {number} r Linear red value of the colour in the range [0, 1].
* @param {number} g Linear green value of the colour in the range [0, 1].
* @param {number} b Linear blue value of the colour in the range [0, 1].
* @return {string} A lower-case RRGGBB representation of the colour.
*/
palette.linearRgbColor = function(r, g, b) {
// http://www.brucelindbloom.com/index.html?Eqn_XYZ_to_RGB.html
return [r, g, b].map(function(v) {
v = clamp(v);
if (v <= 0.0031308) {
v = 12.92 * v;
} else {
v = 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
}
v = Number(Math.round(v * 255)).toString(16);
return v.length == 1 ? '0' + v : v;
}).join('');
};
/**
* Converts an HSV colours to RRGGBB hex representation.
* @param {number} h Hue in the range [0, 1].
* @param {number=} opt_s Saturation in the range [0, 1]. One by default.
* @param {number=} opt_v Value in the range [0, 1]. One by default.
* @return {string} An RRGGBB representation of the colour.
*/
palette.hsvColor = function(h, opt_s, opt_v) {
h *= 6;
var s = opt_s === void(0) ? 1 : clamp(opt_s);
var v = opt_v === void(0) ? 1 : clamp(opt_v);
var x = v * (1 - s * Math.abs(h % 2 - 1));
var m = v * (1 - s);
switch (Math.floor(h) % 6) {
case 0: return palette.rgbColor(v, x, m);
case 1: return palette.rgbColor(x, v, m);
case 2: return palette.rgbColor(m, v, x);
case 3: return palette.rgbColor(m, x, v);
case 4: return palette.rgbColor(x, m, v);
default: return palette.rgbColor(v, m, x);
}
};
palette.register(palette.Scheme.withColorFunction(
'rainbow', 'qualitative', palette.hsvColor, false, true));
return palette;
})();
/** @typedef {function(number): string} */
palette.ColorFunction;
/** @typedef {!Array<string>} */
palette.Palette;
/** @typedef {!Object<number, palette.Palette>|!Array<palette.Palette>} */
palette.PalettesList;
/**
* @typedef {
* function(number, ...?): Array<string>|
* {
* scheme_name: string,
* groups: !Array<string>,
* max: number,
* cbf_max: number,
* addPalette: function(!Array<string>, boolean=),
* addPalettes: function(palette.PalettesList, number=, number=),
* shrinkByTakingHead: function(boolean, number=),
* setColorFunction: function(palette.ColorFunction, boolean=, boolean=),
* color: function(number, ...?): ?string}}
*/
palette.SchemeType;
/* mpn65 palette start here. ************************************************/
/* The ‘mpn65’ palette is designed for systems which show many graphs which
don’t have custom colour palettes chosen by humans or if number of necessary
colours isn’t know a priori. */
(function() {
var scheme = palette.Scheme.fromPalettes('mpn65', 'qualitative', [[
'ff0029', '377eb8', '66a61e', '984ea3', '00d2d5', 'ff7f00', 'af8d00',
'7f80cd', 'b3e900', 'c42e60', 'a65628', 'f781bf', '8dd3c7', 'bebada',
'fb8072', '80b1d3', 'fdb462', 'fccde5', 'bc80bd', 'ffed6f', 'c4eaff',
'cf8c00', '1b9e77', 'd95f02', 'e7298a', 'e6ab02', 'a6761d', '0097ff',
'00d067', '000000', '252525', '525252', '737373', '969696', 'bdbdbd',
'f43600', '4ba93b', '5779bb', '927acc', '97ee3f', 'bf3947', '9f5b00',
'f48758', '8caed6', 'f2b94f', 'eff26e', 'e43872', 'd9b100', '9d7a00',
'698cff', 'd9d9d9', '00d27e', 'd06800', '009f82', 'c49200', 'cbe8ff',
'fecddf', 'c27eb6', '8cd2ce', 'c4b8d9', 'f883b0', 'a49100', 'f48800',
'27d0df', 'a04a9b'
]]);
scheme.shrinkByTakingHead(true);
palette.register(scheme);
})();
/* Paul Tol's schemes start here. *******************************************/
/* See http://www.sron.nl/~pault/ */
(function() {
var rgb = palette.rgbColor;
/**
* Calculates value of a polynomial at given point.
* For example, poly(x, 1, 2, 3) calculates value of “1 + 2*x + 3*X²”.
* @param {number} x Value to calculate polynomial for.
* @param {...number} varargs Coefficients of the polynomial specified in
* the order of rising powers of x including constant as the first
* variable argument.
*/
var poly = function(x, varargs) {
var i = arguments.length - 1, n = arguments[i];
while (i > 1) {
n = n * x + arguments[--i];
}
return n;
};
/**
* Calculate approximate value of error function with maximum error of 0.0005.
* See <https://en.wikipedia.org/wiki/Error_function>.
* @param {number} x Argument of the error function.
* @return {number} Value of error function for x.
*/
var erf = function(x) {
// https://en.wikipedia.org/wiki/Error_function#Approximation_with_elementary_functions
// This produces a maximum error of 0.0005 which is more then we need. In
// the worst case, that error is multiplied by four and then divided by two
// before being multiplied by 255, so in the end, the error is multiplied by
// 510 which produces 0.255 which is less than a single colour step.
var y = poly(Math.abs(x), 1, 0.278393, 0.230389, 0.000972, 0.078108);
y *= y; // y^2
y *= y; // y^4
y = 1 - 1 / y;
return x < 0 ? -y : y;
};
palette.register(palette.Scheme.fromPalettes('tol', 'qualitative', [
['4477aa'],
['4477aa', 'cc6677'],
['4477aa', 'ddcc77', 'cc6677'],
['4477aa', '117733', 'ddcc77', 'cc6677'],
['332288', '88ccee', '117733', 'ddcc77', 'cc6677'],
['332288', '88ccee', '117733', 'ddcc77', 'cc6677', 'aa4499'],
['332288', '88ccee', '44aa99', '117733', 'ddcc77', 'cc6677', 'aa4499'],
['332288', '88ccee', '44aa99', '117733', '999933', 'ddcc77', 'cc6677',
'aa4499'],
['332288', '88ccee', '44aa99', '117733', '999933', 'ddcc77', 'cc6677',
'882255', 'aa4499'],
['332288', '88ccee', '44aa99', '117733', '999933', 'ddcc77', '661100',
'cc6677', '882255', 'aa4499'],
['332288', '6699cc', '88ccee', '44aa99', '117733', '999933', 'ddcc77',
'661100', 'cc6677', '882255', 'aa4499'],
['332288', '6699cc', '88ccee', '44aa99', '117733', '999933', 'ddcc77',
'661100', 'cc6677', 'aa4466', '882255', 'aa4499']
], 12, 12));
/**
* Calculates a colour along Paul Tol's sequential colours axis.
* See <http://www.sron.nl/~pault/colourschemes.pdf> figure 7 and equation 1.
* @param {number} x Position of the colour on the axis in the [0, 1] range.
* @return {string} An RRGGBB representation of the colour.
*/
palette.tolSequentialColor = function(x) {
return rgb(1 - 0.392 * (1 + erf((x - 0.869) / 0.255)),
1.021 - 0.456 * (1 + erf((x - 0.527) / 0.376)),
1 - 0.493 * (1 + erf((x - 0.272) / 0.309)));
};
palette.register(palette.Scheme.withColorFunction(
'tol-sq', 'sequential', palette.tolSequentialColor, true));
/**
* Calculates a colour along Paul Tol's diverging colours axis.
* See <http://www.sron.nl/~pault/colourschemes.pdf> figure 8 and equation 2.
* @param {number} x Position of the colour on the axis in the [0, 1] range.
* @return {string} An RRGGBB representation of the colour.
*/
palette.tolDivergingColor = function(x) {
var g = poly(x, 0.572, 1.524, -1.811) / poly(x, 1, -0.291, 0.1574);
return rgb(poly(x, 0.235, -2.13, 26.92, -65.5, 63.5, -22.36),
g * g,
1 / poly(x, 1.579, -4.03, 12.92, -31.4, 48.6, -23.36));
};
palette.register(palette.Scheme.withColorFunction(
'tol-dv', 'diverging', palette.tolDivergingColor, true));
/**
* Calculates a colour along Paul Tol's rainbow colours axis.
* See <http://www.sron.nl/~pault/colourschemes.pdf> figure 13 and equation 3.
* @param {number} x Position of the colour on the axis in the [0, 1] range.
* @return {string} An RRGGBB representation of the colour.
*/
palette.tolRainbowColor = function(x) {
return rgb(poly(x, 0.472, -0.567, 4.05) / poly(x, 1, 8.72, -19.17, 14.1),
poly(x, 0.108932, -1.22635, 27.284, -98.577, 163.3, -131.395,
40.634),
1 / poly(x, 1.97, 3.54, -68.5, 243, -297, 125));
};
palette.register(palette.Scheme.withColorFunction(
'tol-rainbow', 'qualitative', palette.tolRainbowColor, true));
})();
/* Solarized colour schemes start here. *************************************/
/* See http://ethanschoonover.com/solarized */
(function() {
/*
* Those are not really designed to be used in graphs, but we're keeping
* them here in case someone cares.
*/
palette.register(palette.Scheme.fromPalettes('sol-base', 'sequential', [
['002b36', '073642', '586e75', '657b83', '839496', '93a1a1', 'eee8d5',
'fdf6e3']
], 1, 8));
palette.register(palette.Scheme.fromPalettes('sol-accent', 'qualitative', [
['b58900', 'cb4b16', 'dc322f', 'd33682', '6c71c4', '268bd2', '2aa198',
'859900']
]));
})();
/* ColorBrewer colour schemes start here. ***********************************/
/* See http://colorbrewer2.org/ */
(function() {
var schemes = {
YlGn: {
type: 'sequential',
cbf: 42,
3: ['f7fcb9', 'addd8e', '31a354'],
4: ['ffffcc', 'c2e699', '78c679', '238443'],
5: ['ffffcc', 'c2e699', '78c679', '31a354', '006837'],
6: ['ffffcc', 'd9f0a3', 'addd8e', '78c679', '31a354', '006837'],
7: ['ffffcc', 'd9f0a3', 'addd8e', '78c679', '41ab5d', '238443',
'005a32'],
8: ['ffffe5', 'f7fcb9', 'd9f0a3', 'addd8e', '78c679', '41ab5d',
'238443', '005a32'],
9: ['ffffe5', 'f7fcb9', 'd9f0a3', 'addd8e', '78c679', '41ab5d',
'238443', '006837', '004529']
},
YlGnBu: {
type: 'sequential',
cbf: 42,
3: ['edf8b1', '7fcdbb', '2c7fb8'],
4: ['ffffcc', 'a1dab4', '41b6c4', '225ea8'],
5: ['ffffcc', 'a1dab4', '41b6c4', '2c7fb8', '253494'],
6: ['ffffcc', 'c7e9b4', '7fcdbb', '41b6c4', '2c7fb8', '253494'],
7: ['ffffcc', 'c7e9b4', '7fcdbb', '41b6c4', '1d91c0', '225ea8',
'0c2c84'],
8: ['ffffd9', 'edf8b1', 'c7e9b4', '7fcdbb', '41b6c4', '1d91c0',
'225ea8', '0c2c84'],
9: ['ffffd9', 'edf8b1', 'c7e9b4', '7fcdbb', '41b6c4', '1d91c0',
'225ea8', '253494', '081d58']
},
GnBu: {
type: 'sequential',
cbf: 42,
3: ['e0f3db', 'a8ddb5', '43a2ca'],
4: ['f0f9e8', 'bae4bc', '7bccc4', '2b8cbe'],
5: ['f0f9e8', 'bae4bc', '7bccc4', '43a2ca', '0868ac'],
6: ['f0f9e8', 'ccebc5', 'a8ddb5', '7bccc4', '43a2ca', '0868ac'],
7: ['f0f9e8', 'ccebc5', 'a8ddb5', '7bccc4', '4eb3d3', '2b8cbe',
'08589e'],
8: ['f7fcf0', 'e0f3db', 'ccebc5', 'a8ddb5', '7bccc4', '4eb3d3',
'2b8cbe', '08589e'],
9: ['f7fcf0', 'e0f3db', 'ccebc5', 'a8ddb5', '7bccc4', '4eb3d3',
'2b8cbe', '0868ac', '084081']
},