-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTinyColor.livecodescript
1481 lines (1145 loc) · 46.9 KB
/
TinyColor.livecodescript
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
script "tinyColor"
# TinyColor v1.0.0b
# Fast, small color manipulation and conversion for LiveCode.
local sTinyCounter = 0, sColor, sHexNames
# Parameters
# pColor: Color en formato string
# pOpts["format"]: Formato de salida
# pOpts["gradientType"]: Tipo de gradiente
function tinyColor pColor, pOpts
local rgb, tFormat
delete local sColor
put inputToRGB(pColor) into rgb
if rgb is empty then return "error: input is not string color"
put pColor into sColor["originalInput"]
put rgb["r"]into sColor["r"]
put rgb["g"]into sColor["g"]
put rgb["b"]into sColor["b"]
put rgb["a"]into sColor["a"]
if sColor["a"]is not a number then put 0 into sColor["a"]
put round(100 * sColor["a"]) / 100 into sColor["roundA"]
if rgb["a"]is a integer then put rgb["a"]into sColor["a"]
put pOpts["format"]into tFormat
if rgb["format"]is "" then put "rgb" into rgb["format"]
put rgb["format"]into sColor["format"]
put pOpts["gradientType"]into sColor["gradientType"]
// Don't let the range of [0,255] come back in [0,1].
// Potentially lose a little bit of precision here, but will fix issues where
// .5 gets interpreted as half of the total, instead of half of 1
// If it was supposed to be 128, this was already taken care of by `inputToRgb`
if sColor["r"]< 1 then put trunc(sColor["r"]) into sColor["r"]
if sColor["g"]< 1 then put trunc(sColor["g"]) into sColor["g"]
if sColor["b"]< 1 then put trunc(sColor["b"]) into sColor["b"]
put rgb["ok"]into sColor["ok"]
add 1 to sTinyCounter
put sTinyCounter into sColor["tc_id"]
return toString(tFormat)
end tinyColor
command tinyColor pColor
get tinyColor(pColor)
end tinyColor
function getSystemAppearance
switch platform()
case "Win32"
local tType, tData, tDataReady
put queryRegistry("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\AppsUseLightTheme", tType) into tData
put empty into tDataReady
repeat for each char tchar in tData
put chartonum(tchar) after tDataReady
end repeat
if tDataReady is "1000" then return "light"
else if tDataReady is "0000" then return "dark"
return empty
break
case "MacOS"
case "iphone"
case "android"
if the systemAppearance contains "dark" then
return "dark"
else
return "light"
end if
break
end switch
end getSystemAppearance
#### on tinyColor.prototype ####
function isDark pRGB
return getBrightness(pRGB) < 128
end isDark
function isLight pRGB
return not isDark(pRGB)
end isLight
function isValid
return sColor["ok"]is true
end isValid
function getOriginalInput
return sColor["originalInput"]
end getOriginalInput
function getFormat
return sColor["format"]
end getFormat
function getAlpha
return sColor["a"]
end getAlpha
function getBrightness pRGB
// http://www.w3.org/TR/AERT#color-contrast
local tRGB
put _fixRGB(pRGB) into tRGB
split tRGB by comma
return ((tRGB[1]* 299) + (tRGB[2]* 587) + (tRGB[3]* 114)) / 1000
end getBrightness
function getLuminance pRGB
local tRGB, RsRGB, GsRGB, BsRGB, R, G, B
put _fixRGB(pRGB) into tRGB
// http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
split tRGB by comma
put tRGB[1]/ 255 into RsRGB
put tRGB[2]/ 255 into GsRGB
put tRGB[3]/ 255 into BsRGB
if RsRGB <= 0.03928 then put RsRGB / 12.92 into R
else put ((RsRGB + 0.055) / 1.055)^2.4 into R
if GsRGB <= 0.03928 then put GsRGB / 12.92 into G
else put ((GsRGB + 0.055) / 1.055)^2.4 into G
if BsRGB <= 0.03928 then put BsRGB / 12.92 into B
else put ((BsRGB + 0.055) / 1.055)^2.4 into B
return (0.2126 * R) +(0.7152 * G) +(0.0722 * B)
end getLuminance
command setAlpha pValue
put boundAlpha(pValue) into sColor["a"]
put round(100 * sColor["a"]) / 100 into sColor["roundA"]
return sColor
end setAlpha
function toHsv
local hsv
put rgbToHsv(sColor["r"], sColor["g"], sColor["b"]) into hsv
return "{ h:" & item 1 of hsv * 360, " s:" & item 2 of hsv, "v:" & item 3 of hsv, "a:" & sColor["a"]& " }"
end toHsv
function toHsvString
local hsv, h, s, v
put rgbToHsv(sColor["r"], sColor["g"], sColor["b"]) into hsv
split hsv by comma
put round(hsv[1]* 360) into h
put round(hsv[2]* 100) into s
put round(hsv[3]* 100) into v
if sColor["a"]is 0 then return "hsv(" & h & ", " & s & "%, " & v & "%)"
else return "hsva( " & h & ", " & s & "%, " & v & "%, " & sColor["roundA"]& " )"
end toHsvString
function toHsl
local hsl
put rgbToHsl(sColor["r"], sColor["g"], sColor["b"]) into hsl
return "{ h:" & the item 1 of hsl * 360, "s:" & the item 2 of hsl, "l:" & the item 3 of hsl, "a:" & sColor["a"]& " }"
end toHsl
function toHslString
local hsl, h, s, l
put rgbToHsl(sColor["r"], sColor["g"], sColor["b"]) into hsl
put round(the item 1 of hsl * 360) into h
put round(the item 2 of hsl * 100) into s
put round(the item 3 of hsl * 100) into l
if sColor["a"]is 0 then return "hsl(" & h & ", " & s & "%, " & l & "%)"
else return "hsla( " & h & ", " & s & "%, " & l & "%, " & sColor["roundA"]& " )"
end toHslString
function toHex allow3Char
return rgbToHex(sColor["r"], sColor["g"], sColor["b"], allow3Char);
end toHex
function toHexString allow3Char
return toHex(allow3Char)
end toHexString
function toHex8 allow4Char
return rgbaToHex(sColor["r"], sColor["g"], sColor["b"], sColor["a"], allow4Char);
end toHex8
function toHex8String allow4Char
return toHex8(allow4Char)
end toHex8String
function toRgb pValues
if pValues is not true then
return "{ r:" & round(sColor["r"]), " g:" & round(sColor["g"]), " b:" & round(sColor["b"]), " a:" & sColor["a"]& "}"
else
if sColor["a"]is not a number and sColor["a"]is not in "0 1" then
return round(sColor["r"]), round(sColor["g"]), round(sColor["b"]), sColor["a"]
else
return round(sColor["r"]), round(sColor["g"]), round(sColor["b"])
end if
end if
end toRgb
function toRgbString
if sColor["a"]is 0 then return "rgb(" & round(sColor["r"]) & ", " & round(sColor["g"]) & ", " & round(sColor["b"]) & ")"
else return "rgba( " & round(sColor["r"]) & ", " & round(sColor["g"]) & ", " & round(sColor["b"]) & ", " & sColor["roundA"]& " )"
end toRgbString
function toPercentageRgb
return "{ r:" & round(bound01(sColor["r"], 255) * 100) & "%, g:" & round(bound01(sColor["g"], 255) * 100) & "% , b:" & round(bound01(sColor["b"], 255) * 100) & "%, a:" & sColor["a"]& "}"
end toPercentageRgb
function toPercentageRgbString
if sColor["a"]is 0 then
return "rgb(" && round(bound01(sColor["r"], 255) * 100) & "%, " && round(bound01(sColor["g"], 255) * 100) & "%, " && round(bound01(sColor["b"], 255) * 100) & "%)"
else
return "rgba(" && round(bound01(sColor["r"], 255) * 100) & "%, " && round(bound01(sColor["g"], 255) * 100) & "%, " && round(bound01(sColor["b"], 255) * 100) & "%, " && sColor["a"]& ")";
end if
end toPercentageRgbString
---> TinyColor cuenta con una relacion de 539 nombre de colores diferentes
function toName
local r, g, b, tColorNames, tResult = ""
if sColor["a"]is 100 then return "transparent"
if sColor["a"]< 0 and sColor["a"]is not empty then return false
put getColorArray() into tColorNames
put toLower(tColorNames["rgb"][round(sColor["r"]), round(sColor["g"]), round(sColor["b"])]) into tResult
if tResult is empty then return "none"
return tResult
end toName
local sColorArray
private function getColorArray
local tColorNames, rgb, tem
put the colorNames into tColorNames
put "darkturquoise|0,206,209@palegoldenrod|238,232,170@azure|240,255,255@lightgray|211,211,211@darkseagreen|143,188,143@royalblue|65,105,225@khaki|240,230,140@silver|192,192,192@mediumpurple|147,112,219@purple|128,0,128@gainsboro|220,220,220@darkslateblue|72,61,139@palegreen|152,251,152@darkorange|255,140,0@lightgrey|211,211,211@maroon|128,0,0@mediumorchid|186,85,211@lightsteelblue|176,196,222@darkgoldenrod|184,134,11@darkkhaki|189,183,107@hotpink|255,105,180@mediumseagreen|60,179,113@coral|255,127,80@rosybrown|188,143,143@teal|0,128,128@midnightblue|25,25,112@plum|221,160,221@violet|238,130,238@linen|250,240,230@gold|255,215,0@floralwhite|255,250,240@chocolate|210,105,30@lavender|230,230,250@indigo|75,0,130@slategrey|112,128,144@darkolivegreen|85,107,47@ghostwhite|248,248,255@sandybrown|244,164,96@peru|205,133,63@firebrick|178,34,34@burlywood|222,184,135@aliceblue|240,248,255@lightgoldenrodyellow|250,250,210@darkslategrey|47,79,79@darksalmon|233,150,122@skyblue|135,206,235@ivory|255,255,240@lightcoral|240,128,128@mintcream|245,255,250@pink|255,192,203@saddlebrown|139,69,19@papayawhip|255,239,213@lightskyblue|135,206,250@olivedrab|107,142,35@darkslategray|47,79,79@beige|245,245,220@lightslategrey|119,136,153@lightpink|255,182,193@blanchedalmond|255,235,205@brown|165,42,42@olive|128,128,0@cadetblue|95,158,160@palevioletred|219,112,147@slategray|112,128,144@lightslategray|119,136,153@moccasin|255,228,181@wheat|245,222,179@thistle|216,191,216@salmon|250,128,114@oldlace|253,245,230@powderblue|176,224,230@burntsienna|234,126,93@crimson|220,20,60@rebeccapurple|102,51,153@mediumturquoise|72,209,204@paleturquoise|175,238,238@orange|255,165,0@green|0,128,0@cornsilk|255,248,220@darkorchid|153,50,204@darkviolet|148,0,211@antiquewhite|250,235,215@" into tem
split tem by "@" and "|"
put tem into sColorArray["name"]
put "0,206,209|darkturquoise@238,232,170|palegoldenrod@240,255,255|azure@211,211,211|lightgray@143,188,143|darkseagreen@65,105,225|royalblue@240,230,140|khaki@192,192,192|silver@147,112,219|mediumpurple@128,0,128|purple@220,220,220|gainsboro@72,61,139|darkslateblue@152,251,152|palegreen@255,140,0|darkorange@211,211,211|lightgrey@128,0,0|maroon@186,85,211|mediumorchid@176,196,222|lightsteelblue@184,134,11|darkgoldenrod@189,183,107|darkkhaki@255,105,180|hotpink@60,179,113|mediumseagreen@255,127,80|coral@188,143,143|rosybrown@0,128,128|teal@25,25,112|midnightblue@221,160,221|plum@238,130,238|violet@250,240,230|linen@255,215,0|gold@255,250,240|floralwhite@210,105,30|chocolate@230,230,250|lavender@75,0,130|indigo@112,128,144|slategrey@85,107,47|darkolivegreen@248,248,255|ghostwhite@244,164,96|sandybrown@205,133,63|peru@178,34,34|firebrick@222,184,135|burlywood@240,248,255|aliceblue@250,250,210|lightgoldenrodyellow@47,79,79|darkslategrey@233,150,122|darksalmon@135,206,235|skyblue@255,255,240|ivory@240,128,128|lightcoral@245,255,250|mintcream@255,192,203|pink@139,69,19|saddlebrown@255,239,213|papayawhip@135,206,250|lightskyblue@107,142,35|olivedrab@47,79,79|darkslategray@245,245,220|beige@119,136,153|lightslategrey@255,182,193|lightpink@255,235,205|blanchedalmond@165,42,42|brown@128,128,0|olive@95,158,160|cadetblue@219,112,147|palevioletred@112,128,144|slategray@119,136,153|lightslategray@255,228,181|moccasin@245,222,179|wheat@216,191,216|thistle@250,128,114|salmon@253,245,230|oldlace@176,224,230|powderblue@234,126,93|burntsienna@220,20,60|crimson@102,51,153|rebeccapurple@72,209,204|mediumturquoise@175,238,238|paleturquoise@255,165,0|orange@0,128,0|green@255,248,220|cornsilk@153,50,204|darkorchid@148,0,211|darkviolet@250,235,215|antiquewhite@" into tem
split tem by "@" and "|"
put tem into sColorArray["rgb"]
repeat for each line tName in tColorNames
-- Colores que tienen el mismo rgb que otros. Ej gray0 = black
if tName is among the words of "Yellow1 Gray100 IndianRed4 Tan3 SeaGreen4 DarkSalmon DarkOrange4 Firebrick3 PaleGreen2 OrangeRed4 NavyBlue gray0 Azure1 Chartreuse1 Coral1 CornSilk1 Chocolate1 Chocolate2 Chocolate3 Chocolate4 Firebrick4 Ivory1 LightCyan1 Magenta4 Aquamarine3"
then next repeat
put getRgbFromColor(tName) into rgb
put rgb into sColorArray["name"][tName]
put tName into sColorArray["rgb"][rgb]
end repeat
return sColorArray
end getColorArray
function toFilter pSecondColor
local hex8String, secondHex8String, gradientType
put "#" & rgbaToArgbHex(sColor["r"], sColor["g"], sColor["b"], sColor["a"]) into hex8String
put hex8String into secondHex8String
if sColor["gradientType"]then put "GradientType = 1, " into gradientType
else put "" into gradientType
if pSecondColor then
get tinyColor(pSecondColor)
put "#" & rgbaToArgbHex(sColor["r"], sColor["g"], sColor["b"], sColor["a"]) into secondHex8String
end if
return "progid:DXImageTransform.Microsoft.gradient(" & gradientType & "startColorstr=" & hex8String & ",endColorstr=" & secondHex8String & ")"
end toFilter
function toString pFormat
local formatSet, formattedString = false, hasAlpha, needsAlphaFormat
if pFormat is empty then put sColor["format"]into pFormat
put not(not pFormat) into formatSet
put sColor["a"]< 1 and sColor["a"]>= 0 into hasAlpha
put not formatSet and hasAlpha and(pFormat is among the items "rgb,rgba,hex,hex6,hex3,hex4,hex8,name") into needsAlphaFormat
if needsAlphaFormat then
// Special case for"transparent", all other non - alpha formats
// will return rgba when there is transparency.
if pFormat is "name" then
return toName()
end if
return toRgbString()
end if
if pFormat is "rgb" then
put toRgbString() into formattedString
end if
if pFormat is "prgb" then
put toPercentageRgbString() into formattedString
end if
if pFormat is "hex" or pFormat is "hex6" then
put toHexString() into formattedString
end if
if pFormat is "hex3" then
put toHexString(true) into formattedString
end if
if pFormat is "hex4" then
put toHex8String(true) into formattedString
end if
if pFormat is "hex8" then
put toHex8String() into formattedString
end if
if pFormat is "name" then
put toName() into formattedString
end if
if pFormat is "hsl" then
put toHslString() into formattedString
end if
if pFormat is "hsv" then
put toHsvString() into formattedString
end if
if formattedString is empty then
return formattedString
else
return toHexString()
end if
end toString
private function tinyClone
return toString()
end tinyClone
#### END tinyColor.prototype ####
// If input is an object, force 1 into"1.0" to handle ratios properly
// String input requires"1.0" as input, so 1 will be treated as 1
function tinyColor.fromRatio pColor, opts
local newColor
if typeof(pColor) is "object" then
repeat for each key i in pColor
if pColor[i]is not empty then
if i is "a" then
put pColor[i]into newColor[i]
else
put convertToPercentage(pColor[i]) into newColor[i]
end if
end if
end repeat
put newColor into pColor
end if
return tinyColor(pColor, opts)
end tinyColor.fromRatio
// Given a string or object, convert that input to RGB
// Possible string inputs:
//
//"red"
//"#f00" or "f00"
//"#ff0000" or "ff0000"
//"#ff000000" or "ff000000"
//"rgb 255 0 0" or "rgb (255, 0, 0)"
//"rgb 1.0 0 0" or "rgb (1, 0, 0)"
//"rgba (255, 0, 0, 1)" or "rgba 255, 0, 0, 1"
//"rgba (1.0, 0, 0, 1)" or "rgba 1.0, 0, 0, 1"
//"hsl(0, 100%, 50%)" or "hsl 0 100% 50%"
//"hsla(0, 100%, 50%, 1)" or "hsla 0 100% 50%, 1"
//"hsv(0, 100%, 100%)" or "hsv 0 100% 100%"
//
function inputToRGB pColor
local rgb, tFormat = false, ok = false, tResult, tAlfa, input
local tName
put pColor into tName
replace space with "" in tName
// Input is color name
if tName is among the lines of the colorNames then
put getRgbFromColor(tName) into rgb
split rgb by comma
put rgb[1]into tResult["r"]
put rgb[2]into tResult["g"]
put rgb[3]into tResult["b"]
put "name" into tResult["format"]
put true into tResult["ok"]
end if
// Input is color Hex
if the number of words of pColor is 1 and isHex(pColor) is true then
replace space with "" in pColor
if not(pColor begins with "#") then put "#" & pColor into pColor
put getRgbFromColor(the char 1 to 7 of pColor) into rgb
split rgb by comma
put rgb[1]into tResult["r"]
put rgb[2]into tResult["g"]
put rgb[3]into tResult["b"]
put "hex" into tResult["format"]
put true into tResult["ok"]
end if
// Input is liveCode RGB
if the first char of pColor is a number and the number of items in pColor is 3 then
put replaceText(pColor, space, empty) into rgb
split rgb by comma
put rgb[1] into tResult["r"]
put rgb[2] into tResult["g"]
put rgb[3] into tResult["b"]
put "rgb" into tResult["format"]
put true into tResult["ok"]
end if
if tResult["format"]is empty and typeof(pColor) is "string" then put stringInputToObject(pColor) into pColor
if typeof(pColor) is "object" then
_objectToArray pColor
if isValidCSSUnit(pColor["r"]) and isValidCSSUnit(pColor["g"]) and isValidCSSUnit(pColor["b"]) then
put rgbToRgb(pColor["r"], pColor["g"], pColor["b"]) into rgb
if pColor["r"]ends with "%" then
put "prgb" into tFormat
else
put "rgb" into tFormat
end if
split rgb by comma
put rgb[1]into tResult["r"]
put rgb[2]into tResult["g"]
put rgb[3]into tResult["b"]
put tFormat into tResult["format"]
put true into tResult["ok"]
else if isValidCSSUnit(pColor["h"]) and isValidCSSUnit(pColor["s"]) and isValidCSSUnit(pColor["v"]) then
put convertToPercentage(pColor["s"]) into pColor["s"]
put convertToPercentage(pColor["v"]) into pColor["v"]
put hsvToRgb(pColor["h"], pColor["s"], pColor["v"]) into rgb
split rgb by comma
put rgb[1]into tResult["r"]
put rgb[2]into tResult["g"]
put rgb[3]into tResult["b"]
put "hsv" into tResult["format"]
put true into tResult["ok"]
else if isValidCSSUnit(pColor["h"]) and isValidCSSUnit(pColor["s"]) and isValidCSSUnit(pColor["l"]) then
put convertToPercentage(pColor["s"]) into pColor["s"]
put convertToPercentage(pColor["l"]) into pColor["l"]
put hslToRgb(pColor["h"], pColor["s"], pColor["l"]) into rgb
split rgb by comma
put rgb[1]into tResult["r"]
put rgb[2]into tResult["g"]
put rgb[3]into tResult["b"]
put "hsl" into tResult["format"]
put true into tResult["ok"]
end if
end if
if rgb[4]is not empty then put rgb[4]into tAlfa
put boundAlpha(tAlfa) into tAlfa
if the number of elements of tResult < 5 then
return empty for value
end if
put min(255, max(tResult["r"], 0)) into tResult["r"]
put min(255, max(tResult["g"], 0)) into tResult["g"]
put min(255, max(tResult["b"], 0)) into tResult["b"]
return tResult for value
end inputToRGB
// Conversion Functions
// --------------------
// `rgbToHsl`, `rgbToHsv`, `hslToRgb`, `hsvToRgb` modified from:
// <http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript>
// `rgbToRgb`
// Handle bounds / percentage checking to conform to CSS color spec
// <http://www.w3.org/TR/css3-color/>
// *Assumes:* r, g, b in [0, 255] or [0, 1]
// *Returns:* r, g, b in [0, 255]
function rgbToRgb r, g, b
if ("." is in r or "." is in g or "." is in b) and max(r, g, b) <= 1 then
put r * 255 into r
put g * 255 into g
put b * 255 into b
end if
return bound01(r, 255) * 255, bound01(g, 255) * 255, bound01(b, 255) * 255
end rgbToRgb
// `rgbToHsl`
// Converts an RGB color value to HSL.
// *Assumes:* r, g and b are contained in [0, 255] or [0, 1]
// *Returns:* h, s, l in [0,1]
function rgbToHsl r, g, b
local tMx, tMn, h, s, l, d
put bound01(r, 255) into r
put bound01(g, 255) into g
put bound01(b, 255) into b
put max(r, g, b) into tMx
put min(r, g, b) into tMn
put (tMx + tMn) / 2 into l
if tMx is tMn then
// achromatic
put 0 into h
put 0 into s
else
put tMx - tMn into d
if l > 0.5 then
put d /(2 - tMx - tMn) into s
else
put d /(tMx + tMn) into s
end if
switch tMx
case r
local t
if g < b then
put 6 into t
else
put 0 into t
end if
put (g - b) / d + t into h
break
case g
put (b - r) / d + 2 into h
break
case b
put (r - g) / d + 4 into h
break
end switch
put h / 6 into h
end if
return h, s, l
end rgbToHsl
// `hslToRgb`
// Converts an HSL color value to RGB.
// *Assumes:* h is contained in [0, 1] or [0, 360] and s and l are contained [0, 1] or [0, 100]
// *Returns:* r, g, b in the set [0, 255]
function hslToRgb h, s, l
local r, g, b, q, p
put bound01(h, 360) into h
put bound01(s, 100) into s
put bound01(l, 100) into l
if s is 0 then
// achromatic
put round(l * 255) into l
return l, l, l
else
if l < 0.5 then
put l *(1 + s) into q
else
put l + s - l * s into q
end if
put 2 * l - q into p
put hue2rgb(p, q, h + 1 / 3) into r
put hue2rgb(p, q, h) into g
put hue2rgb(p, q, h - 1 / 3) into b
end if
return round(r * 255), round(g * 255), round(b * 255)
end hslToRgb
private function hue2rgb p, q, t
if t < 0 then add 1 to t
if t > 1 then subtract 1 from t
if t < 1 / 6 then return p +(q - p) * 6 * t
if t < 1 / 2 then return q
if t < 2 / 3 then return p +(q - p) *(2 / 3 - t) * 6
return p
end hue2rgb
// `rgbToHsv`
// Converts an RGB color value to HSV
// *Assumes:* r, g, and b are contained in the set [0, 255] or [0, 1]
// *Returns:* h, s, v in [0,1]
function rgbToHsv r, g, b
local tMx, tMn, d, h, s, v
put bound01(r, 255) into r
put bound01(g, 255) into g
put bound01(b, 255) into b
put max(r, g, b) into tMx
put min(r, g, b) into tMn
put tMx into h
put tMx into s
put tMx into v
put tMx - tMn into d
if tMx is 0 then
put 0 into s
else
put d / tMx into s
end if
if tMx is tMn then
put 0 into h // achromatic
else
switch tMx
case r
if g < b then
put (g - b) / d + 6 into h
else
put (g - b) / d into h
end if
break
case g
put (b - r) / d + 2 into h
break
case b
put (r - g) / d + 4 into h
break
end switch
put h / 6 into h
end if
return h, s, v
end rgbToHsv
// `hsvToRgb`
// Converts an HSV color value to RGB.
// *Assumes:* h is contained in [0, 1] or [0, 360] and s and v are contained in [0, 1] or [0, 100]
// *Returns:* r, g, b in the set [0, 255]
function hsvToRgb h, s, v
local i, f, p, q, t, tMod, r, g, b
put bound01(h, 360) * 6 into h
put bound01(s, 100) into s
put bound01(v, 100) into v
put floor(h) into i
put h - i into f
put v *(1 - s) into p
put v *(1 - f * s) into q
put v *(1 -(1 - f) * s) into t
put i mod 6 into tMod
add 1 to tMod
put item tMod of (v, q, p, p, t, v) into r
put item tMod of (t, v, v, q, p, p) into g
put item tMod of (p, p, t, v, v, q) into b
return r * 255, g * 255, b * 255
end hsvToRgb
// `rgbToHex`
// Converts an RGB color to hex
// Assumes r, g, and b are contained in the set [0, 255]
// Returns a 6 character hex
function rgbToHex r, g, b, allow3Char
local hex
put baseconvert(round(r), 10, 16) into r
if the length of r < 2 then put 0 & r into r
put baseconvert(round(g), 10, 16) into g
if the length of g < 2 then put 0 & g into g
put baseconvert(round(b), 10, 16) into b
if the length of b < 2 then put 0 & b into b
return "#" & toLower(r & g & b)
end rgbToHex
// `rgbaToHex`
// Converts an RGBA color plus alpha transparency to hex
// Assumes r, g, b are contained in the set [0, 255] and
// a in [0, 1]. Returns a 4 or 8 character rgba hex
function rgbaToHex r, g, b, pA, allow4Char
local hex, tA
put baseconvert(round(r), 10, 16) into r
if the length of r < 2 then put 0 & r into r
put baseconvert(round(g), 10, 16) into g
if the length of g < 2 then put 0 & g into g
put baseconvert(round(b), 10, 16) into b
if the length of b < 2 then put 0 & b into b
put convertDecimalToHex(pA) into tA
put baseconvert(round(tA), 10, 16) into tA
if the length of tA < 2 then put 0 & tA into tA
return "#" & toLower(r & g & b & tA)
end rgbaToHex
// `rgbaToArgbHex`
// Converts an RGBA color to an ARGB Hex8 string
// Rarely used, but required for"toFilter()"
function rgbaToArgbHex r, g, b, pA
local hex, tA
put baseconvert(round(r), 10, 16) into r
if the length of r < 2 then put 0 & r into r
put baseconvert(round(g), 10, 16) into g
if the length of g < 2 then put 0 & g into g
put baseconvert(round(b), 10, 16) into b
if the length of b < 2 then put 0 & b into b
put convertDecimalToHex(pA) into tA
put baseconvert(round(tA), 10, 16) into tA
if the length of tA < 2 then put 0 & tA into tA
return "#" & r & g & b & tA
end rgbaToArgbHex
// `equals`
// Can be called with any tinyColor input
function tinyColor.equals color1, color2
if color1 is empty or color2 is empty then return false
tinyColor color1
put toRgbString() into color1
tinyColor color2
put toRgbString() into color2
return color1 is color2
end tinyColor.equals
function tinyColor.random
return tinyColor.fromRatio(random(255), random(255), random(255))
end tinyColor.random
// Modification Functions
// ----------------------
// Thanks to less.js for some of the basics here
// <https://github.com/cloudhead/less.js/blob/master/lib/less/functions.js>
function desaturate pColor, amount
local hsl, l, tBaseColor, tDesaturateColor
put sColor into tBaseColor
if pColor is not empty then tinyColor pColor
if amount is not an integer then put 20 into amount
put toHsl() into hsl
_objectToArray hsl
put hsl["s"]-(amount / 100) into hsl["s"]
put clamp01(hsl["s"]) into hsl["s"]
put tinyColor("hsl" && hsl["h"]& ", " & hsl["s"]& ", " & hsl["l"]) into tDesaturateColor
if tBaseColor is not empty then put tBaseColor into sColor
return tDesaturateColor
end desaturate
function saturate pColor, amount
local hsl, l, tBaseColor, tSaturateColor
put sColor into tBaseColor
if pColor is not empty then tinyColor pColor
if amount is not an integer then put 20 into amount
put toHsl() into hsl
_objectToArray hsl
put hsl["s"]+(amount / 100) into hsl["s"]
put clamp01(hsl["s"]) into hsl["s"]
put tinyColor("hsl" && hsl["h"]& ", " & hsl["s"]& ", " & hsl["l"]) into tSaturateColor
if tBaseColor is not empty then put tBaseColor into sColor
return tSaturateColor
end saturate
function greyScale pColor
return desaturate(pColor, 100)
end greyScale
function lighten pColor, amount
local hsl, l, tBaseColor, tLightenColor
put sColor into tBaseColor
if pColor is not empty then tinyColor pColor
if amount is not an integer then put 20 into amount
put toHsl() into hsl
_objectToArray hsl
put amount / 100 + hsl["l"]into hsl["l"]
put clamp01(hsl["l"]) into hsl["l"]
put tinyColor("hsl" && hsl["h"]& ", " & hsl["s"]& ", " & hsl["l"]) into tLightenColor
if tBaseColor is not empty then put tBaseColor into sColor
return tLightenColor
end lighten
function brighten pColor, amount
local rgb, tBaseColor, tBrightenColor
put sColor into tBaseColor
if amount is not an integer then put 20 into amount
if pColor is not empty then tinyColor pColor
put toRgb() into rgb
_objectToArray rgb
put max(0, min(255, rgb["r"]- round(255 * -(amount / 100)))) into rgb["r"]
put max(0, min(255, rgb["g"]- round(255 * -(amount / 100)))) into rgb["g"]
put max(0, min(255, rgb["b"]- round(255 * -(amount / 100)))) into rgb["b"]
tinyColor("rgb" && rgb["r"]&& rgb["g"]&& rgb["b"])
put toRgb(true) into tBrightenColor
put tBaseColor into sColor
return tBrightenColor
end brighten
function darken pColor, amount
local hsl, l, tBaseColor, tDarkenColor
put sColor into tBaseColor
if pColor is not empty then tinyColor pColor
if amount is not an integer then put 20 into amount
put toHsl() into hsl
_objectToArray hsl
put hsl["l"]- amount / 100 into hsl["l"]
put clamp01(hsl["l"]) into hsl["l"]
put tinyColor("hsl" && hsl["h"]& ", " & hsl["s"]& ", " & hsl["l"]) into tDarkenColor
if tBaseColor is not empty then put tBaseColor into sColor
return tDarkenColor
end darken
// Spin takes a positive or negative amount within [-360, 360] indicating the change of hue.
// Values outside of this range will be wrapped into this range.
function spin pColor, amount
local hsl, hue
get tinyColor(pColor)
put toHsl() into hsl
_objectToArray hsl
put (hsl["h"]+ amount) mod 360 into hue
if hue < 0 then
put 360 + hue into hsl["h"]
else
put hue into hsl["h"]
end if
return tinyColor("hsl" && hsl["h"]& ", " & hsl["s"]& ", " & hsl["l"]);
end spin
// Combination Functions
// ---------------------
// Thanks to jQuery xColor for some of the ideas behind these
// <https://github.com/infusion/jQuery-xcolor/blob/master/jquery.xcolor.js>
function complement pColor
local hsl
get tinyColor(pColor)
put toHsl() into hsl
_objectToArray hsl
put (hsl["h"]+ 180) mod 360 into hsl["h"]
return tinyColor("hsl" && hsl["h"]& ", " & hsl["s"]& ", " & hsl["l"])
end complement
function triad pColor
local hsl, h, triad, tColor, tOldColor
put sColor into tOldColor
tinyColor pColor
put toHsl() into hsl
_objectToArray hsl
put hsl["h"]into h
put toRgb(true) into triad[1]
put "{ h:" &&(h + 120) mod 360, "s: " & hsl["s"], "l: " & hsl["l"]& " }" into tColor
tinyColor tColor
put toRgb(true) into triad[2]
put "{ h:" &&(h + 240) mod 360, "s: " & hsl["s"], "l: " & hsl["l"]& " }" into tColor
tinyColor tColor
put toRgb(true) into triad[3]
put tOldColor into sColor
return triad
end triad
function tetrad pColor
local hsl, h, tetrad, tColor, tOldColor
put sColor into tOldColor
tinyColor pColor
put toHsl() into hsl
_objectToArray hsl
put toRgb(true) into tetrad[1]
put "{ h:" &(h + 90) mod 360, "s: " & hsl["s"], "l: " & hsl["l"]&& "}" into tColor
tinyColor tColor
put toRgb(true) into tetrad[2]
put "{ h:" &(h + 180) mod 360, "s: " & hsl["s"], "l: " & hsl["l"]&& "}" into tColor
tinyColor tColor
put toRgb(true) into tetrad[3]
put "{ h:" &(h + 270) mod 360, "s: " & hsl["s"], "l: " & hsl["l"]&& "}" into tColor
tinyColor tColor
put toRgb(true) into tetrad[4]
return tetrad
end tetrad
function splitcomplement pColor
local hsl, h, tSplitcomplement, tOldColor
put sColor into tOldColor
tinyColor pColor
put toHsl() into hsl
put toRgb(true) into tSplitcomplement[1]
_objectToArray hsl
put hsl["h"]into h
tinyColor("{ h: " &(h + 72) mod 360, "s:" && hsl["s"], "l:" && hsl["l"]& " }")
put toRgb(true) into tSplitcomplement[2]
tinyColor("{ h: " &(h + 216) mod 360, "s:" && hsl["s"], "l:" && hsl["l"]& " }")
put toRgb(true) into tSplitcomplement[3]
put tOldColor into sColor
return tSplitcomplement
end splitcomplement
function analogous pColor, pResults, pSlices
local hsl, tColor, tPart, ret, tOldColor, tResult
put sColor into tOldColor
if pResults is no an integer then put 6 into pResults
if pSlices is no an integer then put 30 into pSlices