-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor2css.php
1971 lines (1613 loc) · 58.7 KB
/
color2css.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
<?php
namespace xenocrat;
class color2css {
const VERSION_MAJOR = 6;
const VERSION_MINOR = 2;
const VERSION_PATCH = 0;
const CSS_COLOR_TRANSPARENT = "#00000000";
const CSS_COLOR_BLACK = "#000000ff";
const CSS_COLOR_SILVER = "#c0c0c0ff";
const CSS_COLOR_GRAY = "#808080ff";
const CSS_COLOR_WHITE = "#ffffffff";
const CSS_COLOR_MAROON = "#800000ff";
const CSS_COLOR_RED = "#ff0000ff";
const CSS_COLOR_PURPLE = "#800080ff";
const CSS_COLOR_FUCHSIA = "#ff00ffff";
const CSS_COLOR_GREEN = "#008000ff";
const CSS_COLOR_LIME = "#00ff00ff";
const CSS_COLOR_OLIVE = "#808000ff";
const CSS_COLOR_YELLOW = "#ffff00ff";
const CSS_COLOR_NAVY = "#000080ff";
const CSS_COLOR_BLUE = "#0000ffff";
const CSS_COLOR_TEAL = "#008080ff";
const CSS_COLOR_AQUA = "#00ffffff";
const CSS_COLOR_ORANGE = "#ffa500ff";
const CSS_COLOR_ALICEBLUE = "#f0f8ffff";
const CSS_COLOR_ANTIQUEWHITE = "#faebd7ff";
const CSS_COLOR_AQUAMARINE = "#7fffd4ff";
const CSS_COLOR_AZURE = "#f0ffffff";
const CSS_COLOR_BEIGE = "#f5f5dcff";
const CSS_COLOR_BISQUE = "#ffe4c4ff";
const CSS_COLOR_BLANCHEDALMOND = "#ffebcdff";
const CSS_COLOR_BLUEVIOLET = "#8a2be2ff";
const CSS_COLOR_BROWN = "#a52a2aff";
const CSS_COLOR_BURLYWOOD = "#deb887ff";
const CSS_COLOR_CADETBLUE = "#5f9ea0ff";
const CSS_COLOR_CHARTREUSE = "#7fff00ff";
const CSS_COLOR_CHOCOLATE = "#d2691eff";
const CSS_COLOR_CORAL = "#ff7f50ff";
const CSS_COLOR_CORNFLOWERBLUE = "#6495edff";
const CSS_COLOR_CORNSILK = "#fff8dcff";
const CSS_COLOR_CRIMSON = "#dc143cff";
const CSS_COLOR_CYAN = "#00ffffff";
const CSS_COLOR_DARKBLUE = "#00008bff";
const CSS_COLOR_DARKCYAN = "#008b8bff";
const CSS_COLOR_DARKGOLDENROD = "#b8860bff";
const CSS_COLOR_DARKGRAY = "#a9a9a9ff";
const CSS_COLOR_DARKGREEN = "#006400ff";
const CSS_COLOR_DARKGREY = "#a9a9a9ff";
const CSS_COLOR_DARKKHAKI = "#bdb76bff";
const CSS_COLOR_DARKMAGENTA = "#8b008bff";
const CSS_COLOR_DARKOLIVEGREEN = "#556b2fff";
const CSS_COLOR_DARKORANGE = "#ff8c00ff";
const CSS_COLOR_DARKORCHID = "#9932ccff";
const CSS_COLOR_DARKRED = "#8b0000ff";
const CSS_COLOR_DARKSALMON = "#e9967aff";
const CSS_COLOR_DARKSEAGREEN = "#8fbc8fff";
const CSS_COLOR_DARKSLATEBLUE = "#483d8bff";
const CSS_COLOR_DARKSLATEGRAY = "#2f4f4fff";
const CSS_COLOR_DARKSLATEGREY = "#2f4f4fff";
const CSS_COLOR_DARKTURQUOISE = "#00ced1ff";
const CSS_COLOR_DARKVIOLET = "#9400d3ff";
const CSS_COLOR_DEEPPINK = "#ff1493ff";
const CSS_COLOR_DEEPSKYBLUE = "#00bfffff";
const CSS_COLOR_DIMGRAY = "#696969ff";
const CSS_COLOR_DIMGREY = "#696969ff";
const CSS_COLOR_DODGERBLUE = "#1e90ffff";
const CSS_COLOR_FIREBRICK = "#b22222ff";
const CSS_COLOR_FLORALWHITE = "#fffaf0ff";
const CSS_COLOR_FORESTGREEN = "#228b22ff";
const CSS_COLOR_GAINSBORO = "#dcdcdcff";
const CSS_COLOR_GHOSTWHITE = "#f8f8ffff";
const CSS_COLOR_GOLD = "#ffd700ff";
const CSS_COLOR_GOLDENROD = "#daa520ff";
const CSS_COLOR_GREENYELLOW = "#adff2fff";
const CSS_COLOR_GREY = "#808080ff";
const CSS_COLOR_HONEYDEW = "#f0fff0ff";
const CSS_COLOR_HOTPINK = "#ff69b4ff";
const CSS_COLOR_INDIANRED = "#cd5c5cff";
const CSS_COLOR_INDIGO = "#4b0082ff";
const CSS_COLOR_IVORY = "#fffff0ff";
const CSS_COLOR_KHAKI = "#f0e68cff";
const CSS_COLOR_LAVENDER = "#e6e6faff";
const CSS_COLOR_LAVENDERBLUSH = "#fff0f5ff";
const CSS_COLOR_LAWNGREEN = "#7cfc00ff";
const CSS_COLOR_LEMONCHIFFON = "#fffacdff";
const CSS_COLOR_LIGHTBLUE = "#add8e6ff";
const CSS_COLOR_LIGHTCORAL = "#f08080ff";
const CSS_COLOR_LIGHTCYAN = "#e0ffffff";
const CSS_COLOR_LIGHTGOLDENRODYELLOW = "#fafad2ff";
const CSS_COLOR_LIGHTGRAY = "#d3d3d3ff";
const CSS_COLOR_LIGHTGREEN = "#90ee90ff";
const CSS_COLOR_LIGHTGREY = "#d3d3d3ff";
const CSS_COLOR_LIGHTPINK = "#ffb6c1ff";
const CSS_COLOR_LIGHTSALMON = "#ffa07aff";
const CSS_COLOR_LIGHTSEAGREEN = "#20b2aaff";
const CSS_COLOR_LIGHTSKYBLUE = "#87cefaff";
const CSS_COLOR_LIGHTSLATEGRAY = "#778899ff";
const CSS_COLOR_LIGHTSLATEGREY = "#778899ff";
const CSS_COLOR_LIGHTSTEELBLUE = "#b0c4deff";
const CSS_COLOR_LIGHTYELLOW = "#ffffe0ff";
const CSS_COLOR_LIMEGREEN = "#32cd32ff";
const CSS_COLOR_LINEN = "#faf0e6ff";
const CSS_COLOR_MAGENTA = "#ff00ffff";
const CSS_COLOR_MEDIUMAQUAMARINE = "#66cdaaff";
const CSS_COLOR_MEDIUMBLUE = "#0000cdff";
const CSS_COLOR_MEDIUMORCHID = "#ba55d3ff";
const CSS_COLOR_MEDIUMPURPLE = "#9370dbff";
const CSS_COLOR_MEDIUMSEAGREEN = "#3cb371ff";
const CSS_COLOR_MEDIUMSLATEBLUE = "#7b68eeff";
const CSS_COLOR_MEDIUMSPRINGGREEN = "#00fa9aff";
const CSS_COLOR_MEDIUMTURQUOISE = "#48d1ccff";
const CSS_COLOR_MEDIUMVIOLETRED = "#c71585ff";
const CSS_COLOR_MIDNIGHTBLUE = "#191970ff";
const CSS_COLOR_MINTCREAM = "#f5fffaff";
const CSS_COLOR_MISTYROSE = "#ffe4e1ff";
const CSS_COLOR_MOCCASIN = "#ffe4b5ff";
const CSS_COLOR_NAVAJOWHITE = "#ffdeadff";
const CSS_COLOR_OLDLACE = "#fdf5e6ff";
const CSS_COLOR_OLIVEDRAB = "#6b8e23ff";
const CSS_COLOR_ORANGERED = "#ff4500ff";
const CSS_COLOR_ORCHID = "#da70d6ff";
const CSS_COLOR_PALEGOLDENROD = "#eee8aaff";
const CSS_COLOR_PALEGREEN = "#98fb98ff";
const CSS_COLOR_PALETURQUOISE = "#afeeeeff";
const CSS_COLOR_PALEVIOLETRED = "#db7093ff";
const CSS_COLOR_PAPAYAWHIP = "#ffefd5ff";
const CSS_COLOR_PEACHPUFF = "#ffdab9ff";
const CSS_COLOR_PERU = "#cd853fff";
const CSS_COLOR_PINK = "#ffc0cbff";
const CSS_COLOR_PLUM = "#dda0ddff";
const CSS_COLOR_POWDERBLUE = "#b0e0e6ff";
const CSS_COLOR_ROSYBROWN = "#bc8f8fff";
const CSS_COLOR_ROYALBLUE = "#4169e1ff";
const CSS_COLOR_SADDLEBROWN = "#8b4513ff";
const CSS_COLOR_SALMON = "#fa8072ff";
const CSS_COLOR_SANDYBROWN = "#f4a460ff";
const CSS_COLOR_SEAGREEN = "#2e8b57ff";
const CSS_COLOR_SEASHELL = "#fff5eeff";
const CSS_COLOR_SIENNA = "#a0522dff";
const CSS_COLOR_SKYBLUE = "#87ceebff";
const CSS_COLOR_SLATEBLUE = "#6a5acdff";
const CSS_COLOR_SLATEGRAY = "#708090ff";
const CSS_COLOR_SLATEGREY = "#708090ff";
const CSS_COLOR_SNOW = "#fffafaff";
const CSS_COLOR_SPRINGGREEN = "#00ff7fff";
const CSS_COLOR_STEELBLUE = "#4682b4ff";
const CSS_COLOR_TAN = "#d2b48cff";
const CSS_COLOR_THISTLE = "#d8bfd8ff";
const CSS_COLOR_TOMATO = "#ff6347ff";
const CSS_COLOR_TURQUOISE = "#40e0d0ff";
const CSS_COLOR_VIOLET = "#ee82eeff";
const CSS_COLOR_WHEAT = "#f5deb3ff";
const CSS_COLOR_WHITESMOKE = "#f5f5f5ff";
const CSS_COLOR_YELLOWGREEN = "#9acd32ff";
const CSS_COLOR_REBECCAPURPLE = "#663399ff";
const REGEX_NUM = "[+-]?[0-9]*\.?[0-9]+";
const REGEX_EXP = "[+-]?[0-9]*\.?[0-9]+[eE][0-9]+";
const REGEX_HEX = "[0-9a-fA-F]";
const REGEX_ANGLE = "(deg|grad|rad|turn)?";
const REGEX_NUM_HUE = self::REGEX_NUM.self::REGEX_ANGLE;
const REGEX_EXP_HUE = self::REGEX_EXP.self::REGEX_ANGLE;
const REGEX_RGB =
"/^rgba?\( *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
" *\)$/";
const REGEX_RGB_LEGACY =
"/^rgba?\( *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?) *, *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?) *, *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?)".
"( *[,\/] *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?))?".
" *\)$/";
const REGEX_HSL =
"/^hsla?\( *".
"(".self::REGEX_NUM_HUE."|".self::REGEX_EXP_HUE."|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
" *\)$/";
const REGEX_HSL_LEGACY =
"/^hsla?\( *".
"(".self::REGEX_NUM_HUE."|".self::REGEX_EXP_HUE.") *, *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?) *, *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?)".
"( *[,\/] *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?))?".
" *\)$/";
const REGEX_HWB =
"/^hwb\( *".
"(".self::REGEX_NUM_HUE."|".self::REGEX_EXP_HUE."|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
" *\)$/";
const REGEX_LAB =
"/^lab\( *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
" *\)$/";
const REGEX_LCH =
"/^lch\( *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM_HUE."|".self::REGEX_EXP_HUE."|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
"\)$/";
const REGEX_OKLAB =
"/^oklab\( *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
" *\)$/";
const REGEX_OKLCH =
"/^oklch\( *".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none) +".
"(".self::REGEX_NUM_HUE."|".self::REGEX_EXP_HUE."|none)".
"( *\/ *(".self::REGEX_NUM."%?|".self::REGEX_EXP."%?|none))?".
"\)$/";
const REGEX_HEX_SHORT =
"/^#".
"(".self::REGEX_HEX.")".
"(".self::REGEX_HEX.")".
"(".self::REGEX_HEX.")".
"(".self::REGEX_HEX.")?".
"$/";
const REGEX_HEX_LONG =
"/^#".
"(".self::REGEX_HEX."{2})".
"(".self::REGEX_HEX."{2})".
"(".self::REGEX_HEX."{2})".
"(".self::REGEX_HEX."{2})?".
"$/";
protected $rgb = array(
"r" => 0.0,
"g" => 0.0,
"b" => 0.0
);
protected $hsl = array(
"h" => 0.0,
"s" => 0.0,
"l" => 0.0
);
protected $hsv = array(
"h" => 0.0,
"s" => 0.0,
"v" => 0.0
);
protected $hwb = array(
"h" => 0.0,
"w" => 0.0,
"b" => 0.0
);
protected $lab = array(
"l" => 0.0,
"a" => 0.0,
"b" => 0.0
);
protected $lch = array(
"l" => 0.0,
"c" => 0.0,
"h" => 0.0
);
protected $oklab = array(
"l" => 0.0,
"a" => 0.0,
"b" => 0.0
);
protected $oklch = array(
"l" => 0.0,
"c" => 0.0,
"h" => 0.0
);
protected $alpha = 0.0;
public function __construct(
$color = "transparent"
) {
$args = func_get_args();
if (count($args) < 2) {
if ($color instanceof color2css)
return $this->take($color);
else
return $this->interpret($color);
}
if (count($args) < 3)
throw new \BadMethodCallException(
"Missing arguments in call to color constructor."
);
$this->red($args[0]);
$this->green($args[1]);
$this->blue($args[2]);
$this->alpha((count($args) == 4) ? $args[3] : 1.0);
}
public function list_keywords(
): array {
$keywords = array();
$reflect = new \ReflectionClass(get_class($this));
$constants = $reflect->getConstants();
foreach ($constants as $key => $val) {
if (preg_match("/^CSS_COLOR_/", $key))
$keywords[strtolower(substr($key, 10))] = $val;
}
return $keywords;
}
public function rgb(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_rgb($str);
$r = $this->number_format($this->rgb["r"] * 255);
$g = $this->number_format($this->rgb["g"] * 255);
$b = $this->number_format($this->rgb["b"] * 255);
$p = $this->number_format($this->alpha * 100, 0);
return "rgb(".$r." ".$g." ".$b." / ".$p."%)";
}
public function hsl(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_hsl($str);
$h = $this->number_format($this->hsl["h"]);
$s = $this->number_format($this->hsl["s"] * 100);
$l = $this->number_format($this->hsl["l"] * 100);
$p = $this->number_format($this->alpha * 100, 0);
return "hsl(".$h." ".$s."% ".$l."% / ".$p."%)";
}
public function hwb(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_hwb($str);
$h = $this->number_format($this->hwb["h"]);
$w = $this->number_format($this->hwb["w"] * 100);
$b = $this->number_format($this->hwb["b"] * 100);
$p = $this->number_format($this->alpha * 100, 0);
return "hwb(".$h." ".$w."% ".$b."% / ".$p."%)";
}
public function lab(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_lab($str);
$l = $this->number_format($this->lab["l"]);
$a = $this->number_format($this->lab["a"]);
$b = $this->number_format($this->lab["b"]);
$p = $this->number_format($this->alpha * 100, 0);
return "lab(".$l." ".$a." ".$b." / ".$p."%)";
}
public function lch(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_lch($str);
$l = $this->number_format($this->lch["l"]);
$c = $this->number_format($this->lch["c"]);
$h = $this->number_format($this->lch["h"]);
$p = $this->number_format($this->alpha * 100, 0);
return "lch(".$l." ".$c." ".$h." / ".$p."%)";
}
public function oklab(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_oklab($str);
$l = $this->number_format($this->oklab["l"], 3);
$a = $this->number_format($this->oklab["a"], 3);
$b = $this->number_format($this->oklab["b"], 3);
$p = $this->number_format($this->alpha * 100, 0);
return "oklab(".$l." ".$a." ".$b." / ".$p."%)";
}
public function oklch(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_oklch($str);
$l = $this->number_format($this->oklch["l"], 3);
$c = $this->number_format($this->oklch["c"], 3);
$h = $this->number_format($this->oklch["h"], 1);
$p = $this->number_format($this->alpha * 100, 0);
return "oklch(".$l." ".$c." ".$h." / ".$p."%)";
}
public function hex(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_hex($str);
$r = (int) round($this->rgb["r"] * 255);
$g = (int) round($this->rgb["g"] * 255);
$b = (int) round($this->rgb["b"] * 255);
$r = str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$g = str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$b = str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
return "#".$r.$g.$b;
}
public function hexa(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_hex($str);
$r = (int) round($this->rgb["r"] * 255);
$g = (int) round($this->rgb["g"] * 255);
$b = (int) round($this->rgb["b"] * 255);
$p = (int) round($this->alpha * 255);
$r = str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
$g = str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
$b = str_pad(dechex($b), 2, "0", STR_PAD_LEFT);
$p = str_pad(dechex($p), 2, "0", STR_PAD_LEFT);
return "#".$r.$g.$b.$p;
}
public function red(
$r = null
): float|bool {
if (!isset($r))
return $this->rgb["r"];
if (!is_numeric($r) or $r < 0 or $r > 1)
throw new \RangeException(
"RGB red value must be in the range 0-1."
);
$this->rgb["r"] = (float) $r;
$hsl = $this->rgb2hsl($this->rgb);
if (is_nan($hsl["h"]))
$hsl["h"] = $this->hsl["h"];
$hwb = $this->rgb2hwb($this->rgb);
if (is_nan($hwb["h"]))
$hwb["h"] = $this->hwb["h"];
$hsv = $this->rgb2hsv($this->rgb);
if (is_nan($hsv["h"]))
$hsv["h"] = $this->hsv["h"];
$this->hsl = $hsl;
$this->hwb = $hwb;
$this->hsv = $hsv;
return true;
}
public function green(
$g = null
): float|bool {
if (!isset($g))
return $this->rgb["g"];
if (!is_numeric($g) or $g < 0 or $g > 1)
throw new \RangeException(
"RGB green value must be in the range 0-1."
);
$this->rgb["g"] = (float) $g;
$hsl = $this->rgb2hsl($this->rgb);
if (is_nan($hsl["h"]))
$hsl["h"] = $this->hsl["h"];
$hwb = $this->rgb2hwb($this->rgb);
if (is_nan($hwb["h"]))
$hwb["h"] = $this->hwb["h"];
$hsv = $this->rgb2hsv($this->rgb);
if (is_nan($hsv["h"]))
$hsv["h"] = $this->hsv["h"];
$this->hsl = $hsl;
$this->hwb = $hwb;
$this->hsv = $hsv;
return true;
}
public function blue(
$b = null
): float|bool {
if (!isset($b))
return $this->rgb["b"];
if (!is_numeric($b) or $b < 0 or $b > 1)
throw new \RangeException(
"RGB blue value must be in the range 0-1."
);
$this->rgb["b"] = (float) $b;
$hsl = $this->rgb2hsl($this->rgb);
if (is_nan($hsl["h"]))
$hsl["h"] = $this->hsl["h"];
$hwb = $this->rgb2hwb($this->rgb);
if (is_nan($hwb["h"]))
$hwb["h"] = $this->hwb["h"];
$hsv = $this->rgb2hsv($this->rgb);
if (is_nan($hsv["h"]))
$hsv["h"] = $this->hsv["h"];
$this->hsl = $hsl;
$this->hwb = $hwb;
$this->hsv = $hsv;
return true;
}
public function hue(
$h = null
): float|bool {
if (!isset($h))
return $this->hsl["h"];
if (!is_numeric($h))
throw new \InvalidArgumentException(
"HSL hue value must be numeric."
);
if ($h > 360)
$h = $h % 360;
while ($h < 0)
$h = $h + 360;
$this->hsl["h"] = (float) $h;
$this->hsv["h"] = (float) $h;
$this->hwb["h"] = (float) $h;
$this->rgb = $this->hsl2rgb($this->hsl);
return true;
}
public function saturation(
$s = null
): float|bool {
if (!isset($s))
return $this->hsl["s"];
if (!is_numeric($s) or $s < 0 or $s > 1)
throw new \RangeException(
"HSL saturation value must be in the range 0-1."
);
$this->hsl["s"] = (float) $s;
$this->hsv = $this->hsl2hsv($this->hsl);
$this->hwb = $this->hsv2hwb($this->hsv);
$this->rgb = $this->hsl2rgb($this->hsl);
return true;
}
public function lightness(
$l = null
): float|bool {
if (!isset($l))
return $this->hsl["l"];
if (!is_numeric($l) or $l < 0 or $l > 1)
throw new \RangeException(
"HSL lightness value must be in the range 0-1."
);
$this->hsl["l"] = (float) $l;
$this->hsv = $this->hsl2hsv($this->hsl);
$this->hwb = $this->hsv2hwb($this->hsv);
$this->rgb = $this->hsl2rgb($this->hsl);
return true;
}
public function whiteness(
$w = null
): float|bool {
if (!isset($w))
return $this->hwb["w"];
if (!is_numeric($w) or $w < 0 or $w > 1)
throw new \RangeException(
"HWB whiteness value must be in the range 0-1."
);
$this->hwb["w"] = (float) $w;
$this->hsv = $this->hwb2hsv($this->hwb);
$this->hsl = $this->hsv2hsl($this->hsv);
$this->rgb = $this->hwb2rgb($this->hwb);
return true;
}
public function blackness(
$d = null
): float|bool {
if (!isset($d))
return $this->hwb["b"];
if (!is_numeric($d) or $d < 0 or $d > 1)
throw new \RangeException(
"HWB blackness value must be in the range 0-1."
);
$this->hwb["b"] = (float) $d;
$this->hsv = $this->hwb2hsv($this->hwb);
$this->hsl = $this->hsv2hsl($this->hsv);
$this->rgb = $this->hwb2rgb($this->hwb);
return true;
}
public function colorfulness(
$s = null
): float|bool {
if (!isset($s))
return $this->hsv["s"];
if (!is_numeric($s) or $s < 0 or $s > 1)
throw new \RangeException(
"HSV colorfulness value must be in the range 0-1."
);
$this->hsv["s"] = (float) $s;
$this->hwb = $this->hsv2hwb($this->hsv);
$this->hsl = $this->hsv2hsl($this->hsv);
$this->rgb = $this->hwb2rgb($this->hwb);
return true;
}
public function brightness(
$v = null
): float|bool {
if (!isset($v))
return $this->hsv["v"];
if (!is_numeric($v) or $v < 0 or $v > 1)
throw new \RangeException(
"HSV brightness value must be in the range 0-1."
);
$this->hsv["v"] = (float) $v;
$this->hwb = $this->hsv2hwb($this->hsv);
$this->hsl = $this->hsv2hsl($this->hsv);
$this->rgb = $this->hwb2rgb($this->hwb);
return true;
}
public function cie_l(
$l = null
): float|bool {
if (!isset($l))
return $this->lab["l"];
if (!is_numeric($l) or $l < 0 or $l > 100)
throw new \RangeException(
"CIE lightness value must be in the range 0-100."
);
$this->lab["l"] = (float) $l;
$lch = $this->lab2lch($this->lab);
if (is_nan($lch["h"]))
$lch["h"] = $this->lch["h"];
$this->lch = $lch;
return true;
}
public function cie_a(
$a = null
): float|bool {
if (!isset($a))
return $this->lab["a"];
if (!is_numeric($a))
throw new \InvalidArgumentException(
"CIE A axis value must be numeric."
);
$this->lab["a"] = (float) $a;
$lch = $this->lab2lch($this->lab);
if (is_nan($lch["h"]))
$lch["h"] = $this->lch["h"];
$this->lch = $lch;
return true;
}
public function cie_b(
$b = null
): float|bool {
if (!isset($b))
return $this->lab["b"];
if (!is_numeric($b))
throw new \InvalidArgumentException(
"CIE B axis value must be numeric."
);
$this->lab["b"] = (float) $b;
$lch = $this->lab2lch($this->lab);
if (is_nan($lch["h"]))
$lch["h"] = $this->lch["h"];
$this->lch = $lch;
return true;
}
public function cie_c(
$c = null
): float|bool {
if (!isset($c))
return $this->lch["c"];
if (!is_numeric($c) or $c < 0)
throw new \RangeException(
"CIE chroma value must be > 0."
);
$this->lch["c"] = (float) $c;
$this->lab = $this->lch2lab($this->lch);
return true;
}
public function cie_h(
$h = null
): float|bool {
if (!isset($h))
return $this->lch["h"];
if (!is_numeric($h))
throw new \InvalidArgumentException(
"CIE hue value must be numeric."
);
if ($h > 360)
$h = $h % 360;
while ($h < 0)
$h = $h + 360;
$this->lch["h"] = (float) $h;
$this->lab = $this->lch2lab($this->lch);
return true;
}
public function ok_l(
$l = null
): float|bool {
if (!isset($l))
return $this->oklab["l"];
if (!is_numeric($l) or $l < 0 or $l > 1)
throw new \RangeException(
"OK lightness value must be in the range 0-1."
);
$this->oklab["l"] = (float) $l;
$oklch = $this->oklab2oklch($this->oklab);
if (is_nan($oklch["h"]))
$oklch["h"] = $this->oklch["h"];
$this->oklch = $oklch;
return true;
}
public function ok_a(
$a = null
): float|bool {
if (!isset($a))
return $this->oklab["a"];
if (!is_numeric($a))
throw new \InvalidArgumentException(
"OK A axis value must be numeric."
);
$this->oklab["a"] = (float) $a;
$oklch = $this->oklab2oklch($this->oklab);
if (is_nan($oklch["h"]))
$oklch["h"] = $this->oklch["h"];
$this->oklch = $oklch;
return true;
}
public function ok_b(
$b = null
): float|bool {
if (!isset($b))
return $this->oklab["b"];
if (!is_numeric($b))
throw new \InvalidArgumentException(
"OK B axis value must be numeric."
);
$this->oklab["b"] = (float) $b;
$oklch = $this->oklab2oklch($this->oklab);
if (is_nan($oklch["h"]))
$oklch["h"] = $this->oklch["h"];
$this->oklch = $oklch;
return true;
}
public function ok_c(
$c = null
): float|bool {
if (!isset($c))
return $this->oklch["c"];
if (!is_numeric($c) or $c < 0)
throw new \RangeException(
"OK chroma value must be > 0."
);
$this->oklch["c"] = (float) $c;
$this->oklab = $this->oklch2oklab($this->oklch);
return true;
}
public function ok_h(
$h = null
): float|bool {
if (!isset($h))
return $this->oklch["h"];
if (!is_numeric($h))
throw new \InvalidArgumentException(
"OK hue value must be numeric."
);
if ($h > 360)
$h = $h % 360;
while ($h < 0)
$h = $h + 360;
$this->oklch["h"] = (float) $h;
$this->oklab = $this->oklch2oklab($this->oklch);
return true;
}
public function alpha(
$a = null
): float|bool {
if (!isset($a))
return $this->alpha;
if (!is_numeric($a) or $a < 0 or $a > 1)
throw new \RangeException(
"Alpha value must be in the range 0-1."
);
$this->alpha = (float) $a;
return true;
}
public function keyword(
$str = null
): string|bool {
if (isset($str))
return $this->interpret_keyword($str);
$reflect = new \ReflectionClass(get_class($this));
$constants = $reflect->getConstants();
$colors = array();
foreach ($constants as $key => $val) {
if (preg_match("/^CSS_COLOR_/", $key))
$colors[strtolower(substr($key, 10))] = $val;
}
$hexa = $this->hexa();
foreach ($colors as $name => $val) {
if ($hexa == $val)
return $name;
}
return false;
}
public static function contrast(
$color1,
$color2
): float {
if (!$color1 instanceof color2css)
throw new \InvalidArgumentException(
"Color 1 must be an instance of color2css."
);
if (!$color2 instanceof color2css)
throw new \InvalidArgumentException(
"Color 2 must be an instance of color2css."
);
$l1 = self::luminance($color1);
$l2 = self::luminance($color2);
$c = ($l1 > $l2) ?
($l1 + 0.05) / ($l2 + 0.05) :
($l2 + 0.05) / ($l1 + 0.05) ;
return (float) $c;
}
public static function luminance(
$color
): float {
if (!$color instanceof color2css)
throw new \InvalidArgumentException(
"Color must be an instance of color2css."
);
$n = $color->red();
$r = ($n <= 0.03928) ?
$n / 12.92 :
(($n + 0.055) / 1.055) ** 2.4 ;