Repository navigation
Expand file tree
/
Copy pathcolor.html
More file actions
974 lines (660 loc) · 49.9 KB
/
Copy pathcolor.html
File metadata and controls
974 lines (660 loc) · 49.9 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
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
<!DOCTYPE html>
<html>
<head>
<title>color.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>color.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>Utilities for browser color types: css color strings, 32 bit integer pixels,
and 4 element Uint8 TypedColors</p>
<p>The rgba values below are ints in 0-255, including a (alpha).
I.e. a is not in 0-1 as in css colors.</p>
<p>Naming convention: <code>rgba</code> or <code>array</code> is generally a 3 or 4 element JavaScript
Array, not a browser color, used for functions taking r,g,b,a=255 values.</p>
</div>
<div class="content"><div class='highlight'><pre>
Color = {</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<h3 id="css-color-strings-">CSS Color Strings.</h3>
</div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>CSS colors in HTML are strings, see <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/color_value">Mozilla’s Color Reference</a>,
taking one of 7 forms:</p>
<ul>
<li>Names: <a href="http://en.wikipedia.org/wiki/Web_colors#HTML_color_names">140 color case-insensitive names</a> like
Red, Green, CadetBlue, etc.</li>
<li>Hex, short and long form: #0f0, #ff10a0</li>
<li>RGB: rgb(255, 0, 0), rgba(255, 0, 0, 0.5)</li>
<li>HSL: hsl(120, 100%, 50%), hsla(120, 100%, 50%, 0.8)</li>
</ul>
<p>See [this wikipedia article]
(<a href="http://en.wikipedia.org/wiki/HSL_and_HSV#Swatches">http://en.wikipedia.org/wiki/HSL_and_HSV#Swatches</a>)
on differences between HSL and HSB/HSV.</p>
</div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Convert 4 r,g,b,a ints in [0-255] to a css color string.
Alpha “a” is int in [0-255], not float in 0-1</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbaString</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b, a=<span class="hljs-number">255</span>)</span> -></span>
a = a/<span class="hljs-number">255</span>; a4 = a.toPrecision(<span class="hljs-number">4</span>)
<span class="hljs-keyword">if</span> a <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> <span class="hljs-string">"rgb(<span class="hljs-subst">#{r}</span>,<span class="hljs-subst">#{g}</span>,<span class="hljs-subst">#{b}</span>)"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"rgba(<span class="hljs-subst">#{r}</span>,<span class="hljs-subst">#{g}</span>,<span class="hljs-subst">#{b}</span>,<span class="hljs-subst">#{a4}</span>)"</span></pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Convert 3 ints, h in [0-360], s,l in [0-100]% to a css color string.
Alpha “a” is int in [0-255].</p>
<p>Note h=0 and h=360 are the same, use h in 0-359 for unique colors.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hslString</span>: <span class="hljs-function"><span class="hljs-params">(h, s, l, a=<span class="hljs-number">255</span>)</span> -></span>
a = a/<span class="hljs-number">255</span>; a4 = a.toPrecision(<span class="hljs-number">4</span>)
<span class="hljs-keyword">if</span> a <span class="hljs-keyword">is</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> <span class="hljs-string">"hsl(<span class="hljs-subst">#{h}</span>,<span class="hljs-subst">#{s}</span>%,<span class="hljs-subst">#{l}</span>%)"</span> <span class="hljs-keyword">else</span> <span class="hljs-string">"hsla(<span class="hljs-subst">#{h}</span>,<span class="hljs-subst">#{s}</span>%,<span class="hljs-subst">#{l}</span>%,<span class="hljs-subst">#{a4}</span>)"</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Return a web/html/css hex color string for an r,g,b opaque color (a=255)</p>
<p>Both #nnn and #nnnnnn forms supported.
Default is to check for the short hex form: #nnn.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hexString</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b, shortOK=<span class="hljs-literal">true</span>)</span> -></span>
<span class="hljs-keyword">if</span> shortOK
<span class="hljs-keyword">if</span> u.isInteger(r0=r/<span class="hljs-number">17</span>) <span class="hljs-keyword">and</span> u.isInteger(g0=g/<span class="hljs-number">17</span>) <span class="hljs-keyword">and</span> u.isInteger(b0=b/<span class="hljs-number">17</span>)
<span class="hljs-keyword">return</span> <span class="hljs-property">@hexShortString</span> r0, g0, b0
<span class="hljs-string">"#"</span> + (<span class="hljs-number">0x1000000</span> | (b | g << <span class="hljs-number">8</span> | r << <span class="hljs-number">16</span>)).toString(<span class="hljs-number">16</span>).slice(-<span class="hljs-number">6</span>)</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Return the 4 char short version of a hex color. Each of the r,g,b values
must be in [0-15]. The resulting color will be equivalent
to <code>r*17</code>, <code>g*17</code>, <code>b*17</code>, resulting in the values:</p>
<pre><code><span class="hljs-number">0</span>, <span class="hljs-number">17</span>, <span class="hljs-number">34</span>, <span class="hljs-number">51</span>, <span class="hljs-number">68</span>, <span class="hljs-number">85</span>, <span class="hljs-number">102</span>, <span class="hljs-number">119</span>, <span class="hljs-number">136</span>, <span class="hljs-number">153</span>, <span class="hljs-number">170</span>, <span class="hljs-number">187</span>, <span class="hljs-number">204</span>, <span class="hljs-number">221</span>, <span class="hljs-number">238</span>, <span class="hljs-number">255</span>
</code></pre><p>This is equivalent u.aRamp(0,255,16), i.e. 16 values per rgb channel.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hexShortString</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b)</span> -></span>
<span class="hljs-keyword">if</span> (r><span class="hljs-number">15</span>) <span class="hljs-keyword">or</span> (g><span class="hljs-number">15</span>) <span class="hljs-keyword">or</span> (b><span class="hljs-number">15</span>)
u.error <span class="hljs-string">"hexShortString: one of <span class="hljs-subst">#{[r,g,b]}</span> > 15"</span>
<span class="hljs-string">"#"</span> + r.toString(<span class="hljs-number">16</span>) + g.toString(<span class="hljs-number">16</span>) + b.toString(<span class="hljs-number">16</span>)</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>This is a hybrid string and generally our default. It returns:</p>
<ul>
<li>rgbaString if a not 255 (i.e. not opaque)</li>
<li>hexString otherwise</li>
<li>with the hexShortString if appropriate</li>
</ul>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">triString</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b, a=<span class="hljs-number">255</span>)</span> -></span>
<span class="hljs-keyword">if</span> a <span class="hljs-keyword">is</span> <span class="hljs-number">255</span> <span class="hljs-keyword">then</span> <span class="hljs-property">@hexString</span>(r, g, b, <span class="hljs-literal">true</span>) <span class="hljs-keyword">else</span> <span class="hljs-property">@rgbaString</span>(r, g, b, a)</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<h3 id="css-string-conversions">CSS String Conversions</h3>
</div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Return 4 element array given any legal CSS string color.</p>
<p>Legal strings vary widely: CadetBlue, #0f0, rgb(255,0,0), hsl(120,100%,50%)</p>
<p>Note: The browser speaks for itself: we simply set a 1x1 canvas fillStyle
to the string and create a pixel, returning the r,g,b,a TypedArray.</p>
</div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>The shared 1x1 canvas 2D context.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">sharedCtx1x1</span>: u.createCtx <span class="hljs-number">1</span>, <span class="hljs-number">1</span> <span class="hljs-comment"># share across calls.</span></pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Convert css string to TypedArray.
If you need a JavaScript Array, use uint8sToRgba below</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">stringToUint8s</span>: <span class="hljs-function"><span class="hljs-params">(string)</span> -></span></pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>@sharedCtx1x1.clearRect 0, 0, 1, 1 # is this needed?</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@sharedCtx1x1</span>.fillStyle = string
<span class="hljs-property">@sharedCtx1x1</span>.fillRect <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>
<span class="hljs-property">@sharedCtx1x1</span>.getImageData(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>).data</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<h3 id="pixel-colors-">Pixel Colors.</h3>
</div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Primitive Rgba <-> Pixel manipulation.</p>
<p>These use two views onto a 4 byte TypedArray buffer.</p>
<p>initSharedPixel called after Color module object exists,
see why at <a href="http://goo.gl/qrHXwB">Stack Overflow</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">sharedPixel</span>: <span class="hljs-literal">null</span>
<span class="hljs-attribute">sharedUint8s</span>: <span class="hljs-literal">null</span>
<span class="hljs-attribute">initSharedPixel</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@sharedPixel</span> = <span class="hljs-keyword">new</span> Uint32Array(<span class="hljs-number">1</span>)
<span class="hljs-property">@sharedUint8s</span> = <span class="hljs-keyword">new</span> Uint8ClampedArray(<span class="hljs-property">@sharedPixel</span>.buffer)</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Convert r,g,b,a to a single Uint32 pixel, correct endian format.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbaToPixel</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b, a=<span class="hljs-number">255</span>)</span> -></span>
<span class="hljs-property">@sharedUint8s</span>.set [r, g, b, a]
<span class="hljs-property">@sharedPixel</span>[<span class="hljs-number">0</span>]</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Convert a pixel to Uint8s via the shared typed views.
sharedOK = true returns the sharedUint8s, useful
for one-time computations like finding the pixel r,g,b,a values.
Default is to clone the sharedUint8s.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">pixelToUint8s</span>: <span class="hljs-function"><span class="hljs-params">(pixel, sharedOK = <span class="hljs-literal">false</span>)</span> -></span>
<span class="hljs-property">@sharedPixel</span>[<span class="hljs-number">0</span>] = pixel
<span class="hljs-keyword">if</span> sharedOK <span class="hljs-keyword">then</span> <span class="hljs-property">@sharedUint8s</span> <span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Uint8ClampedArray <span class="hljs-property">@sharedUint8s</span></pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<h3 id="color-conversion-and-scaling-functions-">Color Conversion and Scaling Functions.</h3>
</div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Return rgb array with 3 random ints in 0-255.
Convert to random cssString or pixel via functions above
or arrayToColor below</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomRgb</span>: <span class="hljs-function">-></span> (u.randomInt(<span class="hljs-number">256</span>) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span><span class="hljs-number">.2</span>])</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>Return random gray color, with intensities in [min,max).</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomGrayRgb</span>: <span class="hljs-function"><span class="hljs-params">(min = <span class="hljs-number">0</span>, max = <span class="hljs-number">256</span>)</span> -></span>
i = u.randomInt2 min, max
[i, i, i]</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Convert Uint8s to Array.
Useful after pixelToUint8s, stringToUint8s, hslToRgb.
(Use <code>Array</code> not <code>new Array</code>, better Coffee translation.)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">uint8sToRgba</span>: <span class="hljs-function"><span class="hljs-params">(uint8s)</span> -></span> Array uint8s...</pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Return the gray/intensity float value for a given r,g,b color.
Round to 0-255 int for gray values.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbIntensity</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b)</span> -></span> <span class="hljs-number">0.2126</span>*r + <span class="hljs-number">0.7152</span>*g + <span class="hljs-number">0.0722</span>*b</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Convert h,s,l to r,g,b Uint8 TypedArray</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hslToRgb</span>: <span class="hljs-function"><span class="hljs-params">(h, s, l)</span> -></span>
str = <span class="hljs-property">@hslString</span>(h, s, l)
<span class="hljs-property">@stringToUint8s</span>(str).subarray(<span class="hljs-number">0</span>,<span class="hljs-number">3</span>) <span class="hljs-comment"># a 3 byte view onto the 4 byte buffer.</span></pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Return a <a href="http://www.compuphase.com/cmetric.htm">distance metric</a> between two colors.
Max distance is roughly 765 (3*255), for black & white.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbDistance</span>: <span class="hljs-function"><span class="hljs-params">(r1, g1, b1, r2, g2, b2)</span> -></span>
rMean = Math.round( (r1 + r2) / <span class="hljs-number">2</span> )
[dr, dg, db] = [r1 - r2, g1 - g2, b1 - b2]
Math.sqrt (((<span class="hljs-number">512</span>+rMean)*dr*dr)>><span class="hljs-number">8</span>) + (<span class="hljs-number">4</span>*dg*dg) + (((<span class="hljs-number">767</span>-rMean)*db*db)>><span class="hljs-number">8</span>)</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Scale a data value to an rgb color.
Value is in [min max], rgb’s are two JS arrays.</p>
<p>See ColorMap’s scaleColor for related scaling method and
gradientColorMap for complex, MatLab-like, gradients.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbLerp</span>: <span class="hljs-function"><span class="hljs-params">(rgb1, value, min = <span class="hljs-number">0</span>, max = <span class="hljs-number">1</span>, rgb0 = [<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>])</span> -></span>
scale = u.lerpScale value, min, max <span class="hljs-comment">#(value - min)/(max - min)</span>
(Math.round(u.lerp(rgb0[i], rgb1[i], scale)) <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span><span class="hljs-number">.2</span>])</pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<h3 id="typed-color">Typed Color</h3>
<p>A typedColor is a 4 element Uint8ClampedArray, with two properties:</p>
<ul>
<li>pixelArray: A single element Uint32Array view on the Uint8ClampedArray</li>
<li>string: an optional, lazy evaluated, css color string.</li>
</ul>
<p>TypedColors are used in canvas’s <a href="https://developer.mozilla.org/en-US/docs/Web/API/ImageData">ImageData pixels</a>,
WebGL colors (4 rgba floats in 0-1), and in images.</p>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-attribute">typedColor</span>: <span class="hljs-keyword">do</span> () -> <span class="hljs-comment"># IIFE returns typedColor function.</span></pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Return a 4 element typedColor.</p>
<p>Any change to the typedColor r,g,b,a elements will dynamically change
the pixel value as it is a view onto the same buffer, and vice versa.
See Mozilla <a href="http://goo.gl/3OOQzy">TypedArray Docs</a>.
If r is not a number but a TypedArray, use it for the typedColor,
mainly used by colormaps.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"> <span class="hljs-title">typedColor</span> = <span class="hljs-params">(r, g, b, a=<span class="hljs-number">255</span>)</span> -></span>
ua = <span class="hljs-keyword">if</span> r.buffer <span class="hljs-keyword">then</span> r <span class="hljs-keyword">else</span> <span class="hljs-keyword">new</span> Uint8ClampedArray([r,g,b,a])
ua.pixelArray = <span class="hljs-keyword">new</span> Uint32Array(ua.buffer, ua.byteOffset, <span class="hljs-number">1</span>)</pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Make this an instance of TypedColorProto</p>
</div>
<div class="content"><div class='highlight'><pre> ua.__proto__ = TypedColorProto
ua</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Prototypal Inheritance: TypedColorProto has an Uint8ClampedArray
prototype. This allows TypedArrays to have prototype setters/getters,
including Object.defineProperties without enlarging the typedColor.</p>
</div>
<div class="content"><div class='highlight'><pre> TypedColorProto = {</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Set TypedColorProto prototype to Uint8ClampedArray’s prototype</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">__proto__</span>: Uint8ClampedArray.prototype</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Set the TypedArray; no need for getColor, it <em>is</em> the typed Uint8 array</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setColor</span>: <span class="hljs-function"><span class="hljs-params">(r,g,b,a=<span class="hljs-number">255</span>)</span> -></span></pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>@string = null if @string # will be lazy evaluated via getString.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@checkColorChange</span>()
@[<span class="hljs-number">0</span>]=r; @[<span class="hljs-number">1</span>]=g; @[<span class="hljs-number">2</span>]=b; @[<span class="hljs-number">3</span>]=a
@</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>Set the pixel view, changing the shared array (Uint8) view too</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setPixel</span>: <span class="hljs-function"><span class="hljs-params">(pixel)</span>-></span></pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>@string = null if @string # will be lazy evaluated via getString.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@checkColorChange</span>()
<span class="hljs-property">@pixelArray</span>[<span class="hljs-number">0</span>]=pixel</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>Get the pixel value</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">getPixel</span>: <span class="hljs-function">-></span> <span class="hljs-property">@pixelArray</span>[<span class="hljs-number">0</span>]</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>Set pixel/rgba values to equivalent of the css string.</p>
<p>Does <em>not</em> set the chached @string, which will be lazily evaluated
to its triString. This lets the typedColor remain small without the
color string until required by its getter.</p>
<p>Note if you set string to “red” or “rgb(255,0,0)”, the resulting
css string will return the triString #f00 value.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setString</span>: <span class="hljs-function"><span class="hljs-params">(string)</span> -></span>
<span class="hljs-property">@setColor</span>(Color.stringToUint8s(string)...)</pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>Return the triString for this typedColor, cached in the @string value</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">getString</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@string</span> = Color.triString(@...) <span class="hljs-keyword">unless</span> <span class="hljs-property">@string</span>?
<span class="hljs-property">@string</span></pre></div></div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>Housekeeping when a color is modified.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">checkColorChange</span>: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<p>Check for immutable colormap color.</p>
</div>
<div class="content"><div class='highlight'><pre> u.error <span class="hljs-string">"ColorMap.typedColor: cannot modify ColorMap color."</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@map</span></pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>Reset string on color change.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@string</span> = <span class="hljs-literal">null</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@string</span> <span class="hljs-comment"># will be lazy evaluated via getString.</span></pre></div></div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>Create duplicate of myself.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">clone</span>: <span class="hljs-function">-></span> typeColor @...
}</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-42">¶</a>
</div>
<p>Experiment: Sugar for converting getter/setters into properties.
Frequent rgba property setter very poor performance due to GC overhead.
These are in TypedColorProto, not in the typedColor,
thus shared and take no space in the color itself.</p>
</div>
<div class="content"><div class='highlight'><pre> Object.defineProperties TypedColorProto,</pre></div></div>
</li>
<li id="section-43">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-43">¶</a>
</div>
<p>pixel: get/set typedColor.pixelArray[0] via pixel property</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">pixel</span>:
<span class="hljs-attribute">get</span>: <span class="hljs-function">-></span> <span class="hljs-property">@pixelArray</span>[<span class="hljs-number">0</span>]
<span class="hljs-attribute">set</span>: <span class="hljs-function"><span class="hljs-params">(val)</span> -></span> <span class="hljs-property">@setPixel</span>(val)
<span class="hljs-attribute">enumerable</span>: <span class="hljs-literal">true</span> <span class="hljs-comment"># make visible in stack trace, remove after debugging</span></pre></div></div>
</li>
<li id="section-44">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-44">¶</a>
</div>
<p>rgba: set Uint8 values via JavaScript or TypedArray. Getter not needed.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgba</span>:
<span class="hljs-attribute">get</span>: <span class="hljs-function">-></span> @ <span class="hljs-comment">#.. not needed, already array</span>
<span class="hljs-attribute">set</span>: <span class="hljs-function"><span class="hljs-params">(val)</span> -></span> <span class="hljs-property">@setColor</span>(val...) <span class="hljs-comment"># beware! array GC slows this down.</span>
<span class="hljs-attribute">enumerable</span>: <span class="hljs-literal">true</span> <span class="hljs-comment"># make visible in stack trace, remove after debugging</span></pre></div></div>
</li>
<li id="section-45">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-45">¶</a>
</div>
<p>css: get/set typedColor.str via @string property. Updates TypedArrays.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">css</span>:
<span class="hljs-attribute">get</span>: <span class="hljs-function">-></span> <span class="hljs-property">@getString</span>()
<span class="hljs-attribute">set</span>: <span class="hljs-function"><span class="hljs-params">(val)</span> -></span> <span class="hljs-property">@setString</span>(val)
<span class="hljs-attribute">enumerable</span>: <span class="hljs-literal">true</span> <span class="hljs-comment"># make visible in stack trace, remove after debugging</span></pre></div></div>
</li>
<li id="section-46">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-46">¶</a>
</div>
<p>str: Legacy usage, identical to css getter property, will remove asap</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">str</span>:
<span class="hljs-attribute">get</span>: <span class="hljs-function">-></span> <span class="hljs-property">@getString</span>()
<span class="hljs-attribute">enumerable</span>: <span class="hljs-literal">true</span> <span class="hljs-comment"># make visible in stack trace, remove after debugging</span>
typedColor</pre></div></div>
</li>
<li id="section-47">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-47">¶</a>
</div>
<h3 id="color-types">Color Types</h3>
<p>Utilities for color types: css, pixel, typed</p>
</div>
</li>
<li id="section-48">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-48">¶</a>
</div>
<p>Return the color type of a given color, null if not a color.
null useful for testing if color <em>is</em> a color.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">colorType</span>: <span class="hljs-function"><span class="hljs-params">(color)</span> -></span>
<span class="hljs-keyword">if</span> color.pixelArray <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-string">"typed"</span>
<span class="hljs-keyword">if</span> u.isString color <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-string">"css"</span>
<span class="hljs-keyword">if</span> u.isInteger color <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-string">"pixel"</span>
<span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-49">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-49">¶</a>
</div>
<p>Return new color of the given type, given an r,g,b,(a) array,
where a defaults to opaque (255).
The array can be a JavaScript Array, a TypedArray, or a TypedColor.
Use randomRgb & randomGrayRgb arrays to create random valid colors.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">arrayToColor</span>: <span class="hljs-function"><span class="hljs-params">(array, type = <span class="hljs-string">"typed"</span>)</span> -></span>
<span class="hljs-keyword">switch</span> type
<span class="hljs-keyword">when</span> <span class="hljs-string">"css"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@triString</span> array...
<span class="hljs-keyword">when</span> <span class="hljs-string">"pixel"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@rgbaToPixel</span> array...
<span class="hljs-keyword">when</span> <span class="hljs-string">"typed"</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> array.buffer <span class="hljs-keyword">then</span> <span class="hljs-property">@typedColor</span> array <span class="hljs-keyword">else</span> <span class="hljs-property">@typedColor</span> array...
u.error <span class="hljs-string">"arrayToColor: incorrect type: <span class="hljs-subst">#{type}</span>"</span></pre></div></div>
</li>
<li id="section-50">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-50">¶</a>
</div>
<p>Return rgba array (either typed or Array) representing the color.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">colorToArray</span>: <span class="hljs-function"><span class="hljs-params">(color)</span> -></span>
<span class="hljs-keyword">switch</span> <span class="hljs-property">@colorType</span>(color)
<span class="hljs-keyword">when</span> <span class="hljs-string">"css"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@stringToUint8s</span> color
<span class="hljs-keyword">when</span> <span class="hljs-string">"pixel"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@pixelToUint8s</span> color
<span class="hljs-keyword">when</span> <span class="hljs-string">"typed"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> color <span class="hljs-comment"># already a (typed) array</span>
<span class="hljs-keyword">return</span> color <span class="hljs-keyword">if</span> u.isArray(color) <span class="hljs-keyword">or</span> color.buffer
u.error <span class="hljs-string">"colorToArray: bad color: <span class="hljs-subst">#{color}</span>"</span></pre></div></div>
</li>
<li id="section-51">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-51">¶</a>
</div>
<p>Given a color or rgba array and a type, return color of that type.
Return color if color is already of type.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">convertColor</span>: <span class="hljs-function"><span class="hljs-params">(color, type)</span> -></span>
type0 = <span class="hljs-property">@colorType</span>(color)
<span class="hljs-keyword">return</span> color <span class="hljs-keyword">if</span> type0 <span class="hljs-keyword">is</span> type <span class="hljs-keyword">and</span> type <span class="hljs-keyword">isnt</span> <span class="hljs-string">"css"</span> <span class="hljs-comment"># convert to triColor</span>
<span class="hljs-keyword">return</span> color[type] <span class="hljs-keyword">if</span> type0 <span class="hljs-keyword">is</span> <span class="hljs-string">"typed"</span>
<span class="hljs-property">@arrayToColor</span>(<span class="hljs-property">@colorToArray</span>(color), type)
<span class="hljs-attribute">rgbaToColor</span>: <span class="hljs-function"><span class="hljs-params">(r, g, b, a=<span class="hljs-number">255</span>, type=<span class="hljs-string">"typed"</span>)</span> -></span>
<span class="hljs-keyword">switch</span> type
<span class="hljs-keyword">when</span> <span class="hljs-string">"css"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@triString</span> r,g,b,a
<span class="hljs-keyword">when</span> <span class="hljs-string">"pixel"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@rgbaToPixel</span> r,g,b,a
<span class="hljs-keyword">when</span> <span class="hljs-string">"typed"</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@typedColor</span> r,g,b,a
u.error <span class="hljs-string">"rgbaToColor: incorrect type: <span class="hljs-subst">#{type}</span>"</span></pre></div></div>
</li>
<li id="section-52">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-52">¶</a>
</div>
<p>Return true if two colors result in the same color
Colors can be of different type:</p>
<p> Color.colorsEqual(“red”, [255,0,0]) returns <code>true</code></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">colorsEqual</span>: <span class="hljs-function"><span class="hljs-params">(color1, color2)</span> -></span>
<span class="hljs-property">@convertColor</span>(color1, <span class="hljs-string">"pixel"</span>) <span class="hljs-keyword">is</span> <span class="hljs-property">@convertColor</span>(color2, <span class="hljs-string">"pixel"</span>)
};
Color.initSharedPixel() <span class="hljs-comment"># Initialize the shared buffer pixel/rgb view</span></pre></div></div>
</li>
<li id="section-53">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-53">¶</a>
</div>
<p>Here are the 140 case insensitive legal color names (the X11 set)
To include them in your model, use:</p>
<pre><code>namedColors = <span class="hljs-string">"AliceBlue AntiqueWhite Aqua Aquamarine Azure Beige Bisque Black BlanchedAlmond Blue BlueViolet Brown BurlyWood CadetBlue Chartreuse Chocolate Coral CornflowerBlue Cornsilk Crimson Cyan DarkBlue DarkCyan DarkGoldenRod DarkGray DarkGreen DarkKhaki DarkMagenta DarkOliveGreen DarkOrange DarkOrchid DarkRed DarkSalmon DarkSeaGreen DarkSlateBlue DarkSlateGray DarkTurquoise DarkViolet DeepPink DeepSkyBlue DimGray DodgerBlue FireBrick FloralWhite ForestGreen Fuchsia Gainsboro GhostWhite Gold GoldenRod Gray Green GreenYellow HoneyDew HotPink IndianRed Indigo Ivory Khaki Lavender LavenderBlush LawnGreen LemonChiffon LightBlue LightCoral LightCyan LightGoldenRodYellow LightGray LightGreen LightPink LightSalmon LightSeaGreen LightSkyBlue LightSlateGray LightSteelBlue LightYellow Lime LimeGreen Linen Magenta Maroon MediumAquaMarine MediumBlue MediumOrchid MediumPurple MediumSeaGreen MediumSlateBlue MediumSpringGreen MediumTurquoise MediumVioletRed MidnightBlue MintCream MistyRose Moccasin NavajoWhite Navy OldLace Olive OliveDrab Orange OrangeRed Orchid PaleGoldenRod PaleGreen PaleTurquoise PaleVioletRed PapayaWhip PeachPuff Peru Pink Plum PowderBlue Purple Red RosyBrown RoyalBlue SaddleBrown Salmon SandyBrown SeaGreen SeaShell Sienna Silver SkyBlue SlateBlue SlateGray Snow SpringGreen SteelBlue Tan Teal Thistle Tomato Turquoise Violet Wheat White WhiteSmoke Yellow YellowGreen"</span>.split(<span class="hljs-string">" "</span>)
</code></pre><p>NetLogo’s 14 base colors names are:</p>
<pre><code>gray red orange brown yellow green lime turquoise cyan sky blue violet magenta pink
</code></pre><p>but do not match exactly the css named colors:</p>
<pre><code>netLogoColors={<span class="hljs-attribute">gray</span>:[<span class="hljs-number">141</span>,<span class="hljs-number">141</span>,<span class="hljs-number">141</span>], <span class="hljs-attribute">red</span>:[<span class="hljs-number">215</span>,<span class="hljs-number">50</span>,<span class="hljs-number">41</span>], <span class="hljs-attribute">orange</span>:[<span class="hljs-number">241</span>,<span class="hljs-number">106</span>,<span class="hljs-number">21</span>], <span class="hljs-attribute">brown</span>:[<span class="hljs-number">157</span>,<span class="hljs-number">110</span>,<span class="hljs-number">72</span>], <span class="hljs-attribute">yellow</span>:[<span class="hljs-number">237</span>,<span class="hljs-number">237</span>,<span class="hljs-number">49</span>], <span class="hljs-attribute">green</span>:[<span class="hljs-number">89</span>,<span class="hljs-number">176</span>,<span class="hljs-number">60</span>], <span class="hljs-attribute">lime</span>:[<span class="hljs-number">44</span>,<span class="hljs-number">209</span>,<span class="hljs-number">59</span>], <span class="hljs-attribute">turquoise</span>:[<span class="hljs-number">29</span>,<span class="hljs-number">159</span>,<span class="hljs-number">120</span>], <span class="hljs-attribute">cyan</span>:[<span class="hljs-number">84</span>,<span class="hljs-number">196</span>,<span class="hljs-number">196</span>], <span class="hljs-attribute">sky</span>:[<span class="hljs-number">45</span>,<span class="hljs-number">141</span>,<span class="hljs-number">190</span>], <span class="hljs-attribute">blue</span>:[<span class="hljs-number">52</span>,<span class="hljs-number">93</span>,<span class="hljs-number">169</span>], <span class="hljs-attribute">violet</span>:[<span class="hljs-number">124</span>,<span class="hljs-number">80</span>,<span class="hljs-number">164</span>], <span class="hljs-attribute">magenta</span>:[<span class="hljs-number">167</span>,<span class="hljs-number">27</span>,<span class="hljs-number">106</span>], <span class="hljs-attribute">pink</span>:[<span class="hljs-number">224</span>,<span class="hljs-number">127</span>,<span class="hljs-number">150</span>]}
</code></pre>
</div>
</li>
</ul>
</div>
</body>
</html>