Repository navigation
Expand file tree
/
Copy pathutil.html
More file actions
2635 lines (1818 loc) · 135 KB
/
Copy pathutil.html
File metadata and controls
2635 lines (1818 loc) · 135 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
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
<!DOCTYPE html>
<html>
<head>
<title>util.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>util.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>This documentation uses Jeremy Ashkenas’s
<a href="http://jashkenas.github.com/docco/">docco</a> which allows
<a href="http://daringfireball.net/projects/markdown/syntax">markdown</a>.</p>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p><strong>Util</strong> contains the general utilities for the project. Note that within
a <strong>Util</strong> function <code>@</code> referrs to Util, <em>not</em> the global name space.
Alias: u is an alias for ABM.Util within the agentscript module (not outside)</p>
<pre><code> u.clearCtx(ctx) <span class="hljs-keyword">is</span> equivalent to
ABM.Util.clearCtx(ctx)
</code></pre>
</div>
<div class="content"><div class='highlight'><pre>
Util = util = u = <span class="hljs-comment"># TODO: "util" deprecated in favor of Util</span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Shortcut for throwing an error. Good for debugging:</p>
<pre><code>error(<span class="hljs-string">"wtf? foo=<span class="hljs-subst">#{foo}</span>"</span>) <span class="hljs-keyword">if</span> fooProblem
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">error</span>: <span class="hljs-function"><span class="hljs-params">(s)</span> -></span> <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> Error s</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Utility for logging a message only once .. i.e. if in loop</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">loggedMsgs</span>: []
<span class="hljs-attribute">logOnce</span>: <span class="hljs-function"><span class="hljs-params">(s)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@loggedMsgs</span>.indexOf(s) < <span class="hljs-number">0</span>
<span class="hljs-built_in">console</span>.log s
<span class="hljs-property">@loggedMsgs</span>.push s</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Deprecations: logOnce w/ optional single alert</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">deprecatedAlert</span>: <span class="hljs-literal">false</span> <span class="hljs-comment"># Set to true to let modeler know</span>
<span class="hljs-attribute">deprecated</span>: <span class="hljs-function"><span class="hljs-params">(s)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@deprecatedAlert</span>
alert <span class="hljs-string">"Deprecated functions, see console.log"</span>
<span class="hljs-property">@deprecatedAlert</span> = <span class="hljs-literal">false</span>
<span class="hljs-property">@logOnce</span> <span class="hljs-string">"DEPRECATED - <span class="hljs-subst">#{s}</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>Two max/min int values. One for 2^53, largest int in float64, other for
bitwise ops which are 32 bit. See <a href="http://goo.gl/WpAzT">discussion</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">MaxINT</span>: Math.pow(<span class="hljs-number">2</span>,<span class="hljs-number">53</span>); <span class="hljs-attribute">MinINT</span>: -Math.pow(<span class="hljs-number">2</span>,<span class="hljs-number">53</span>) <span class="hljs-comment"># -@MaxINT fails, @ not defined yet</span>
<span class="hljs-attribute">MaxINT32</span>: <span class="hljs-number">0</span>|<span class="hljs-number">0x7fffffff</span>; <span class="hljs-attribute">MinINT32</span>: <span class="hljs-number">0</span>|<span class="hljs-number">0x80000000</span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>Replacements for Javascript’s iffy <code>typeof</code> and <code>instanceof</code>
See <a href="http://underscorejs.org/docs/underscore.html">underscore docs</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isArray</span>: Array.isArray <span class="hljs-keyword">or</span> <span class="hljs-comment"># returns a function that is then applied to arg.</span>
(obj) -> toString.call(obj) <span class="hljs-keyword">is</span> <span class="hljs-string">'[object Array]'</span></pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Can use: !!(obj and obj.constructor and obj.call and obj.apply)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isFunction</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> <span class="hljs-keyword">typeof</span> obj <span class="hljs-keyword">is</span> <span class="hljs-string">"function"</span></pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Can use: !!(obj is ‘’ or (obj and obj.charCodeAt and obj.substr))</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isString</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> <span class="hljs-keyword">typeof</span> obj <span class="hljs-keyword">is</span> <span class="hljs-string">'string'</span> || obj <span class="hljs-keyword">instanceof</span> String
<span class="hljs-attribute">isInteger</span>: Number.isInteger <span class="hljs-keyword">or</span> <span class="hljs-comment"># like isArray</span>
(num) -> Math.floor(num) <span class="hljs-keyword">is</span> num
<span class="hljs-attribute">isObject</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> toString.call(obj) <span class="hljs-keyword">is</span> <span class="hljs-string">'[object Object]'</span></pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<h3 id="numeric-operations">Numeric Operations</h3>
</div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Replace Math.random with a simple seedable generator.
See <a href="http://goo.gl/FafN3z">StackOverflow</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomSeed</span>: <span class="hljs-function"><span class="hljs-params">(seed=<span class="hljs-number">123456</span>)</span> -></span>
Math.random = <span class="hljs-function">-></span> x=Math.sin(seed++)*<span class="hljs-number">10000</span>; x-Math.floor(x)</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Return random int in [0,max) or [min,max)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomInt</span>: <span class="hljs-function"><span class="hljs-params">(max)</span> -></span> Math.floor(Math.random() * max)
<span class="hljs-attribute">randomInt2</span>: <span class="hljs-function"><span class="hljs-params">(min, max)</span> -></span> min + Math.floor(Math.random() * (max-min))</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Return float Gaussian normal with given mean, std deviation.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomNormal</span>: <span class="hljs-function"><span class="hljs-params">(mean = <span class="hljs-number">0.0</span>, sigma = <span class="hljs-number">1.0</span>)</span> -></span> <span class="hljs-comment"># Box-Muller</span>
u1 = <span class="hljs-number">1.0</span>-Math.random(); u2 = Math.random() <span class="hljs-comment"># u1 in (0,1]</span>
norm = Math.sqrt(-<span class="hljs-number">2.0</span>*Math.log(u1)) * Math.cos(<span class="hljs-number">2.0</span>*Math.PI*u2)
norm*sigma + mean</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Return float in [0,max) or [min,max) or [-r/2,r/2)</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomFloat</span>: <span class="hljs-function"><span class="hljs-params">(max)</span> -></span> Math.random() * max
<span class="hljs-attribute">randomFloat2</span>: <span class="hljs-function"><span class="hljs-params">(min, max)</span> -></span> min + Math.random() * (max-min)
<span class="hljs-attribute">randomCentered</span>: <span class="hljs-function"><span class="hljs-params">(r)</span> -></span> <span class="hljs-property">@randomFloat2</span> -r/<span class="hljs-number">2</span>, r/<span class="hljs-number">2</span></pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Return log n where base is 10, base, e respectively.
Note ln: (n) -> Math.log n .. i.e. JS’s log is log base e</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">log10</span>: <span class="hljs-function"><span class="hljs-params">(n)</span> -></span> Math.log(n)/Math.LN10
<span class="hljs-attribute">log2</span>: <span class="hljs-function"><span class="hljs-params">(n)</span> -></span> <span class="hljs-property">@logN</span> n, <span class="hljs-number">2</span>
<span class="hljs-attribute">logN</span>: <span class="hljs-function"><span class="hljs-params">(n, base)</span> -></span> Math.log(n)/Math.log(base)</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Return true <a href="http://goo.gl/spr24">mod functin</a>, % is remainder, not mod.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">mod</span>: <span class="hljs-function"><span class="hljs-params">(v, n)</span> -></span> ((v % n) + n) % n</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Return v to be between min, max via mod fcn</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">wrap</span>: <span class="hljs-function"><span class="hljs-params">(v, min, max)</span> -></span> min + <span class="hljs-property">@mod</span>(v-min, max-min)</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>Return v to be between min, max via clamping with Math.min/max</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">clamp</span>: <span class="hljs-function"><span class="hljs-params">(v, min, max)</span> -></span></pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Math.max(Math.min(v,max),min) appears to be slower</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> v < min <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> min
<span class="hljs-keyword">if</span> v > max <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> max
v</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>Return sign of a number as +/- 1</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">sign</span>: <span class="hljs-function"><span class="hljs-params">(v)</span> -></span> <span class="hljs-keyword">if</span> v<<span class="hljs-number">0</span> <span class="hljs-keyword">then</span> -<span class="hljs-number">1</span> <span class="hljs-keyword">else</span> <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Return n to given precision, default 2
Considerably faster than equivalent: Number(n.toFixed(p))</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">fixed</span>: <span class="hljs-function"><span class="hljs-params">(n,p=<span class="hljs-number">2</span>)</span> -></span> p = Math.pow(<span class="hljs-number">10</span>,p); Math.round(n*p)/p</pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Return an array of floating pt numbers as strings at given precision;
useful for printing</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">aToFixed</span>: <span class="hljs-function"><span class="hljs-params">(a, p=<span class="hljs-number">2</span>)</span> -></span> (i.toFixed p <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> a)</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Return localized string for number, with commas etc</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">tls</span>: <span class="hljs-function"><span class="hljs-params">(n)</span> -></span> n.toLocaleString()</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<h3 id="string-operations">String Operations</h3>
</div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Convert camelCase to CamelCase, capitolizing first character</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">upperCamelCase</span>: <span class="hljs-function"><span class="hljs-params">(str)</span> -></span> str.charAt(<span class="hljs-number">0</span>).toUpperCase() + str.substr(<span class="hljs-number">1</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="color-and-angle-operations">Color and Angle Operations</h3>
<p>Our colors are r,g,b,[a] arrays, with an optional color.str HTML
color string property. The str value is set on the first call to colorStr</p>
</div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Return a random RGB or gray color. Array passed to minimize garbage collection</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomColor</span>: <span class="hljs-function"><span class="hljs-params">(c = [])</span> -></span></pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>c.str = null if c.str?
c[i] = @randomInt(256) for i in [0..2]
c</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.randomColor: use ColorMaps.randomColor"</span>
<span class="hljs-property">@error</span> <span class="hljs-string">"Util.randomColor: c cannot be exiting color"</span> <span class="hljs-keyword">if</span> c.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
ColorMaps.randomColor()</pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Note: if 2 args passed, assume they’re min, max w/ default c</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomGray</span>: <span class="hljs-function"><span class="hljs-params">(c = [], min = <span class="hljs-number">64</span>, max = <span class="hljs-number">192</span>)</span> -></span>
<span class="hljs-keyword">if</span> arguments.length <span class="hljs-keyword">is</span> <span class="hljs-number">2</span> <span class="hljs-keyword">then</span> <span class="hljs-keyword">return</span> <span class="hljs-property">@randomGray</span> <span class="hljs-literal">null</span>, c, min</pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>c.str = null if c.str?
r=@randomInt2 min,max
c[i] = r for i in [0..2]
c</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.randomGray: use ColorMaps.randomGray"</span>
<span class="hljs-property">@error</span> <span class="hljs-string">"Util.randomGray: c cannot be exiting color"</span> <span class="hljs-keyword">if</span> c.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
ColorMaps.randomGray(min, max)</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Random color from a colormap set of r,g,b values.
Default is one of 125 (5^3) colors</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">randomMapColor</span>: <span class="hljs-function"><span class="hljs-params">(c = [], set = [<span class="hljs-number">0</span>,<span class="hljs-number">63</span>,<span class="hljs-number">127</span>,<span class="hljs-number">191</span>,<span class="hljs-number">255</span>])</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.randomMapColor: use ColorMaps.randomColor() or similar"</span>
<span class="hljs-property">@error</span> <span class="hljs-string">"Util.randomMapColor: c cannot be exiting color"</span> <span class="hljs-keyword">if</span> c.length <span class="hljs-keyword">isnt</span> <span class="hljs-number">0</span>
ColorMaps.randomColor()</pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>@setColor c, @oneOf(set), @oneOf(set), @oneOf(set)
randomBrightColor: (c=[]) -> @randomMapColor c, [0,127,255]
randomHSBColor: (c=[]) ->
c = @hsbToRgb([@randomInt(51)*5,255,255])
Modify an existing rgb or gray color. Alpha optional, not set if not provided.
Modifying an existing array minimizes GC overhead</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setColor</span>: <span class="hljs-function"><span class="hljs-params">(c, r, g, b, a)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.setColor: use the Color/ColorMaps modules"</span>
<span class="hljs-keyword">if</span> c.setColor? <span class="hljs-comment"># typedColor</span>
<span class="hljs-keyword">if</span> c.map
<span class="hljs-property">@error</span> <span class="hljs-string">"Util.setColor: cannot modify colormap colors"</span>
<span class="hljs-keyword">else</span>
c.setColor r, g, b, a
<span class="hljs-keyword">else</span>
c[<span class="hljs-number">0</span>] = r; c[<span class="hljs-number">1</span>] = g; c[<span class="hljs-number">2</span>] = b; c[<span class="hljs-number">3</span>] = a <span class="hljs-keyword">if</span> a?
c</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>c.str = null if c.str?
c[0] = r; c[1] = g; c[2] = b; c[3] = a if a?
c</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setGray</span>: <span class="hljs-function"><span class="hljs-params">(c, g, a)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.setGray: use the Color/ColorMaps modules"</span>
<span class="hljs-property">@setColor</span> c, g, g, g, a</pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>Return new color, c, by scaling each value of the rgb color max.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">scaleColor</span>: <span class="hljs-function"><span class="hljs-params">(max, s, c = [])</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.scaleColor: use the Color/ColorMaps modules"</span>
<span class="hljs-property">@error</span> <span class="hljs-string">"Util.scaleColor: cannot modify colormap colors"</span>
ColorMaps.Rgb.findClosestColor Color.rgbLerp(max, s)</pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>c.str = null if c.str?
c[i] = @clamp(Math.round(val*s),0,255) for val, i in max # [r,g,b] must be ints
c
Return color with new opacity, result, by scaling a value of rgba.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">scaleOpacity</span>: <span class="hljs-function"><span class="hljs-params">(rgba, scale, c = <span class="hljs-property">@clone</span>(rgba))</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.scaleOpacity: use the Color/ColorMaps modules"</span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@isArray</span> rgba
c[<span class="hljs-number">3</span>] = <span class="hljs-number">1</span> <span class="hljs-keyword">unless</span> c[<span class="hljs-number">3</span>]?
c[<span class="hljs-number">3</span>] = <span class="hljs-property">@lerp</span> <span class="hljs-number">0</span>, c[<span class="hljs-number">3</span>], scale
<span class="hljs-keyword">else</span> <span class="hljs-comment"># typed color</span>
c[<span class="hljs-number">3</span>] = <span class="hljs-property">@lerp</span> <span class="hljs-number">0</span>, c[<span class="hljs-number">3</span>], scale
<span class="hljs-keyword">if</span> c.map
c = Color.typedColor c...
<span class="hljs-keyword">else</span>
c.setColor c...
c</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>result.str = null if result.str?
if rgba.length == 3
then rgba.push(1)
result[3] = parseFloat(@clamp((rgba[3]*scale),0,1).toFixed(3))
result</p>
</div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>Return HTML color as used by canvas element. Can include Alpha</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">colorStr</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.colorStr: use Color module or typedColor"</span>
c.css ? Color.arrayToColor c, <span class="hljs-string">"css"</span></pre></div></div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>return s if (s = c.str)?
@error “alpha > 1” if c.length is 4 and c[3] > 1
c.str = if c.length is 3 then “rgb(#{c})” else “rgba(#{c})”
Compare two colors. Alas, there is no array.Equal operator.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">colorsEqual</span>: <span class="hljs-function"><span class="hljs-params">(c1, c2)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.colorsEqual: use Color/ColorMaps or typedColor.pixel"</span>
Color.colorsEqual c1, c2</pre></div></div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<p>c1.toString() is c2.toString()
Convert r,g,b to a luminance float value (not color array).
Round for 0-255 int for gray color value.
<a href="http://goo.gl/pE9cV8">Good post on image filters</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbToGray</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.rgbToGray: use Color.rgbIntensity"</span>
Color.rgbIntensity c...</pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>0.2126<em>c[0] + 0.7152</em>c[1] + 0.0722*c[2]
RGB <> HSB (HSV) conversions.
RGB in [0-255], HSB in [0-1]
See <a href="http://en.wikipedia.org/wiki/HSV_color_space">Wikipedia</a>
and <a href="http://goo.gl/7yP4cO">Blog Post</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbToHsb</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.rgbToHsb: use extras/rgbToHsl"</span>
r=c[<span class="hljs-number">0</span>]/<span class="hljs-number">255</span>; g=c[<span class="hljs-number">1</span>]/<span class="hljs-number">255</span>; b=c[<span class="hljs-number">2</span>]/<span class="hljs-number">255</span>
max = Math.max(r,g,b); min = Math.min(r,g,b); v = max
h = <span class="hljs-number">0</span>; d = max-min; s = <span class="hljs-keyword">if</span> max <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> <span class="hljs-number">0</span> <span class="hljs-keyword">else</span> d/max
<span class="hljs-keyword">if</span> max <span class="hljs-keyword">isnt</span> min <span class="hljs-keyword">then</span> <span class="hljs-keyword">switch</span> max
<span class="hljs-keyword">when</span> r <span class="hljs-keyword">then</span> h = (g - b) / d + (<span class="hljs-keyword">if</span> g < b <span class="hljs-keyword">then</span> <span class="hljs-number">6</span> <span class="hljs-keyword">else</span> <span class="hljs-number">0</span>)
<span class="hljs-keyword">when</span> g <span class="hljs-keyword">then</span> h = (b - r) / d + <span class="hljs-number">2</span>
<span class="hljs-keyword">when</span> b <span class="hljs-keyword">then</span> h = (r - g) / d + <span class="hljs-number">4</span>
[Math.round(<span class="hljs-number">255</span>*h/<span class="hljs-number">6</span>), Math.round(<span class="hljs-number">255</span>*s), Math.round(<span class="hljs-number">255</span>*v)]
<span class="hljs-attribute">hsbToRgb</span>: <span class="hljs-function"><span class="hljs-params">(c)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.hsbToRgb: use Color/ColorMaps HSL functions"</span></pre></div></div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>Could use ths but very slow.</p>
<pre><code>Color.hslToRgb c[<span class="hljs-number">0</span>]*<span class="hljs-number">360</span>/<span class="hljs-number">255</span>, c[<span class="hljs-number">1</span>]*<span class="hljs-number">100</span>/<span class="hljs-number">255</span>, c[<span class="hljs-number">2</span>]*<span class="hljs-number">50</span>/<span class="hljs-number">255</span> <span class="hljs-comment"># very slow</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre> h=c[<span class="hljs-number">0</span>]/<span class="hljs-number">255</span>; s=c[<span class="hljs-number">1</span>]/<span class="hljs-number">255</span>; v=c[<span class="hljs-number">2</span>]/<span class="hljs-number">255</span>; i = Math.floor(h*<span class="hljs-number">6</span>)
f = h * <span class="hljs-number">6</span> - i; p = v * (<span class="hljs-number">1</span> - s)
q = v * (<span class="hljs-number">1</span> - f * s); t = v * (<span class="hljs-number">1</span> - (<span class="hljs-number">1</span> - f) * s)
<span class="hljs-keyword">switch</span>(i % <span class="hljs-number">6</span>)
<span class="hljs-keyword">when</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> r = v; g = t; b = p
<span class="hljs-keyword">when</span> <span class="hljs-number">1</span> <span class="hljs-keyword">then</span> r = q; g = v; b = p
<span class="hljs-keyword">when</span> <span class="hljs-number">2</span> <span class="hljs-keyword">then</span> r = p; g = v; b = t
<span class="hljs-keyword">when</span> <span class="hljs-number">3</span> <span class="hljs-keyword">then</span> r = p; g = q; b = v
<span class="hljs-keyword">when</span> <span class="hljs-number">4</span> <span class="hljs-keyword">then</span> r = t; g = p; b = v
<span class="hljs-keyword">when</span> <span class="hljs-number">5</span> <span class="hljs-keyword">then</span> r = v; g = p; b = q
[Math.round(r*<span class="hljs-number">255</span>), Math.round(g*<span class="hljs-number">255</span>), Math.round(b*<span class="hljs-number">255</span>)]</pre></div></div>
</li>
<li id="section-42">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-42">¶</a>
</div>
<p>Colormap factories, building a colormap of a particular type</p>
</div>
</li>
<li id="section-43">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-43">¶</a>
</div>
<p>Create a colormap by rgb values. R, G, B can be either a number,
the number of steps beteen 0-255, or an array of values to use
for the color. Ex: R = 3, corresponds to [0, 128, 255]
The resulting map permutes the R, G, V values. Thus if
R=G=B=4, the resulting map has 4<em>4</em>4=64 colors.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">rgbMap</span>: <span class="hljs-function"><span class="hljs-params">(R,G=R,B=R)</span> -></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.rgbMap: use ColorMaps.rgbColorMap or Rgb/Rgb256"</span>
ColorMaps.rgbColorMap R, G, B</pre></div></div>
</li>
<li id="section-44">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-44">¶</a>
</div>
<p>R = (Math.round(i<em>255/(R-1)) for i in [0…R]) if typeof R is “number”
G = (Math.round(i</em>255/(G-1)) for i in [0…G]) if typeof G is “number”
B = (Math.round(i*255/(B-1)) for i in [0…B]) if typeof B is “number”
map=[]; ((map.push [r,g,b] for b in B) for g in G) for r in R
map
Create a gray map of all 256 gray values</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">grayMap</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.grayMap: use ColorMaps.grayColorMap or Gray"</span>
ColorMaps.Gray</pre></div></div>
</li>
<li id="section-45">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-45">¶</a>
</div>
<p>([i,i,i] for i in [0..255])
Create an hsb map with n hues, with constant saturation
and brightness.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">hsbMap</span>: <span class="hljs-function"><span class="hljs-params">(n=<span class="hljs-number">256</span>, s=<span class="hljs-number">255</span>,b=<span class="hljs-number">255</span>)</span>-></span>
<span class="hljs-property">@deprecated</span> <span class="hljs-string">"Util.hsbMap: use ColorMaps.hslColorMap"</span>
ColorMaps.hslColorMap n, <span class="hljs-number">1</span>, <span class="hljs-number">1</span></pre></div></div>
</li>
<li id="section-46">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-46">¶</a>
</div>
<p>Return little/big endian-ness of hardware.
See Mozilla pixel <a href="http://goo.gl/Lxliq">manipulation article</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">isLittleEndian</span>: <span class="hljs-function">-></span></pre></div></div>
</li>
<li id="section-47">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-47">¶</a>
</div>
<p>convert 1-int array to typed array</p>
</div>
<div class="content"><div class='highlight'><pre> d32 = <span class="hljs-keyword">new</span> Uint32Array [<span class="hljs-number">0x01020304</span>]</pre></div></div>
</li>
<li id="section-48">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-48">¶</a>
</div>
<p>return true if byte order reversed</p>
</div>
<div class="content"><div class='highlight'><pre> (<span class="hljs-keyword">new</span> Uint8ClampedArray d32.buffer)[<span class="hljs-number">0</span>] <span class="hljs-keyword">is</span> <span class="hljs-number">4</span></pre></div></div>
</li>
<li id="section-49">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-49">¶</a>
</div>
<p>Convert between degrees and radians. We/Math package use radians.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">degToRad</span>: <span class="hljs-function"><span class="hljs-params">(degrees)</span> -></span> degrees * Math.PI / <span class="hljs-number">180</span>
<span class="hljs-attribute">radToDeg</span>: <span class="hljs-function"><span class="hljs-params">(radians)</span> -></span> radians * <span class="hljs-number">180</span> / Math.PI</pre></div></div>
</li>
<li id="section-50">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-50">¶</a>
</div>
<p>Return angle in (-pi,pi] that added to rad2 = rad1
See NetLogo’s <a href="http://goo.gl/CjoHuV">subtract-headings</a> for explanation</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">subtractRads</span>: <span class="hljs-function"><span class="hljs-params">(rad1, rad2)</span> -></span>
dr = rad1-rad2; PI = Math.PI
dr += <span class="hljs-number">2</span>*PI <span class="hljs-keyword">if</span> dr <= -PI; dr -= <span class="hljs-number">2</span>*PI <span class="hljs-keyword">if</span> dr > PI; dr</pre></div></div>
</li>
<li id="section-51">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-51">¶</a>
</div>
<h3 id="object-operations">Object Operations</h3>
</div>
</li>
<li id="section-52">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-52">¶</a>
</div>
<p>Return object’s own key or variable values</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">ownKeys</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> (key <span class="hljs-keyword">for</span> own key, value <span class="hljs-keyword">of</span> obj)
<span class="hljs-attribute">ownVarKeys</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> (key <span class="hljs-keyword">for</span> own key, value <span class="hljs-keyword">of</span> obj <span class="hljs-keyword">when</span> <span class="hljs-keyword">not</span> <span class="hljs-property">@isFunction</span> value)
<span class="hljs-attribute">ownValues</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span> (value <span class="hljs-keyword">for</span> own key, value <span class="hljs-keyword">of</span> obj)</pre></div></div>
</li>
<li id="section-53">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-53">¶</a>
</div>
<p>Mix the attributes from one object into another</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">mixinObject</span>: <span class="hljs-function"><span class="hljs-params">(destObj, srcObject)</span> -></span></pre></div></div>
</li>
<li id="section-54">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-54">¶</a>
</div>
<p>Get <em>all</em> own props, not just enumerable.</p>
</div>
<div class="content"><div class='highlight'><pre> keys = Object.getOwnPropertyNames(srcObject)
<span class="hljs-keyword">for</span> key <span class="hljs-keyword">in</span> keys
prop = Object.getOwnPropertyDescriptor(srcObject, key)
Object.defineProperty(destObj, key, prop)
destObj</pre></div></div>
</li>
<li id="section-55">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-55">¶</a>
</div>
<p>Return a copy of an object, with the prototype also set in the copy
when the object has a prototype other than Object.prototype</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">cloneObject</span>: <span class="hljs-function"><span class="hljs-params">(obj)</span> -></span>
newObj = Object.create(Object.getPrototypeOf(obj))
<span class="hljs-property">@mixinObject</span> newObj, obj</pre></div></div>
</li>
<li id="section-56">
<div class="annotation">