-
Notifications
You must be signed in to change notification settings - Fork 24
/
color.clj
executable file
·3467 lines (2975 loc) · 118 KB
/
color.clj
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
(ns clojure2d.color
"Color functions.
This namespace contains color manipulation functions which can be divided into following groups:
* Color creators
* Channel manipulations
* Conversions
* Palettes / gradients
* Distances
## Representation
Color can be represented by following types:
* fastmath `Vec4` - this is core type representing 3 color channels and alpha (RGBA). Values are `double` type from `[0-255]` range. [[color]], [[gray]] creators returns `Vec4` representation.
* fastmath `Vec3` - 3 channels (RGB), assuming `alpha` set to value of `255`.
* fastmath `Vec2` - gray with alpha
* `java.awt.Color` - Java AWT representation. Creators are [[awt-color]], [[awt-gray]]. Use [[to-awt-color]] to convert to this representation.
* `keyword` - one of the defined names (see [[named-colors-list]])
* `Integer` - packed ARGB value. If value is less than `0xff000000`, alpha is set to `0xff`. Example: `0xffaa01`.
* `String` - CSS (`#rgb` or `#rgba`) or string containg hexadecimal representation (\"ffaa01\")
* any `seqable` - list, vector containing 2-4 elements. Conversion is done by applying content to [[color]] function.
To create color from individual channel values use [[color]] function. To create gray for given intensity call [[gray]].
By default color is treated as `RGB` with values from ranges `[0.0-255.0]` inclusive.
All funtions internally convert any color representation to `Vec4` type using [[to-color]] function..
[Coloured list of all names](../static/colors.html)
## Color/ channel manipulations
You can access individual channels by calling on of the following:
* [[red]] or [[ch0]] - to get first channel value.
* [[green]] or [[ch1]] - to get second channel value.
* [[blue]] or [[ch2]] - to get third channel value.
* [[alpha]] - to get alpha value.
* [[luma]] - to get luma or brightness (range from `0` (black) to `255` (white)).
* [[hue]] - to get hue value in degrees (range from 0 to 360). Hexagon projection.
* [[hue-polar]] - to get hue from polar transformation.
[[set-ch0]], [[set-ch1]], [[set-ch2]] and [[set-alpha]] return new color with respective channel set to new value.
General [[set-channel]] and [[get-channel]] can work with any colorspace.
To make color darker/brighter use [[darken]] / [[lighten]] functions. Operations are done in `Lab` color space.
To change saturation call [[saturate]] / [[desaturate]]. Operations are done in `LCH` color space.
You can also [[modulate]] color (ie. multiply by given value).
## Conversions
Color can be converted from RGB to other color space (and back). List of color spaces are listed under [[colorspaces-list]] variable. There are two types of conversions:
* raw - with names `to-XXX` and `from-XXX` where `XXX` is color space name. Every color space has it's own value range for each channel. `(comp from-XXX to-XXX)` acts almost as identity.
* normalized - with names `to-XXX*` and `from-XXX*` where `XXX` is color space name. `to-XXX*` returns values normalized to `[0-255]` range. `from-XXX*` expects also channel values in range `[0-255]`.
NOTE: there is no information which color space is used. It's just a matter of your interpretation.
NOTE 2: be aware that converting function do not clamp any values to/from expected range.
Color space conversion functions are collected in two maps [[colorspaces]] for raw and [[colorspaces*]] for normalized functions. Keys are color space names as `keyword` and values are vectors with `to-` fn as first and `from-` fn as second element.
## Palettes / gradients
### Links
List of all defined colors and palettes:
* [Named palettes](../static/palettes.html)
* [Colourlovers palettes](../static/colourlovers.html)
* [Gradients](../static/gradients.html)
### Palette
Palette is just sequence of colors.
There are plenty of them predefined or can be generated:
* [[palette]] to access palette by keyword (from presets), by number (from colourlovers). Use [[palette]] to resample palette or convert gradient to palette.
* [[paletton-palette]] function to generate palette of type: `:monochromatic`, `:triad`, `:tetrad` with complementary color for given hue and configuration. See also [Paletton](http://paletton.com) website for details.
Call [[palette]] without any parameters to get list of predefined gradients.
### Gradient
Gradient is continuous functions which accepts value from `[0-1]` range and returns color. Call [[gradient]] to create one.
Call [[gradient]] without any parameters to obtain list of predefined gradients.
Use [[gradient]] to convert any palette to gradient or access predefined gradients by keyword.
### Conversions
To convert palette to gradient call [[gradient]] function. You can set interpolation method and colorspace.
To convert gradient to palette call [[palette]] function.
Call [[palette]] to resample palette to other number of colors. Internally input palette is converted to gradient and sampled back.
Use [[lerp]], [[lerp+]], [[mix]], [[average]] to mix two colors in different ways.
## Distances
Several functions to calculate distance between colors (`euclidean`, `delta-xxx` etc.).
## References
* https://vis4.net/chromajs/
* https://github.com/thi-ng/color
* https://github.com/nschloe/colorio"
{:metadoc/categories {:ops "Color/channel operations"
:conv "Color conversions"
:gr "Gradients"
:pal "Colors, palettes"
:interp "Interpolation"
:dist "Distance"}}
(:require [clojure.xml :as xml]
[fastmath.core :as m]
[fastmath.random :as r]
[fastmath.vector :as v]
[fastmath.stats :as stat]
[fastmath.interpolation :as i]
[fastmath.clustering :as cl]
[fastmath.easings :as e]
[clojure2d.protocols :as pr]
[clojure.java.io :refer [input-stream resource]])
(:import [fastmath.vector Vec2 Vec3 Vec4]
[java.awt Color]
[clojure.lang APersistentVector ISeq]))
(set! *warn-on-reflection* true)
(set! *unchecked-math* :warn-on-boxed)
(m/use-primitive-operators)
(defonce ^:private meta-conv #{:conv})
(defonce ^:private meta-ops #{:ops})
;; load the stuff to delay
;;;;;;;;;
;; read
;;;;;;;;;
(defn- read-gz-edn
"Read gzipped edn to structure."
[n]
(-> (resource n)
(input-stream)
(java.util.zip.GZIPInputStream.)
(slurp)
(read-string)))
;; load bunch of palettes
(def ^:private palette-presets-delay
(delay (into {} (map (fn [[k v]]
[k (mapv pr/to-color v)]) (read-gz-edn "palette_presets.edn.gz")))))
;; load bunch of colourlovers palettes
(def ^:private colourlovers-palettes-delay
(delay (let [f (fn [xml-in] (map (fn [x] (map #((:content %) 0) (:content (first (filter #(= (:tag %) :colors) (:content ((:content xml-in) x))))))) (range 100))) ;; parser
all (->> (range 1 6) ;; five files
(map #(-> (str "cl" % ".xml.gz")
(resource)
(input-stream)
(java.util.zip.GZIPInputStream.)
(xml/parse)
(f)))
(apply concat))] ;; join them
(mapv (fn [x] (mapv pr/to-color x)) all))))
(def ^:private color-presets-delay
(delay (into {} (map (fn [[k v]]
[k (pr/to-color v)]) (read-gz-edn "color_presets.edn.gz")))))
(defn named-colors-list
"Return list of the named colors."
{:metadoc/categories #{:pal}}
[] (keys @color-presets-delay))
;; ## Clamping functions
;; First define some clamping functions
(defn- clamp255
"Constrain value `a` to 0-255 double.
Use to ensure that value is in RGB range.
Accepts and returns `double`.
See also [[lclamp255]], [[clamp]] and [[lclamp]]."
{:metadoc/categories meta-ops}
^double [^double a]
(m/constrain a 0 255))
(defn- lclamp255
"Constrain value `a` to 0-255 long (rounding if necessary).
Use to ensure that value is in RGB range.
See also [[clamp255]], [[clamp]] and [[lclamp]]."
{:metadoc/categories meta-ops}
^long [^double a]
(m/constrain (m/round a) 0 255))
(defn possible-color?
"Check if given argument can be considered as color.
Check is done by analyzing type of the argument.
See also [[valid-color?]]."
{:metadoc/categories #{:pal}}
[c]
(or (and (not (seqable? c))
(satisfies? pr/ColorProto c))
(and (seqable? c)
(let [v (first c)]
(and (number? v)
(< (long v) 0x01000000))))))
(defn possible-palette?
"Check if given argument can be considered as palette.
Check is done by analyzing type of the argument."
{:metadoc/categories #{:pal}}
[c]
(and (seqable? c)
(not (possible-color? c))
(not (fn? c))))
(defn valid-color?
"Check if given argument is valid color.
Check is done by trying to convert to color representation.
Returns color when valid.
See also [[possible-color?]]"
{:metadoc/categories #{:pal}}
[c]
(try
(pr/to-color c)
(catch Exception e false)))
;; ## Color representation
(defn to-color
"Convert any color representation to `Vec4` vector."
{:metadoc/categories meta-ops}
^Vec4 [c] (pr/to-color c))
(defn to-awt-color
"Convert any color representation to `java.awt.Color`."
{:metadoc/categories meta-ops}
^java.awt.Color [c] (pr/to-awt-color c))
(defn luma
"Returns luma"
{:metadoc/categories meta-ops}
^double [c] (pr/luma c))
(defn- luma-fn
"Local luma conversion function"
^double [^double r ^double g ^double b]
(+ (* 0.212671 r)
(* 0.715160 g)
(* 0.072169 b)))
(declare from-sRGB)
(defn relative-luma
"Returns relative luminance"
{:metadoc/categories meta-ops}
^double [c]
(let [^Vec4 c (from-sRGB c)]
(luma-fn (.x c) (.y c) (.z c))))
(defn red
"Returns red (first channel) value."
{:metadoc/categories meta-ops}
^double [c] (pr/red c))
(defn green
"Returns green (second channel) value."
{:metadoc/categories meta-ops}
^double [c] (pr/green c))
(defn blue
"Returns blue (third channel) value."
{:metadoc/categories meta-ops}
^double [c] (pr/blue c))
(defn alpha
"Returns alpha value."
{:metadoc/categories meta-ops}
^double [c] (pr/alpha c))
(defn ch0
"Returns first channel value. Same as [[red]]."
{:metadoc/categories meta-ops}
^double [c] (pr/red c))
(defn ch1
"Returns second channel value. Same as [[green]]."
{:metadoc/categories meta-ops}
^double [c] (pr/green c))
(defn ch2
"Returns third channel value. Same as [[blue]]."
{:metadoc/categories meta-ops}
^double [c] (pr/blue c))
(defn hue-polar
"Hue value of color (any representation). Returns angle (0-360).
Uses polar transformation. See also [[hue]]."
{:metadoc/categories meta-ops}
^double [c]
(let [^Vec4 c (pr/to-color c)
a (* 0.5 (- (+ (.x c) (.x c)) (.y c) (.z c)))
b (* 0.8660254037844386 (- (.y c) (.z c)))
h (m/degrees (m/atan2 b a))]
(if (neg? h) (+ 360.0 h) h)))
(declare to-HC)
(defn hue
"Hue value of color (any representation). Returns angle (0-360).
Uses hexagonal transformation. See also [[hue-polar]]."
{:metadoc/categories meta-ops}
^double [c]
(let [^Vec4 ret (to-HC (pr/to-color c))] (.x ret)))
(defn set-alpha
"Set alpha channel and return new color"
{:metadoc/categories meta-ops}
^Vec4 [c a]
(let [^Vec4 v (pr/to-color c)]
(Vec4. (.x v) (.y v) (.z v) a)))
(defn set-red
"Set red channel and return new color."
{:metadoc/categories meta-ops}
^Vec4 [c val]
(let [^Vec4 v (pr/to-color c)]
(Vec4. val (.y v) (.z v) (.w v))))
(defn set-green
"Set green channel and return new color."
{:metadoc/categories meta-ops}
^Vec4 [c val]
(let [^Vec4 v (pr/to-color c)]
(Vec4. (.x v) val (.z v) (.w v))))
(defn set-blue
"Set blue channel and return new color"
{:metadoc/categories meta-ops}
^Vec4 [c val]
(let [^Vec4 v (pr/to-color c)]
(Vec4. (.x v) (.y v) val (.w v))))
(defn set-ch0
"Set red channel and return new color."
{:metadoc/categories meta-ops}
^Vec4 [c val]
(let [^Vec4 v (pr/to-color c)]
(Vec4. val (.y v) (.z v) (.w v))))
(defn set-ch1
"Set green channel and return new color."
{:metadoc/categories meta-ops}
^Vec4 [c val]
(let [^Vec4 v (pr/to-color c)]
(Vec4. (.x v) val (.z v) (.w v))))
(defn set-ch2
"Set blue channel and return new color"
{:metadoc/categories meta-ops}
^Vec4 [c val]
(let [^Vec4 v (pr/to-color c)]
(Vec4. (.x v) (.y v) val (.w v))))
(defn set-awt-alpha
"Set alpha channel and return `Color` representation."
{:metadoc/categories meta-ops}
^Color [c a]
(let [^Color cc (pr/to-awt-color c)]
(Color. (.getRed cc)
(.getGreen cc)
(.getBlue cc)
(lclamp255 a))))
(defn awt-color
"Create java.awt.Color object.
See also [[color]], [[gray]]."
{:metadoc/categories meta-ops}
(^Color [c]
(pr/to-awt-color c))
(^Color [c a]
(set-awt-alpha c a))
(^Color [^double r ^double g ^double b]
(Color. (lclamp255 r)
(lclamp255 g)
(lclamp255 b)))
(^Color [^double r ^double g ^double b ^double a]
(Color. (lclamp255 r)
(lclamp255 g)
(lclamp255 b)
(lclamp255 a))))
(defn color
"Create Vec4 object as color representation.
Arity:
* 1 - convert to `Vec4` from any color. Same as [[to-color]]
* 2 - sets color alpha
* 3 - sets r,g,b with alpha 255
* 4 - sets r,g,b and alpha
See also [[gray]]. [[awt-color]], [[awt-gray]]."
{:metadoc/categories meta-ops}
(^Vec4 [c]
(pr/to-color c))
(^Vec4 [c a]
(set-alpha c a))
(^Vec4 [^double r ^double g ^double b]
(Vec4. (clamp255 r)
(clamp255 g)
(clamp255 b)
255.0))
(^Vec4 [^double r ^double g ^double b ^double a]
(Vec4. (clamp255 r)
(clamp255 g)
(clamp255 b)
(clamp255 a))))
(defn clamp
"Clamp all color channels to `[0-255]` range."
{:metadoc/categories meta-ops}
[c]
(v/fmap (pr/to-color c) clamp255))
(defn lclamp
"Clamp all color channels to `[0-255]` range. Round if necessary."
{:metadoc/categories meta-ops}
[c]
(v/fmap (pr/to-color c) lclamp255))
(defn gray
"Create grayscale color based on intensity `v`. Optional parameter alpha `a`.
See also [[color]]"
{:metadoc/categories meta-ops}
(^Vec4 [^double v] (color v v v))
(^Vec4 [^double v ^double a] (color v v v a)))
(defn awt-gray
"Create grayscale color based on intensity `v`. Optional parameter alpha `a`.
AWT version of [[gray]]. See also [[awt-color]]"
{:metadoc/categories meta-ops}
(^Color [^double v] (awt-color v v v))
(^Color [^double v ^double a] (awt-color v v v a)))
(defn- string->long
"Parse color string and convert to long"
^long [^String s]
(let [^String s (if (= (first s) \#) (subs s 1) s)]
(Long/parseLong (condp = (count s)
1 (str s s s s s s)
2 (str s s s)
3 (let [[r g b] s] (str r r g g b b))
8 (str (subs s 6) (subs s 0 6))
s) 16)))
(extend-protocol pr/ColorProto
Vec2
(to-color ^Vec4 [^Vec2 c]
(Vec4. (.x c) (.x c) (.x c) (.y c)))
(to-awt-color ^Color [^Vec2 c]
(let [v (lclamp255 (.x c))]
(Color. v v v (lclamp255 (.y c)))))
(luma ^double [^Vec2 c] (.x c))
(red ^double [^Vec2 c] (.x c))
(green ^double [^Vec2 c] (.x c))
(blue ^double [^Vec2 c] (.x c))
(alpha ^double [^Vec2 c] (.y c))
Vec3
(to-color ^Vec4 [^Vec3 c]
(Vec4. (.x c) (.y c) (.z c) 255))
(to-awt-color ^Color [^Vec3 c]
(Color. (lclamp255 (.x c))
(lclamp255 (.y c))
(lclamp255 (.z c))))
(luma ^double [^Vec3 c] (luma-fn (.x c) (.y c) (.z c)))
(red ^double [^Vec3 c] (.x c))
(green ^double [^Vec3 c] (.y c))
(blue ^double [^Vec3 c] (.z c))
(alpha ^double [_] 255.0)
Vec4
(to-color ^Vec4 [c] c)
(to-awt-color ^Color [^Vec4 c]
(Color. (lclamp255 (.x c))
(lclamp255 (.y c))
(lclamp255 (.z c))
(lclamp255 (.w c))))
(luma ^double [^Vec4 c] (luma-fn (.x c) (.y c) (.z c)))
(red ^double [^Vec4 c] (.x c))
(green ^double [^Vec4 c] (.y c))
(blue ^double [^Vec4 c] (.z c))
(alpha ^double [^Vec4 c] (.w c))
clojure.lang.Keyword
(to-color ^Vec4 [n] (@color-presets-delay n))
(to-awt-color ^Color [n] (to-awt-color (@color-presets-delay n)))
(luma ^double [n] (pr/luma (@color-presets-delay n)))
(red ^double [n] (pr/red (@color-presets-delay n)))
(green ^double [n] (pr/green (@color-presets-delay n)))
(blue ^double [n] (pr/blue (@color-presets-delay n)))
(alpha ^double [n] (pr/alpha (@color-presets-delay n)))
Color
(to-color ^Vec4 [^Color c]
(Vec4. (.getRed c)
(.getGreen c)
(.getBlue c)
(.getAlpha c)))
(to-awt-color ^Color [c] c)
(luma ^double [^Color c] (luma-fn (.getRed c) (.getGreen c) (.getBlue c)))
(red ^double [^Color c] (.getRed c))
(green ^double [^Color c] (.getGreen c))
(blue ^double [^Color c] (.getBlue c))
(alpha ^double [^Color c] (.getAlpha c))
nil
(to-color [_] nil)
(to-awt-color [_] nil)
(alpha [_] nil)
(red [_] nil)
(green [_] nil)
(blue [_] nil)
Long
(alpha ^double [^long c] (bit-and 0xff (>> c 24)))
(red ^double [^long c] (bit-and 0xff (>> c 16)))
(green ^double [^long c] (bit-and 0xff (>> c 8)))
(blue ^double [^long c] (bit-and 0xff c))
(to-color ^Vec4 [^long c] (Vec4. (bit-and 0xff (>> c 16))
(bit-and 0xff (>> c 8))
(bit-and 0xff c)
(if (zero? (bit-and 0xff000000 c)) 255.0 (bit-and 0xff (>> c 24)))))
(to-awt-color ^Color [c] (pr/to-awt-color (pr/to-color c)))
(luma ^double [c] (pr/luma (pr/to-color c)))
Integer
(alpha ^double [c] (bit-and 0xff (>> ^int c 24)))
(red ^double [c] (bit-and 0xff (>> ^int c 16)))
(green ^double [c] (bit-and 0xff (>> ^int c 8)))
(blue ^double [c] (bit-and 0xff ^int c))
(to-color ^Vec4 [c] (Vec4. (bit-and 0xff (>> ^int c 16))
(bit-and 0xff (>> ^int c 8))
(bit-and 0xff ^int c)
(if (zero? (bit-and 0xff000000 c)) 255.0 (bit-and 0xff (>> ^int c 24)))))
(to-awt-color ^Color [c] (pr/to-awt-color (pr/to-color c)))
(luma ^double [c] (pr/luma (pr/to-color c)))
String
(alpha ^double [^String c] (pr/alpha (string->long c)))
(red ^double [^String c] (pr/red (string->long c)))
(green ^double [^String c] (pr/green (string->long c)))
(blue ^double [^String c] (pr/blue (string->long c)))
(to-color ^Vec4 [^String c] (pr/to-color (string->long c)))
(to-awt-color ^Color [^String c] (pr/to-awt-color (pr/to-color (string->long c))))
(luma ^double [^String c] (pr/luma (pr/to-color (string->long c))))
APersistentVector
(to-color ^Vec4 [c] (case (count c)
0 (Vec4. 0.0 0.0 0.0 255.0)
1 (gray (c 0))
2 (gray (c 0) (c 1))
3 (Vec4. (c 0) (c 1) (c 2) 255.0)
(Vec4. (c 0) (c 1) (c 2) (c 3))))
(alpha ^double [c] (get c 3 255.0))
(red ^double [c] (c 0))
(green ^double [c] (c 1))
(blue ^double [c] (c 2))
(to-awt-color ^Color [c] (pr/to-awt-color (pr/to-color c)))
(luma ^double [c] (pr/luma (pr/to-color c)))
ISeq
(to-color ^Vec4 [c] (case (count c)
0 (Vec4. 0.0 0.0 0.0 255.0)
1 (gray (first c))
2 (gray (first c) (second c))
3 (Vec4. (first c) (second c) (nth c 2) 255.0)
(Vec4. (first c) (second c) (nth c 2) (nth c 3 255.0))))
(alpha ^double [c] (nth c 3 255.0))
(red ^double [c] (first c))
(green ^double [c] (second c))
(blue ^double [c] (nth c 2))
(to-awt-color ^Color [c] (pr/to-awt-color (pr/to-color c)))
(luma [c] ^double (pr/luma (pr/to-color c))))
;;
(defn format-hex
"Convert color to hex string (css).
When alpha is lower than 255.0, #rgba is returned."
{:metadoc/categories meta-ops}
^String [c]
(let [^Vec4 c (pr/to-color c)
s (str "#" (format "%02x" (lclamp255 (.x c)))
(format "%02x" (lclamp255 (.y c)))
(format "%02x" (lclamp255 (.z c))))
a (lclamp255 (.w c))]
(if (< a 255) (str s (format "%02x" a)) s)))
(defn pack
"Pack color to ARGB 32bit integer."
{:metadoc/categories meta-ops}
^long [c]
(unchecked-long (bit-or (<< (lclamp255 (pr/alpha c)) 24)
(<< (lclamp255 (pr/red c)) 16)
(<< (lclamp255 (pr/green c)) 8)
(lclamp255 (pr/blue c)))))
;; ## Colorspace functions
;;
;; Conversion from RGB to specific color space always converts to range 0-255
;; Reverse conversion is not normalized and can exceed 0-255 range
(defn- test-colors
"to remove, check ranges"
[f]
(loop [cc (int 0)
mnr (double Integer/MAX_VALUE)
mxr (double Integer/MIN_VALUE)
mng (double Integer/MAX_VALUE)
mxg (double Integer/MIN_VALUE)
mnb (double Integer/MAX_VALUE)
mxb (double Integer/MIN_VALUE)]
(let [r (bit-and 0xff (bit-shift-right cc 16))
g (bit-and 0xff (bit-shift-right cc 8))
b (bit-and 0xff cc)
^Vec4 res (f (Vec4. r g b 255.0))
nmnr (if (< (.x res) mnr) (.x res) mnr)
nmxr (if (> (.x res) mxr) (.x res) mxr)
nmng (if (< (.y res) mng) (.y res) mng)
nmxg (if (> (.y res) mxg) (.y res) mxg)
nmnb (if (< (.z res) mnb) (.z res) mnb)
nmxb (if (> (.z res) mxb) (.z res) mxb)]
(if (< cc 0x1000000)
(recur (inc cc) (double nmnr) (double nmxr) (double nmng) (double nmxg) (double nmnb) (double nmxb))
{:min-r nmnr :max-r nmxr :min-g nmng :max-g nmxg :min-b nmnb :max-b nmxb}))))
;; ### CMY
(defn to-CMY
"RGB -> CMY"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)]
(Vec4. (- 255.0 (.x c))
(- 255.0 (.y c))
(- 255.0 (.z c))
(.w c))))
(def ^{:doc "CMY -> RGB" :metadoc/categories meta-conv} from-CMY to-CMY)
(def ^{:doc "CMY -> RGB, alias for [[to-CMY]]" :metadoc/categories meta-conv} to-CMY* to-CMY)
(def ^{:doc "CMY -> RGB, alias for [[from-CMY]]" :metadoc/categories meta-conv} from-CMY* to-CMY)
;; ### OHTA
(defn to-OHTA
"RGB -> OHTA
Returned ranges:
* I1: 0.0 - 255.0
* I2: -127.5 - 127.5
* I3: -127.5 - 127.5"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)
i1 (/ (+ (.x c) (.y c) (.z c)) 3.0)
i2 (* 0.5 (- (.x c) (.z c)))
i3 (* 0.25 (- (* 2.0 (.y c)) (.x c) (.z c)))]
(Vec4. i1 i2 i3 (.w c))))
(def ^:private ^:const ohta-s (Vec4. 0.0 127.5 127.5 0.0))
(defn to-OHTA*
"RGB -> OHTA, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(v/add (to-OHTA c) ohta-s))
(def ^:private ^:const ^double c23- (/ -2.0 3.0))
(def ^:private ^:const ^double c43 (/ 4.0 3.0))
(defn from-OHTA
"OHTA -> RGB
For ranges, see [[to-OHTA]]."
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)
i1 (.x c)
i2 (.y c)
i3 (.z c)
r (+ i1 i2 (* c23- i3))
g (+ i1 (* c43 i3))
b (+ i1 (- i2) (* c23- i3))]
(Vec4. r g b (.w c))))
(defn from-OHTA*
"OHTA -> RGB, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(from-OHTA (v/sub c ohta-s)))
;; ### sRGB
(def ^:private ^:const ^double gamma-factor (/ 2.4))
(defn to-linear
"Gamma correction (gamma=2.4), darken"
{:metadoc/categories meta-conv}
^double [^double v]
(if (> v 0.04045)
(m/pow (/ (+ 0.055 v) 1.055) 2.4)
(/ v 12.92)))
(defn from-linear
"Gamma correction (gamma=1/2.4), lighten"
{:metadoc/categories meta-conv}
^double [^double v]
(if (> v 0.0031308)
(- (* 1.055 (m/pow v gamma-factor)) 0.055)
(* v 12.92)))
(defn to-sRGB
"RGB -> sRGB"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)]
(v/vec4 (-> (Vec3. (.x c) (.y c) (.z c))
(v/div 255.0)
(v/fmap from-linear)
(v/mult 255.0))
(.w c))))
(defn from-sRGB
"sRGB -> RGB"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)]
(v/vec4 (-> (Vec3. (.x c) (.y c) (.z c))
(v/div 255.0)
(v/fmap to-linear)
(v/mult 255.0))
(.w c))))
(def ^{:doc "RGB -> sRGB" :metadoc/categories meta-conv} to-sRGB* to-sRGB)
(def ^{:doc "sRGB -> RGB" :metadoc/categories meta-conv}from-sRGB* from-sRGB)
;; ### XYZ
(def ^:private ^:const ^double D65X 95.047)
(def ^:private ^:const ^double D65Y 100.0)
(def ^:private ^:const ^double D65Z 108.883)
(defn- to-XYZ-
"Pure RGB->XYZ conversion without corrections."
^Vec3 [^Vec3 c]
(Vec3. (+ (* (.x c) 0.4124) (* (.y c) 0.3576) (* (.z c) 0.1805))
(+ (* (.x c) 0.2126) (* (.y c) 0.7152) (* (.z c) 0.0722))
(+ (* (.x c) 0.0193) (* (.y c) 0.1192) (* (.z c) 0.9505))))
(defn to-XYZ
"RGB -> XYZ
Returned ranges (D65):
* X: 0.0 - 95.047
* Y: 0.0 - 100.0
* Z: 0.0 - 108.883"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)
xyz-raw (to-XYZ- (-> (Vec3. (.x c) (.y c) (.z c))
(v/div 255.0)
(v/fmap to-linear)
(v/mult 100.0)))]
(v/vec4 xyz-raw (.w c))))
(defn to-XYZ*
"RGB -> XYZ, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [cc (to-XYZ c)]
(Vec4. (m/mnorm (.x cc) 0.0 D65X 0.0 255.0)
(m/mnorm (.y cc) 0.0 D65Y 0.0 255.0)
(m/mnorm (.z cc) 0.0 D65Z 0.0 255.0)
(.w cc))))
(defn- from-XYZ-
"Pure XYZ->RGB conversion."
^Vec3 [^Vec3 v]
(Vec3. (+ (* (.x v) 3.2406) (* (.y v) -1.5372) (* (.z v) -0.4986))
(+ (* (.x v) -0.9689) (* (.y v) 1.8758) (* (.z v) 0.0415))
(+ (* (.x v) 0.0557) (* (.y v) -0.2040) (* (.z v) 1.0570))))
(defn from-XYZ
"XYZ -> RGB
For ranges, see [[to-XYZ]]"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)
rgb-raw (v/mult (v/fmap (from-XYZ- (v/div (Vec3. (.x c) (.y c) (.z c)) 100.0)) from-linear) 255.0)]
(v/vec4 rgb-raw (.w c))))
(defn from-XYZ*
"XYZ -> RGB, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)
x (m/mnorm (.x c) 0.0 255.0 0.0 D65X)
y (m/mnorm (.y c) 0.0 255.0 0.0 D65Y)
z (m/mnorm (.z c) 0.0 255.0 0.0 D65Z)]
(from-XYZ (Vec4. x y z (.w c)))))
;;
(def ^:private ^:const ^double CIEEpsilon (/ 216.0 24389.0))
(def ^:private ^:const ^double CIEK (/ 24389.0 27.0))
(def ^:private ^:const ^double REF-U (/ (* 4.0 D65X) (+ D65X (* 15.0 D65Y) (* 3.0 D65Z))))
(def ^:private ^:const ^double REF-V (/ (* 9.0 D65Y) (+ D65X (* 15.0 D65Y) (* 3.0 D65Z))))
;; ### LAB
(defn- to-lab-correct
"LAB correction"
^double [^double v]
(if (> v CIEEpsilon)
(m/cbrt v)
(/ (+ 16.0 (* v CIEK)) 116.0)))
(defn to-LAB
"RGB -> LAB
Returned ranges:
* L: 0.0 - 100.0
* a: -86.18 - 98.25
* b: -107.86 - 94.48"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [xyz (to-XYZ c)
x (to-lab-correct (/ (.x xyz) D65X))
y (to-lab-correct (/ (.y xyz) D65Y))
z (to-lab-correct (/ (.z xyz) D65Z))
L (- (* y 116.0) 16.0)
a (* 500.0 (- x y))
b (* 200.0 (- y z))]
(Vec4. L a b (.w xyz))))
(defn to-LAB*
"RGB -> LAB, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [cc (to-LAB c)]
(Vec4. (m/mnorm (.x cc) 0.0 100.0 0.0 255.0)
(m/mnorm (.y cc) -86.18463649762525 98.25421868616108 0.0 255.0)
(m/mnorm (.z cc) -107.86368104495168 94.48248544644461 0.0 255.0)
(.w cc))))
(defn- from-lab-correct
"LAB correction"
^double [^double v]
(let [v3 (* v v v)]
(if (> v3 CIEEpsilon)
v3
(/ (- (* 116.0 v) 16.0) CIEK))))
(defn from-LAB
"LAB -> RGB,
For ranges, see [[to-LAB]]"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)
y (/ (+ (.x c) 16.0) 116.0)
x (* D65X (from-lab-correct (+ y (/ (.y c) 500.0))))
z (* D65Z (from-lab-correct (- y (/ (.z c) 200.0))))]
(from-XYZ (Vec4. x (* D65Y (from-lab-correct y)) z (.w c)))))
(defn from-LAB*
"LAB -> RGB, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)]
(from-LAB (Vec4. (m/mnorm (.x c) 0.0 255.0 0.0 100.0)
(m/mnorm (.y c) 0.0 255.0 -86.18463649762525 98.25421868616108)
(m/mnorm (.z c) 0.0 255.0 -107.86368104495168 94.48248544644461)
(.w c)))))
;;
(defn to-LUV
"RGB -> LUV
Returned ranges:
* L: 0.0 - 100.0
* u: -83.08 - 175.05
* v: -134.12 - 107.40"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [cc (to-XYZ c)
uv-factor (+ (.x cc) (* 15.0 (.y cc)) (* 3.0 (.z cc)))]
(if (zero? uv-factor)
(Vec4. 0.0 0.0 0.0 (.w cc))
(let [uv-factor* (/ uv-factor)
var-u (* 4.0 (.x cc) uv-factor*)
var-v (* 9.0 (.y cc) uv-factor*)
var-y (to-lab-correct (/ (.y cc) 100.0))
L (- (* 116.0 var-y) 16.0)]
(Vec4. L
(* 13.0 L (- var-u REF-U))
(* 13.0 L (- var-v REF-V))
(.w cc))))))
(defn to-LUV*
"RGB -> LUV, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [cc (to-LUV c)]
(Vec4. (m/mnorm (.x cc) 0.0 100.0 0.0 255.0)
(m/mnorm (.y cc) -83.07975193131836 175.05303573649485 0.0 255.0)
(m/mnorm (.z cc) -134.1160763907768 107.40136474095397 0.0 255.0)
(.w cc))))
(defn from-LUV
"LUV -> RGB
For ranges, see [[to-LUV]]"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)]
(if (zero? (.x c))
(Vec4. 0.0 0.0 0.0 (.w c))
(let [var-y (from-lab-correct (/ (+ (.x c) 16.0) 116.0))
var-u (+ REF-U (/ (.y c) (* 13.0 (.x c))))
var-v (+ REF-V (/ (.z c) (* 13.0 (.x c))))
Y (* 100.0 var-y)
X (/ (* -9.0 Y var-u) (- (* (- var-u 4.0) var-v) (* var-u var-v)))]
(from-XYZ (Vec4. X
Y
(/ (- (* 9.0 Y) (* 15.0 var-v Y) (* var-v X)) (* 3.0 var-v))
(.w c)))))))
(defn from-LUV*
"LUV -> RGB, normalized"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [^Vec4 c (pr/to-color c)]
(from-LUV (Vec4. (m/mnorm (.x c) 0.0 255.0 0.0 100.0)
(m/mnorm (.y c) 0.0 255.0 -83.07975193131836 175.05303573649485)
(m/mnorm (.z c) 0.0 255.0 -134.1160763907768 107.40136474095397)
(.w c)))))
;; HLab
(def ^:private ^:const ^double Ka (* (/ 175.0 198.04) (+ D65X D65Y)))
(def ^:private ^:const ^double Kb (* (/ 70.0 218.11) (+ D65Y D65Z)))
(defn to-HunterLAB
"RGB -> HunterLAB
Returned ranges:
* L: 0.0 - 100.0
* a: -69.08 - 109.48
* b: -199.78 - 55.72"
{:metadoc/categories meta-conv}
^Vec4 [c]
(let [cc (to-XYZ c)
X (/ (.x cc) D65X)
Y (/ (.y cc) D65Y)
Z (/ (.z cc) D65Z)]
(if (zero? Y)
(Vec4. 0.0 0.0 0.0 (.w cc))
(let [sqrtY (m/sqrt Y)]
(Vec4. (* 100.0 sqrtY)
(* Ka (/ (- X Y) sqrtY))
(* Kb (/ (- Y Z) sqrtY))
(.w cc))))))