-
Notifications
You must be signed in to change notification settings - Fork 57
/
experiment.php
1608 lines (1479 loc) · 65.7 KB
/
experiment.php
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>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>iWantHue - Experiment</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<?php include('includes/codetop.php') ?>
<style>
body {
padding-top: 60px;
padding-bottom: 40px;
}
/* Specific styles */
#hs_div, #sl_div, #hl_div, #sv_div, #hv_div, #rg_div, #gb_div, #rb_div{
max-width: 300px;
}
.generator button{
width: 100%;
height: 30px;
}
.infotitle{
font-family: "Helvetica Neue", "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;
color:#a99;
}
.info{
font-family: "Helvetica Neue", "Lucida Grande", Helvetica, Arial, Verdana, sans-serif;
color: #a99;
text-indent: 20px;
}
.generator{
margin-bottom: 30px;
}
.generators_zone{
overflow-y: scroll;
height: 280px;
border-top: 1px solid #a99;
border-bottom: 1px solid #a99;
margin-top: 30px;
margin-bottom: 10px;
margin-right: -40px;
}
</style>
</head>
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->
<?php include('includes/header.php') ?>
<div class="container">
<!-- Main hero unit for a primary marketing message or call to action -->
<div class="splash-unit row">
<div class="span7">
<div class="image">
<a href="index.php"><img src="res/header.png"/></a>
</div>
<div class="title">
i want hue
</div>
</div>
<div class="span5">
<div class="abstract">
<p><strong>Colors for data scientists.</strong> Generate and refine palettes of optimally distinct colors.</p>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="span12">
<h1>Random Colors: Experiment</h1>
</div>
</div>
<div class="row">
<div class="span3">
<h3>How it works</h3>
<p>
Choose a generator and look at how the random colors are distributed.
</p>
<p>
The purpose here is to observe the properties of different generators of colors.
</p>
</div>
<div class="span3">
<h3>Settings</h3>
<p>
<input id="nodescount" type="text" value="20" style="width:35px;"/> Colors
</p>
<p>
<input id="scatterplot" type="checkbox"/> Scatterplot style
</p>
</div>
<div class="span3">
<h3 class="infotitle">Use case A</h3>
<div class="info">
<p>
Set a small number of colors (10 to 50), generate several times to look if it fits your expectations.
</p>
</div>
</div>
<div class="span3">
<h3 class="infotitle">Use case B</h3>
<div class="info">
<p>
Set a large number of colors (1000 or more), check "scatterplot style", generate and you will observe how the probabilities are distributed (takes more time).
</p>
</div>
</div>
</div>
<div class="generators_zone row"><div class="span12">
<div class="row">
<div class="span2 generator">
<h3>RGB Naïve</h3>
<button class="go" onclick='rgbNaive()'>Go</button>
<script>
function rgbNaive(){
var colors = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = chroma.rgb(255*Math.random(),255*Math.random(),255*Math.random());
colors.push(color);
}
drawBackground();
drawColors(colors);
}
</script>
<p>
A random value in each red/green/blue channel.
</p>
</div>
<div class="span2 generator">
<h3>HSL Naïve</h3>
<button class="go" onclick='hslNaive()'>Go</button>
<script>
function hslNaive(){
var colors = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = chroma.hsl(360*Math.random(),Math.random(),Math.random());
colors.push(color);
}
drawBackground();
drawColors(colors);
}
</script>
<p>
A random value in each hue/saturation/lightness channel.
</p>
</div>
<div class="span2 generator">
<h3>HSV Naïve</h3>
<button class="go" onclick='hsvNaive()'>Go</button>
<script>
function hsvNaive(){
var colors = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = chroma.hsv(360*Math.random(),Math.random(),Math.random());
colors.push(color);
}
drawBackground();
drawColors(colors);
}
</script>
<p>
A random value in each hue/saturation/value channel.
</p>
</div>
<div class="span6">
<h3 class="infotitle">Naïve generators:</h3>
<div class="info">
<p>
Random numbers do not necessary make nice swatches. You may read sometimes that HSV random colors are better than RGB. I think that it is clearly not the case.
</p>
<p>
These basic generators allow to see that the "randomness" strongly depends on the color space that is randomized.
</p>
</div>
</div>
</div>
<div class="row">
<div class="span2 generator">
<h3>Lab Naïve</h3>
<button class="go" onclick='labNaive()'>Go</button>
<script>
function labNaive(){
var colors = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = chroma.lab(Math.random(),2*Math.random()-1,2*Math.random()-1);
while(isNaN(color.rgb[0])){
color = chroma.lab(Math.random(),2*Math.random()-1,2*Math.random()-1);
}
colors.push(color);
}
drawBackground();
drawColors(colors);
}
</script>
<p>
A random value in each L*/a*/b* color coordinates (fits to how we perceive colors).
</p>
</div>
<div class="span2 generator">
<h3>Lab Pastel</h3>
<button class="go" onclick='labPastel()'>Go</button>
<script>
function labPastel(){
var colors = [];
function checkColor(color){
var lab = color.lab();
return !isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256 && lab[0]<0.95 && lab[0]>0.6;
}
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = chroma.lab(Math.random(),2*Math.random()-1,2*Math.random()-1);
while(!checkColor(color)){
color = chroma.lab(Math.random(),2*Math.random()-1,2*Math.random()-1);
}
colors.push(color);
}
drawBackground();
drawColors(colors);
}
</script>
<p>
A random value in each L*/a*/b* color coordinates, filtered to keep only pastel colors.
</p>
</div>
<div class="span2 generator">
<h3>Lab Pimp</h3>
<button class="go" onclick='labPimp()'>Go</button>
<script>
function labPimp(){
var colors = [];
function checkColor(color){
var lab = color.lab();
var hcl = color.hcl();
return !isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256 && hcl[2]>0.5 && hcl[1]>0.8;
}
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = chroma.lab(Math.random(),2*Math.random()-1,2*Math.random()-1);
while(!checkColor(color)){
color = chroma.lab(Math.random(),2*Math.random()-1,2*Math.random()-1);
}
colors.push(color);
}
drawBackground();
drawColors(colors);
}
</script>
<p>
A random value in each L*/a*/b* color coordinates, filtered to keep only flashy colors.
</p>
</div>
<div class="span6">
<h3 class="infotitle">Lab generators:</h3>
<div class="info">
<p>
The L*a*b* color space is the more natural to the eye. Randomizing in this space gives more natural results.
</p>
<p>
Adding constraints helps to set some properties of the produced palette ('Pimp', 'Pastel'), and keeps the natural distribution of the colors.
</p>
</div>
</div>
</div>
<div class="row">
<div class="span2 generator">
<h3>RGB Force Vector</h3>
<button class="go" onclick='rgbForceVector()'>Go</button>
<script>
function rgbForceVector(){
drawBackground();
var colors = [];
// Init
var vectors = {};
for(i=0; i<parseInt($('#nodescount').val()); i++){
var color = [255*Math.random(),255*Math.random(),255*Math.random()];
colors.push(color);
}
// Force vector: repulsion
var repulsion = 10;
var speed = 500;
var steps = 1000;
while(steps-- > 0){
// Init
for(i=0; i<colors.length; i++){
vectors[i] = {dr:0, dg:0, db:0};
}
// Compute Force
for(i=0; i<colors.length; i++){
var colorA = colors[i];
for(j=0; j<i; j++){
var colorB = colors[j];
// repulsion force
var dr = colorA[0]-colorB[0];
var dg = colorA[1]-colorB[1];
var db = colorA[2]-colorB[2];
var d = Math.sqrt(Math.pow(dr, 2)+Math.pow(dg, 2)+Math.pow(db, 2));
if(d>0){
var force = repulsion/Math.pow(d,2);
vectors[i].dr += dr * force / d;
vectors[i].dg += dg * force / d;
vectors[i].db += db * force / d;
vectors[j].dr -= dr * force / d;
vectors[j].dg -= dg * force / d;
vectors[j].db -= db * force / d;
} else {
// Jitter
vectors[j].dr += 2 - 4 * Math.random();
vectors[j].dg += 2 - 4 * Math.random();
vectors[j].db += 2 - 4 * Math.random();
}
}
}
// Apply Force
for(i=0; i<colors.length; i++){
var color = colors[i];
var displacement = speed * Math.sqrt(Math.pow(vectors[i].dr, 2)+Math.pow(vectors[i].dg, 2)+Math.pow(vectors[i].db, 2));
if(displacement>0){
var ratio = speed * Math.min(50, displacement)/displacement;
color[0] = Math.min(255, Math.max(0, color[0] + vectors[i].dr*ratio));
color[1] = Math.min(255, Math.max(0, color[1] + vectors[i].dg*ratio));
color[2] = Math.min(255, Math.max(0, color[2] + vectors[i].db*ratio));
}
}
}
drawColors(colors.map(function(rgb){return chroma.rgb(Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2]));}));
}
</script>
<p>
A Force Vector algorithm optimizes the distribution of colors inthe RGB color space.
</p>
</div>
<div class="span2 generator">
<h3>Lab Force Vector</h3>
<button class="go" onclick='labForceVector()'>Go</button>
<script>
function labForceVector(){
drawBackground();
var colors = [];
// It will be necessary to check if a Lab color exists in the rgb space.
function checkLab(lab){
var color = chroma.lab(lab[0], lab[1], lab[2]);
return !isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256;
}
// Init
var vectors = {};
for(i=0; i<parseInt($('#nodescount').val()); i++){
// Find a valid Lab color
var color = [Math.random(),2*Math.random()-1,2*Math.random()-1];
while(!checkLab(color)){
color = [Math.random(),2*Math.random()-1,2*Math.random()-1];
}
colors.push(color);
}
// Force vector: repulsion
var repulsion = 0.1;
var speed = 0.05;
var steps = 1000;
while(steps-- > 0){
// Init
for(i=0; i<colors.length; i++){
vectors[i] = {dl:0, da:0, db:0};
}
// Compute Force
for(i=0; i<colors.length; i++){
var colorA = colors[i];
for(j=0; j<i; j++){
var colorB = colors[j];
// repulsion force
var dl = colorA[0]-colorB[0];
var da = colorA[1]-colorB[1];
var db = colorA[2]-colorB[2];
var d = Math.sqrt(Math.pow(dl, 2)+Math.pow(da, 2)+Math.pow(db, 2));
if(d>0){
var force = repulsion/Math.pow(d,2);
vectors[i].dl += dl * force / d;
vectors[i].da += da * force / d;
vectors[i].db += db * force / d;
vectors[j].dl -= dl * force / d;
vectors[j].da -= da * force / d;
vectors[j].db -= db * force / d;
} else {
// Jitter
vectors[j].dl += 0.02 - 0.04 * Math.random();
vectors[j].da += 0.02 - 0.04 * Math.random();
vectors[j].db += 0.02 - 0.04 * Math.random();
}
}
}
// Apply Force
for(i=0; i<colors.length; i++){
var color = colors[i];
var displacement = speed * Math.sqrt(Math.pow(vectors[i].dl, 2)+Math.pow(vectors[i].da, 2)+Math.pow(vectors[i].db, 2));
if(displacement>0){
var ratio = speed * Math.min(0.1, displacement)/displacement;
candidateLab = [color[0] + vectors[i].dl*ratio, color[1] + vectors[i].da*ratio, color[2] + vectors[i].db*ratio];
if(checkLab(candidateLab)){
colors[i] = candidateLab;
}
}
//drawColors(colors.map(function(lab){return chroma.lab(lab[0], lab[1], lab[2]);}));
}
}
drawColors(colors.map(function(lab){return chroma.lab(lab[0], lab[1], lab[2]);}));
}
</script>
<p>
A Force Vector algorithm optimizes the distribution of colors inthe L*a*b* color space.
</p>
</div>
<div class="span2 generator">
<h3>Lab Fancy Force Vector</h3>
<button class="go" onclick='labFancyForceVector()'>Go</button>
<script>
function labFancyForceVector(){
drawBackground();
var colors = [];
// It will be necessary to check if a Lab color exists in the rgb space.
function checkLab(lab){
var color = chroma.lab(lab[0], lab[1], lab[2]);
var hcl = color.hcl();
return !isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256 && hcl[2]>0.6 && hcl[1]>0.6;
}
// Init
var vectors = {};
for(i=0; i<parseInt($('#nodescount').val()); i++){
// Find a valid Lab color
var color = [Math.random(),2*Math.random()-1,2*Math.random()-1];
while(!checkLab(color)){
color = [Math.random(),2*Math.random()-1,2*Math.random()-1];
}
colors.push(color);
}
// Force vector: repulsion
var repulsion = 0.2;
var speed = 0.05;
var steps = 1000;
while(steps-- > 0){
// Init
for(i=0; i<colors.length; i++){
vectors[i] = {dl:0, da:0, db:0};
}
// Compute Force
for(i=0; i<colors.length; i++){
var colorA = colors[i];
for(j=0; j<i; j++){
var colorB = colors[j];
// repulsion force
var dl = colorA[0]-colorB[0];
var da = colorA[1]-colorB[1];
var db = colorA[2]-colorB[2];
var d = Math.sqrt(Math.pow(dl, 2)+Math.pow(da, 2)+Math.pow(db, 2));
if(d>0){
var force = repulsion/Math.pow(d,2);
vectors[i].dl += dl * force / d;
vectors[i].da += da * force / d;
vectors[i].db += db * force / d;
vectors[j].dl -= dl * force / d;
vectors[j].da -= da * force / d;
vectors[j].db -= db * force / d;
} else {
// Jitter
vectors[j].dl += 0.02 - 0.04 * Math.random();
vectors[j].da += 0.02 - 0.04 * Math.random();
vectors[j].db += 0.02 - 0.04 * Math.random();
}
}
}
// Apply Force
for(i=0; i<colors.length; i++){
var color = colors[i];
var displacement = speed * Math.sqrt(Math.pow(vectors[i].dl, 2)+Math.pow(vectors[i].da, 2)+Math.pow(vectors[i].db, 2));
if(displacement>0){
var ratio = speed * Math.min(0.1, displacement)/displacement;
candidateLab = [color[0] + vectors[i].dl*ratio, color[1] + vectors[i].da*ratio, color[2] + vectors[i].db*ratio];
if(checkLab(candidateLab)){
colors[i] = candidateLab;
}
}
//drawColors(colors.map(function(lab){return chroma.lab(lab[0], lab[1], lab[2]);}));
}
}
drawColors(colors.map(function(lab){return chroma.lab(lab[0], lab[1], lab[2]);}));
}
</script>
<p>
A Force Vector algorithm optimizes the distribution of colors inthe L*a*b* color space. Additional conditions apply to restrain the color space to flashy colors.
</p>
</div>
<div class="span6 last">
<h3 class="infotitle">Force Vector generators:</h3>
<div class="info">
<p>
The Force Vector strategy allows an even distribution of the colors in the space. It still allows some coontrol on the produced palette ('Fancy').
</p>
<p>
Note that contrary to the "naïve" generators, the colors are generated all at the same time (they depend on each other).
</p>
</div>
</div>
</div>
<div class="row">
<div class="span2 generator">
<h3>RGB kMeans</h3>
<button class="go" onclick='rgbKMeans()'>Go</button>
<script>
function rgbKMeans(){
drawBackground();
var kMeans = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var rgb = [255*Math.random(),255*Math.random(),255*Math.random()];
kMeans.push(rgb);
}
var colorSamples = [];
var samplesClosest = [];
for(r=0; r<=255; r+=10){
for(g=0; g<=255; g+=10){
for(b=0; b<=255; b+=10){
var rgb = [r, g, b];
colorSamples.push(rgb);
samplesClosest.push(null);
}
}
}
// Steps
var steps = 50;
while(steps-- > 0){
// kMeans -> Samples Closest
for(i=0; i<colorSamples.length; i++){
var rgb = colorSamples[i];
var maxDistance = 1000000;
for(j=0; j<kMeans.length; j++){
var kMean = kMeans[j];
var distance = Math.sqrt(Math.pow(rgb[0]-kMean[0], 2) + Math.pow(rgb[1]-kMean[1], 2) + Math.pow(rgb[2]-kMean[2], 2));
if(distance < maxDistance){
maxDistance = distance;
samplesClosest[i] = j;
}
}
}
// Samples -> kMeans
for(j=0; j<kMeans.length; j++){
var count = 0;
kMeans[j] = [0, 0, 0];
for(i=0; i<colorSamples.length; i++){
if(samplesClosest[i] == j){
count++;
kMeans[j][0] += colorSamples[i][0];
kMeans[j][1] += colorSamples[i][1];
kMeans[j][2] += colorSamples[i][2];
}
}
kMeans[j][0] /= count;
kMeans[j][1] /= count;
kMeans[j][2] /= count;
}
}
drawColors(kMeans.map(function(rgb){return chroma.rgb(Math.round(rgb[0]), Math.round(rgb[1]), Math.round(rgb[2]));}));
}
</script>
<p>
The k-Means algorithm clusterises the RGB color space. The resulting palette is constituted of these k-Means (the "centers" of color clusters).
</p>
</div>
<div class="span2 generator">
<h3>Lab kMeans</h3>
<button class="go" onclick='labKMeans()'>Go</button>
<script>
function labKMeans(){
drawBackground();
function checkColor(color){
var lab = color.lab();
var hcl = color.hcl();
return !isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256 /*&& hcl[2]>0.5 && hcl[1]>0.8*/;
}
var kMeans = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var lab = [Math.random(),2*Math.random()-1,2*Math.random()-1];
while(!checkColor(chroma.lab(lab))){
lab = [Math.random(),2*Math.random()-1,2*Math.random()-1];
}
kMeans.push(lab);
}
var colorSamples = [];
var samplesClosest = [];
for(l=0; l<=1; l+=0.05){
for(a=-1; a<=1; a+=0.1){
for(b=-1; b<=1; b+=0.1){
if(checkColor(chroma.lab(l, a, b))){
colorSamples.push([l, a, b]);
samplesClosest.push(null);
}
}
}
}
// Steps
var steps = 50;
while(steps-- > 0){
// kMeans -> Samples Closest
for(i=0; i<colorSamples.length; i++){
var lab = colorSamples[i];
var minDistance = 1000000;
for(j=0; j<kMeans.length; j++){
var kMean = kMeans[j];
var distance = Math.sqrt(Math.pow(lab[0]-kMean[0], 2) + Math.pow(lab[1]-kMean[1], 2) + Math.pow(lab[2]-kMean[2], 2));
if(distance < minDistance){
minDistance = distance;
samplesClosest[i] = j;
}
}
}
// Samples -> kMeans
for(j=0; j<kMeans.length; j++){
var count = 0;
var candidateKMean = [0, 0, 0];
for(i=0; i<colorSamples.length; i++){
if(samplesClosest[i] == j){
count++;
candidateKMean[0] += colorSamples[i][0];
candidateKMean[1] += colorSamples[i][1];
candidateKMean[2] += colorSamples[i][2];
}
}
candidateKMean[0] /= count;
candidateKMean[1] /= count;
candidateKMean[2] /= count;
if(checkColor(chroma.lab(candidateKMean[0], candidateKMean[1], candidateKMean[2]))){
kMeans[j] = candidateKMean;
} else {
// The candidate kMean is out of the boundaries of the color space...
// Then we just search for the closest color, in the sample, of the candidate kMean
var minDistance = 1000000;
var closest = -1;
for(i=0; i<colorSamples.length; i++){
if(samplesClosest[i] == j){
var distance = Math.sqrt(Math.pow(colorSamples[i][0]-candidateKMean[0], 2) + Math.pow(colorSamples[i][1]-candidateKMean[1], 2) + Math.pow(colorSamples[i][2]-candidateKMean[2], 2));
if(distance < minDistance){
minDistance = distance;
closest = i;
}
}
}
kMeans[j] = colorSamples[closest];
}
}
}
drawColors(kMeans.map(function(lab){return chroma.lab(lab[0], lab[1], lab[2]);}));
}
</script>
<p>
The k-Means algorithm clusterises the L*a*b* color space. The resulting palette is constituted of these k-Means (the "centers" of color clusters).
</p>
</div>
<div class="span2 generator">
<h3>Lab Fancy kMeans</h3>
<button class="go" onclick='labFancyKMeans()'>Go</button>
<script>
function labFancyKMeans(){
drawBackground();
function checkColor(color){
var lab = color.lab();
var hcl = color.hcl();
return !isNaN(color.rgb[0]) && color.rgb[0]>=0 && color.rgb[1]>=0 && color.rgb[2]>=0 && color.rgb[0]<256 && color.rgb[1]<256 && color.rgb[2]<256 && hcl[2]>0.6 && hcl[1]>0.6;
}
var kMeans = [];
for(i=0; i<parseInt($('#nodescount').val()); i++){
var lab = [Math.random(),2*Math.random()-1,2*Math.random()-1];
while(!checkColor(chroma.lab(lab))){
lab = [Math.random(),2*Math.random()-1,2*Math.random()-1];
}
kMeans.push(lab);
}
var colorSamples = [];
var samplesClosest = [];
for(l=0; l<=1; l+=0.05){
for(a=-1; a<=1; a+=0.1){
for(b=-1; b<=1; b+=0.1){
if(checkColor(chroma.lab(l, a, b))){
colorSamples.push([l, a, b]);
samplesClosest.push(null);
}
}
}
}
// Steps
var steps = 50;
while(steps-- > 0){
// kMeans -> Samples Closest
for(i=0; i<colorSamples.length; i++){
var lab = colorSamples[i];
var minDistance = 1000000;
for(j=0; j<kMeans.length; j++){
var kMean = kMeans[j];
var distance = Math.sqrt(Math.pow(lab[0]-kMean[0], 2) + Math.pow(lab[1]-kMean[1], 2) + Math.pow(lab[2]-kMean[2], 2));
if(distance < minDistance){
minDistance = distance;
samplesClosest[i] = j;
}
}
}
// Samples -> kMeans
for(j=0; j<kMeans.length; j++){
var count = 0;
var candidateKMean = [0, 0, 0];
for(i=0; i<colorSamples.length; i++){
if(samplesClosest[i] == j){
count++;
candidateKMean[0] += colorSamples[i][0];
candidateKMean[1] += colorSamples[i][1];
candidateKMean[2] += colorSamples[i][2];
}
}
candidateKMean[0] /= count;
candidateKMean[1] /= count;
candidateKMean[2] /= count;
if(checkColor(chroma.lab(candidateKMean[0], candidateKMean[1], candidateKMean[2]))){
kMeans[j] = candidateKMean;
} else {
// The candidate kMean is out of the boundaries of the color space...
// Then we just search for the closest color, in the sample, of the candidate kMean
var minDistance = 1000000;
var closest = -1;
for(i=0; i<colorSamples.length; i++){
if(samplesClosest[i] == j){
var distance = Math.sqrt(Math.pow(colorSamples[i][0]-candidateKMean[0], 2) + Math.pow(colorSamples[i][1]-candidateKMean[1], 2) + Math.pow(colorSamples[i][2]-candidateKMean[2], 2));
if(distance < minDistance){
minDistance = distance;
closest = i;
}
}
}
kMeans[j] = colorSamples[closest];
}
}
}
drawColors(kMeans.map(function(lab){return chroma.lab(lab[0], lab[1], lab[2]);}));
}
</script>
<p>
Idem, but with an additional constraint (high chroma and lightness*).
</p>
</div>
<div class="span6">
<h3 class="infotitle">K-Means generators:</h3>
<div class="info">
<p>
This strategy also allows an even distribution of the colors, but with less "extreme" colors (the borders of the color space are not occupied). It allows more coordinated colors on small palettes.
</p>
<p>
NB: The palette strongly depends on the number of colors asked.
</p>
</div>
</div>
</div>
</div></div>
<div class="row">
<div class="span3">
<br/>
<b>Green x Blue</b>
<div id="gb_div"></div>
</div>
<div class="span3">
<br/>
<b>a* x b*</b>
<div id="aStarbStar_div"></div>
</div>
<div class="span3">
<br/>
<b>h* x L*</b>
<div id="hStarLStar_div"></div>
</div>
<div class="span3">
<br/>
<b>Hue x Value</b>
<div id="hv_div"></div>
</div>
</div>
<div class="row">
<div class="span3">
<br/>
<b>Red x Green</b>
<div id="rg_div"></div>
</div>
<div class="span3">
<br/>
<b>L* x a*</b>
<div id="LStaraStar_div"></div>
</div>
<div class="span3">
<br/>
<b>Hue x Lightness</b>
<div id="hl_div"></div>
</div>
<div class="span3">
<br/>
<b>Hue x Saturation</b>
<div id="hs_div"></div>
</div>
</div>
<div class="row">
<div class="span3">
<br/>
<b>Red x Blue</b>
<div id="rb_div"></div>
</div>
<div class="span3">
<br/>
<b>L* x b*</b>
<div id="LStarbStar_div"></div>
</div>
<div class="span3">
<br/>
<b>Saturation x Lightness</b>
<div id="sl_div"></div>
</div>
<div class="span3">
<br/>
<b>Saturation x Value</b>
<div id="sv_div"></div>
</div>
</div>
<div class="row">
<div id="width_reference" class="span12">
<br/>
<b>Random colors</b>
<div id="random_div"></div>
</div>
</div>
<div class="row">
<div class="span12">
<br/>
<b>Sorted by Hue</b>
<div id="h_div"></div>
</div>
</div>
<div class="row">
<div class="span12">
<br/>
<b>Sorted by Saturation</b>
<div id="s_div"></div>
</div>
</div>
<div class="row">
<div class="span12">
<br/>
<b>Sorted by Chroma (similar to saturation, but normalized to fit our perception)</b>
<div id="c_div"></div>
</div>
</div>
<div class="row">
<div class="span12">
<br/>
<b>Sorted by Lightness</b>
<div id="l_div"></div>
</div>
</div>
<div class="row">
<div class="span12">
<br/>
<b>Sorted by Lightness* (normalized to fit our perception)</b>
<div id="lStar_div"></div>
</div>
</div>
<div class="row">
<div class="span12">
<br/>
<b>Sorted by Value</b>
<div id="v_div"></div>
</div>
</div>
<div class="row" hidden="true">
<div class="span12">
<br/>
<b>Sorted by Red channel</b>
<div id="r_div"></div>
</div>
</div>
<div class="row" hidden="true">
<div class="span12">
<br/>
<b>Sorted by Green channel</b>
<div id="g_div"></div>
</div>
</div>
<div class="row" hidden="true">
<div class="span12">
<br/>
<b>Sorted by Blue channel</b>
<div id="b_div"></div>
</div>
</div>
<div class="row">
<div class="span6">
<h3 class="infotitle">Properties of color spaces</h3>
<div class="info">
<p>
<b>RGB</b> or "Red Green Blue": the most easy to compute since it fits to how computer screens and TV work. Actually it fits quite well to our perception.
</p>
<p>
<b>HSV</b> or "Hue Saturation Value": used in softwares like Photoshop, since it allows to separate the hue (the color itself) for other parameters. It allows strong perceptual biases since the hues are not perceived with the same lightness (yellow is light, blue is dark...).
</p>
<p>
<b>HSL</b> or "Hue Saturation Lightness" is a variant of this space, and is biased as well.
</p>
<p>
<b>L*a*b*</b> or "Lightness* a* b*" is a space designed to fit to our perception. Lightness* is the unbiased version of lightness (a scale from black to color to white) and the a* and b* dimensions describe the colors (and grays).
It is not easy to deal with it, since every color does not exist, but it gives the most appropriate results.
</p>
<p>
<b>HCL</b> or "Hue Chroma Lightness*" is a transformation of L*a*b* where the Hue has been separated. It can be considered as the unbiased version of HSL, where saturation is named Chroma and the unbiased version of Lightness is Lightness* (the star indicates the difference).
Like with L*a*b*, every color does not exist. What's more, it is not a convex space, and this is an issue for computing colors. That's why I prefer L*a*b* to compute, but HCL is the best space to monitor results.
</p>
</div>
</div>
<div class="span6">
<h3 class="infotitle">Reading the color spaces</h3>
<div class="info">
<p>
The squares above are projections of different color spaces. Example: the RGB space is represented with R x G, G x B and R x B.
They allow you to determine where the colors accumulate and where they are sparse.
For instance you will observe that an even distribution in a given space (say, HSV) can be uneven in another space (RGB).
</p>
<p>
We assume that our goal is to generate varied colors. So we want to avoid accumulation, since it means that some colors are too similar, thus the palette is confusing.
</p>
<p>
The most suitable palettes are probably the ones that are evenly distributed in the L*a*b* color space.
</p>
</div>
<h3 class="infotitle">Reading sorted palettes</h3>
<div class="info">
<p>
The sorted palette contain the exact same colors (the ones that were generated) but sorted different ways.
</p>
<p>
The goal is just to check if there are similar colors. Sorting the colors allows to put similar colors together. For instance if two shades of blue are very similar, they will be put together in the "Hue" palette.
</p>
</div>
</div>
</div>
<div class="row">
<div class="span12">
<h2>The "best" palette</h3>
<b>Sorted by differenciation first</b>
<div id="diff_div"></div>
<p>
Here are the colors sorted so that the most different colors are put first. This is very useful in many cases, since we often know what we want to differentiate in priority.
</p>
</div>
</div>
<div class="row">
<div class="span6">