-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyepy.py
1171 lines (938 loc) · 36.9 KB
/
dyepy.py
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
"""
Module with everything related to styles and colors in any way.
From rgb-hex converters to printing text in colors on a command-line,
this module allows you to deal with colors, also helpful when making
GUIs on Python (using tkinter, PyGame, etc.), by giving you the
colors you want in Hexadecimal to common colors to all types
of color representations supported by CSS4 too! Just name it!
Separate documentation for each function and class written with them.
To read the documentation, type `help(<function/class>)` into the CLI
NOTE: Some conversions/color values (like dyepy.Colors.hsv, hsl) may be
inaccurate due to floating point properties and type conversions.
They can't be removed completely (although optimizations are being done)
But they shouldn't be much of a problem since the errors are very small
Therefore, there is no need to worry. It is not a bug, just a software \
limitation. In that case, try not to use those functions if you want accuracy
"""
__author__ = 'SFM61319'
__email__ = '[email protected]'
__github__ = 'https://github.com/SFM61319/DyePy'
__version__ = '1.0.1'
__desc__ = 'A Python module for all colors and related functions'
# Import `typing` for type-hinting
import typing
# Import `randint` from `random` as `_randint`
from random import randint as _randint
# Import `system` from `os` as `_system`
from os import system as _system
def clamp(
value: typing.Union[int, float] = 0.5,
minimum: typing.Union[int, float] = 0,
maximum: typing.Union[int, float] = 1
) -> typing.Union[int, float]:
"""
Returns the clamped value between *minimum* and *maximum*
A clamped value is the value itself when it lies
between the minimum and the maximum, and if the
value crosses the extremes, then the extremity
closer to the value becomes the clamped value.
E.g.:
clamp() -> clamp(0.5, 0, 1) -> 0.5;
clamp(2, 0, 1) -> 1;
clamp(-1, 0, 1) -> 0;
"""
if value < minimum:
return minimum
if value > maximum:
return maximum
return value
# A class to print in different colors (command-line only)
class Styles:
"""
Styles class
A class to help print both foreground and background in different
styles and colors (both as variables) on a command-line.
NOTE: All methods and sub-methods are `static`
Print blue text with green background
E.g.: print(Styles.Bg.GREEN, Styles.Fg.BLUE, 'Blue text', Styles.RESET)
Or print the text in italics
E.g.: print(Styles.ITALIC, 'Italic text', Styles.RESET)
Here's what the constants are supposed to do:
---- Reset ----
RESET: resets all the colors and styles used in the preceding text
---- Styles ----
BOLD: bolds all the succeeding text
DISABLE: reduces intensity (not widely supported)
ITALIC: writes text in italics (not widely supported)
UNDERLINE: underlines succeeding text
SLOWBLINK: makes the text blink slowly (< 150 per minute)
RAPIDBLINK: makes the text blink > 150/min (not widely supported)
REVERSE: swaps default foreground and background colors and styles
INVISIBLE: makes the foreground transparent
STRIKETHROUGH: strikes through the text
REVEAL: switches Conceal off (not widely supported)
FRAME: frames the succeeding text
ENCIRCLE: encircles the succeeding text
OVERLINE: prints an overline on the succeeding text
---- Colors ----
-- Foreground (Fg) --
Fg.<COLOR_NAME>: sets the color to the succeeding fg
Fg.n(<intensity>): sets the pre-selected color to fg
Fg.rgb(<r>, <g>, <b>): sets the calculated color to fg
-- Background (Bg) --
Bg.<COLOR_NAME>: sets the color to the succeding bg
Bg.n(<intensity>): sets the pre-selected color to bg
Bg.rgb(<r>, <g>, <b>): sets the calculated color to bg
For more info on `n`, refer to this table:
https://i.stack.imgur.com/KTSQa.png
Please do not attempt to change these constants in the module
or the Python file this is being imported to, as it may affect
the output of the colors and may even corrupt and work irregularly
and not as expected when being ran in a Python environment
For more information on command-line styling, refer to:
http://ascii-table.com/ansi-escape-sequences-vt-100.php
"""
# Reset
RESET = reset = '\x1b[00m'
# Styles
BOLD = bold = '\x1b[01m'
DISABLE = disable = '\x1b[02m'
ITALIC = italic = '\x1b[03m'
UNDERLINE = underline = '\x1b[04m'
SLOWBLINK = slowblink = '\x1b[05m'
RAPIDBLINK = rapidblink = '\x1b[06m'
REVERSE = reverse = '\x1b[07m'
INVISIBLE = invisible = '\x1b[08m'
STRIKETHROUGH = strikethrough = '\x1b[09m'
REVEAL = reveal = '\x1b[28m'
FRAME = frame = '\x1b[51m'
ENCIRCLE = encircle = '\x1b[52m'
OVERLINE = overline = '\x1b[53m'
# Foreground colors
class Foreground:
"""
Foreground (Fg) sub-class to set foreground
colors of succeeding text occurences
"""
BLACK = black = '\x1b[30m'
RED = red = '\x1b[31m'
GREEN = green = '\x1b[32m'
ORANGE = orange = '\x1b[33m'
BLUE = blue = '\x1b[34m'
PURPLE = purple = '\x1b[35m'
CYAN = cyan = '\x1b[36m'
LIGHTGREY = lightgrey = '\x1b[37m'
DARKGREY = darkgrey = '\x1b[90m'
LIGHTRED = lightred = '\x1b[91m'
LIGHTGREEN = lightgreen = '\x1b[92m'
YELLOW = yellow = '\x1b[93m'
LIGHTBLUE = lightblue = '\x1b[94m'
PINK = pink = '\x1b[95m'
LIGHTCYAN = lightcyan = '\x1b[96m'
WHITE = white = '\x1b[38;2;255;255;255m'
SPOTIFYGREEN = spotifygreen = '\x1b[38;2;29;185;84m'
WINDOWSBLUE = '\x1b[38;2;0;120;215m'
@staticmethod
def n(intensity: typing.Union[int, float] = 0) -> str:
"""
Returns calculated ANSI color code for unnamed bg colors
Can be used as follow:
print(Styles.Fg.n(255), 'Some random color', Styles.RESET)
Refer to https://i.stack.imgur.com/KTSQa.png for more
information on how to use the function to get a color
"""
return f'\x1b[38;5;{clamp(round(intensity), 0, 255)}m'
@staticmethod
def rgb(
red: typing.Union[int, float] = 0,
blue: typing.Union[int, float] = 0,
green: typing.Union[int, float] = 0
) -> str:
"""
Returns calculated ANSI color code for unnamed fg colors
Can be used as follows:
print(Styles.Fg.rgb(0, 120, 215), 'Windows Default Blue', Styles.RESET)
print(Styles.Fg.rgb(29, 185, 84), 'Spotify Green', Styles.RESET)
NOTE: 0 ≤ r, g, b ≤ 255
"""
return (
f'\x1b[38;2;'
f'{clamp(round(red), 0, 255)};'
f'{clamp(round(blue), 0, 255)};'
f'{clamp(round(green), 0, 255)}m'
)
Fg = Foreground
# Background colors
class Background:
"""
Background (Bg) sub-class to set background
colors of succeeding text occurences
"""
BLACK = black = '\x1b[40m'
RED = red = '\x1b[41m'
GREEN = green = '\x1b[42m'
ORANGE = orange = '\x1b[43m'
BLUE = blue = '\x1b[44m'
PURPLE = purple = '\x1b[45m'
CYAN = cyan = '\x1b[46m'
LIGHTGREY = lightgrey = '\x1b[47m'
DARKGREY = darkgrey = '\x1b[100m'
LIGHTRED = lightred = '\x1b[101m'
LIGHTGREEN = lightgreen = '\x1b[102m'
YELLOW = yellow = '\x1b[103m'
LIGHTBLUE = lightblue = '\x1b[104m'
PINK = pink = '\x1b[105m'
LIGHTCYAN = lightcyan = '\x1b[106m'
WHITE = white = '\x1b[48;2;255;255;255m'
SPOTIFYGREEN = spotifygreen = '\x1b[48;2;29;185;84m'
WINDOWSBLUE = '\x1b[48;2;0;120;215m'
@staticmethod
def n(intensity: typing.Union[int, float] = 255) -> str:
"""
Returns calculated ANSI color code for unnamed bg colors
Can be used as follow:
print(Styles.Bg.n(255), 'Some random color', Styles.RESET)
Refer to https://i.stack.imgur.com/KTSQa.png for more
information on how to use the function to get a color
"""
return f'\x1b[48;5;{clamp(round(intensity), 0, 255)}m'
@staticmethod
def rgb(
red: typing.Union[int, float] = 255,
blue: typing.Union[int, float] = 255,
green: typing.Union[int, float] = 255
) -> str:
"""
Returns calculated ANSI color code for unnamed bg colors
Can be used as follows:
print(Styles.Bg.rgb(0, 120, 215), 'Windows Default Blue', Styles.RESET)
print(Styles.Bg.rgb(29, 185, 84), 'Spotify Green', Styles.RESET)
NOTE: 0 ≤ red, green, blue ≤ 255
"""
return (
f'\x1b[48;2;'
f'{clamp(round(red), 0, 255)};'
f'{clamp(round(blue), 0, 255)};'
f'{clamp(round(green), 0, 255)}m'
)
Bg = Background
# A class to use pre-defined colors from CSS4 and get HEX values
class Colors:
"""
Colors class
A class of named Hex color codes as constants
(that can be used simply by referencing them), and
static methods to convert ABC colorspaces to Hex
These names and values have been inherited from CSS4,
and there are some extra built-in colors like the
Windows Default Blue color and the Spotify Green color.
This class is completely different from the class
`Styles` (which can be used for font-styling and coloring)
"""
ALICEBLUE = '#f0f8ff'
ANTIQUEWHITE = '#faebd7'
AQUA = '#00ffff'
AQUAMARINE = '#7fffd4'
AZURE = '#f0ffff'
BEIGE = '#f5f5dc'
BISQUE = '#ffe4c4'
BLACK = '#000000'
BLANCHEDALMOND = '#ffebcd'
BLUE = '#0000ff'
BLUEVIOLET = '#8a2be2'
BROWN = '#a52a2a'
BURLYWOOD = '#deb887'
CADETBLUE = '#5f9ea0'
CHARTREUSE = '#7fff00'
CHOCOLATE = '#d22551e'
CORAL = '#ff7f50'
CORNFLOWERBLUE = '#6495ed'
CORNSILK = '#fff8dc'
CRIMSON = '#dc143c'
CYAN = '#00ffff'
DARKBLUE = '#00008b'
DARKCYAN = '#008b8b'
DARKGOLDENROD = '#b8860b'
DARKGRAY = '#a9a9a9'
DARKGREEN = '#006400'
DARKKHAKI = '#bdb76b'
DARKMAGENTA = '#8b008b'
DARKOLIVEGREEN = '#556b2f'
DARKORANGE = '#ff8c00'
DARKORCHID = '#9932cc'
DARKRED = '#8b0000'
DARKSALMON = '#e9967a'
DARKSEAGREEN = '#8fbc8f'
DARKSLATEBLUE = '#483d8b'
DARKSLATEGRAY = '#2f4f4f'
DARKTURQUOISE = '#00ced1'
DARKVIOLET = '#9400d3'
DEEPPINK = '#ff1493'
DEEPSKYBLUE = '#00bfff'
DIMGRAY = '#255255255'
DODGERBLUE = '#1e90ff'
FIREBRICK = '#b22222'
FLORALWHITE = '#fffaf0'
FORESTGREEN = '#228b22'
FUCHSIA = '#ff00ff'
GAINSBORO = '#dcdcdc'
GHOSTWHITE = '#f8f8ff'
GOLD = '#ffd700'
GOLDENROD = '#daa520'
GRAY = '#808080'
GREEN = '#008000'
GREENYELLOW = '#adff2f'
HONEYDEW = '#f0fff0'
HOTPINK = '#ff255b4'
INDIANRED = '#cd5c5c'
INDIGO = '#4b0082'
IVORY = '#fffff0'
KHAKI = '#f0e68c'
LAVENDER = '#e6e6fa'
LAVENDERBLUSH = '#fff0f5'
LAWNGREEN = '#7cfc00'
LEMONCHIFFON = '#fffacd'
LIGHTBLUE = '#add8e6'
LIGHTCORAL = '#f08080'
LIGHTCYAN = '#e0ffff'
LIGHTGOLDENRODYELLOW = '#fafad2'
LIGHTGREEN = '#90ee90'
LIGHTGREY = '#d3d3d3'
LIGHTPINK = '#ffb6c1'
LIGHTSALMON = '#ffa07a'
LIGHTSEAGREEN = '#20b2aa'
LIGHTSKYBLUE = '#87cefa'
LIGHTSLATEGRAY = '#778899'
LIGHTSTEELBLUE = '#b0c4de'
LIGHTYELLOW = '#ffffe0'
LIME = '#00ff00'
LIMEGREEN = '#32cd32'
LINEN = '#faf0e6'
MAGENTA = '#ff00ff'
MAROON = '#800000'
MEDIUMAQUAMARINE = '#66cdaa'
MEDIUMBLUE = '#0000cd'
MEDIUMORCHID = '#ba55d3'
MEDIUMPURPLE = '#9370d8'
MEDIUMSEAGREEN = '#3cb371'
MEDIUMSLATEBLUE = '#7b68ee'
MEDIUMSPRINGGREEN = '#00fa9a'
MEDIUMTURQUOISE = '#48d1cc'
MEDIUMVIOLETRED = '#c71585'
MIDNIGHTBLUE = '#191970'
MINTCREAM = '#f5fffa'
MISTYROSE = '#ffe4e1'
MOCCASIN = '#ffe4b5'
NAVAJOWHITE = '#ffdead'
NAVY = '#000080'
OLDLACE = '#fdf5e6'
OLIVE = '#808000'
OLIVEDRAB = '#6b8e23'
ORANGE = '#ffa500'
ORANGERED = '#ff4500'
ORCHID = '#da70d6'
PALEGOLDENROD = '#eee8aa'
PALEGREEN = '#98fb98'
PALETURQUOISE = '#afeeee'
PALEVIOLETRED = '#d87093'
PAPAYAWHIP = '#ffefd5'
PEACHPUFF = '#ffdab9'
PERU = '#cd853f'
PINK = '#ffc0cb'
PLUM = '#dda0dd'
POWDERBLUE = '#b0e0e6'
PURPLE = '#800080'
REBECCAPURPLE = '#663399'
RED = '#ff0000'
ROSYBROWN = '#bc8f8f'
ROYALBLUE = '#41255e1'
SADDLEBROWN = '#8b4513'
SALMON = '#fa8072'
SANDYBROWN = '#f4a460'
SEAGREEN = '#2e8b57'
SEASHELL = '#fff5ee'
SIENNA = '#a0522d'
SILVER = '#c0c0c0'
SKYBLUE = '#87ceeb'
SLATEBLUE = '#6a5acd'
SLATEGRAY = '#708090'
SNOW = '#fffafa'
SPOTIFYGREEN = '#1db954'
SPRINGGREEN = '#00ff7f'
STEELBLUE = '#4682b4'
TAN = '#d2b48c'
TEAL = '#008080'
THISTLE = '#d8bfd8'
TOMATO = '#ff6347'
TURQUOISE = '#40e0d0'
VIOLET = '#ee82ee'
WHEAT = '#f5deb3'
WHITE = '#ffffff'
WHITESMOKE = '#f5f5f5'
WINDOWSBLUE = '#0078d7'
YELLOW = '#ffff00'
YELLOWGREEN = '#9acd32'
# A function to convert RGB values to Hex values
@staticmethod
def rgb(
red: typing.Union[int, float] = 0,
green: typing.Union[int, float] = 0,
blue: typing.Union[int, float] = 0
) -> str:
"""
Returns the Hex value from an RGB value for many modules use
Hex values as their default or accepted color code values
NOTE: 0 ≤ red, green, blue ≤ 255
Can also be used as a RGB-to-Hex converter
"""
return (
f'#{clamp(round(red), 0, 255):02x}'
f'{clamp(round(green), 0, 255):02x}'
f'{clamp(round(blue), 0, 255):02x}'
)
# A function to convert HSV values to Hex values
@staticmethod
def hsv(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
value: typing.Union[int, float] = 0
) -> str:
"""
Returns the Hex value from an HSV value for many modules use
Hex values as their default or accepted color code values
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, value ≤ 1; any other value will be clamped
NOTE: both HSB and HSV are the same colorspaces
Can also be used as a HSV-to-Hex converter
"""
return Colors.rgb(*Converters.hsv2rgb(hue, saturation, value))
hsb = hsv # Since both are the same
# A function to convert HSL values to Hex values
@staticmethod
def hsl(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
luminance: typing.Union[int, float] = 0
) -> str:
"""
Returns the Hex value from an HSL value for many modules use
Hex values as their default or accepted color code values
0 ≤ hue ≤ 360; although a other values are also acceptable
0 ≤ saturation, luminance ≤ 1; any other value will be clamped
NOTE: HSV and HSL are different colorspaces, for more information
refer to:
https://en.wikipedia.org/wiki/HSL_and_HSV
Can also be used as a HSL-to-Hex converter
"""
return Colors.rgb(*Converters.hsl2rgb(hue, saturation, luminance))
# A function to convert a YIQ color to Hex values
@staticmethod
def yiq(
y: typing.Union[int, float] = 0,
i: typing.Union[int, float] = 0,
q: typing.Union[int, float] = 0
) -> str:
"""
Returns the Hex value from a YIQ color space for many modules
use Hex values as their default or accepted color code values
NOTE: The following are the acceptable values:
0 ≤ y ≤ 1,
-0.5959 ≤ i ≤ 0.5959,
-0.5229 ≤ q ≤ 0.5229
All other values will be clamped between these ranges
Can also be used as a YIQ-to-Hex converter
"""
return Colors.rgb(*Converters.yiq2rgb(y, i, q))
# A function to convert a CMYK color to Hex values
@staticmethod
def cmyk(
cyan: typing.Union[int, float] = 0,
magenta: typing.Union[int, float] = 0,
yellow: typing.Union[int, float] = 0,
black_key: typing.Union[int, float] = 0
) -> str:
"""
Returns the Hex value from a CMYK color space for many modules
use Hex values as their default or accepted color code values
0 ≤ cyan, magenta, yellow, black_key ≤ 1, other values will be clamped
Can also be used as a CMYK-to-Hex converter
"""
return Colors.rgb(*Converters.cmyk2rgb(cyan, magenta, yellow, black_key))
@staticmethod
def getrandomcolor() -> str:
return Colors.rgb(_randint(0, 255), _randint(0, 255), _randint(0, 255))
# A class to convert colors to-fro different colorspaces
class Converters:
"""
A class of color space converters
NOTE: All methods in this class are `static` methods
Usage:
dyepy.Converters.hex2rgb(0xffffff)
"""
# A function to convert Hex values to RGB colors
@staticmethod
def hex2rgb(hexcode: typing.Union[str, int] = '#000000')\
-> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent RGB values of a hex color *hexcode*
"""
if isinstance(hexcode, int):
hexcode = hex(hexcode)
hexcode = hexcode.replace('0x', '#')
if len(hexcode) == 4: # Repetative shortcut
hexcode = f'#{hexcode[1]*2}{hexcode[2]*2}{hexcode[3]*2}'
return (
int(hexcode[1:3], 16),
int(hexcode[3:5], 16),
int(hexcode[5:7], 16)
)
# A function to convert Hex values to HSV colors
@staticmethod
def hex2hsv(hexcode: typing.Union[str, int] = '#000000')\
-> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSV/HSB values of a hex color *hexcode*
"""
return Converters.rgb2hsv(*Converters.hex2rgb(hexcode))
# A function to convert Hex values to HSL colors
@staticmethod
def hex2hsl(hexcode: typing.Union[str, int] = '#000000')\
-> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSL values of a hex color *hexcode*
"""
return Converters.rgb2hsl(*Converters.hex2rgb(hexcode))
# A function to convert Hex values to YIQ colors
@staticmethod
def hex2yiq(hexcode: typing.Union[str, int] = '#000000')\
-> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent YIQ values of a hex color *hexcode*
"""
return Converters.rgb2yiq(*Converters.hex2rgb(hexcode))
# A function to convert Hex values to CMYK colors
@staticmethod
def hex2cmyk(hexcode: typing.Union[str, int] = '#000000')\
-> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent CMYK values of a hex color *hexcode*
"""
return Converters.rgb2cmyk(*Converters.hex2rgb(hexcode))
# A function to convert RGB colors to HSV colors
@staticmethod
def rgb2hsv(
red: typing.Union[int, float] = 0,
green: typing.Union[int, float] = 0,
blue: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSV values of an RGB color value
NOTE: 0 ≤ red, green, blue ≤ 255, all other values will be clamped
"""
red = clamp(round(red), 0, 255) / 255
green = clamp(round(green), 0, 255) / 255
blue = clamp(round(blue), 0, 255) / 255
cmax = max(red, green, blue)
cmin = min(red, green, blue)
diff = cmax - cmin
# Value calculation
value = cmax
# Saturation calculation
saturation = 0 if cmax == 0 else diff / cmax
# Hue calculation
if diff == 0:
hue = 0
return (hue, saturation, value)
if cmax == red:
hue = round(60 * (((green - blue) / diff) % 6))
return (hue, saturation, value)
if cmax == green:
hue = round(60 * (((blue - red) / diff) + 2))
return (hue, saturation, value)
if cmax == blue:
hue = round(60 * (((red - green) / diff) + 4))
return (hue, saturation, value)
# A function to convert an RGB color to an HSL color
@staticmethod
def rgb2hsl(
red: typing.Union[int, float] = 0,
green: typing.Union[int, float] = 0,
blue: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSL values of an RGB color value
NOTE: 0 ≤ red, green, blue ≤ 255, all other values will be clamped
"""
red = clamp(round(red), 0, 255) / 255
green = clamp(round(green), 0, 255) / 255
blue = clamp(round(blue), 0, 255) / 255
cmax = max(red, green, blue)
cmin = min(red, green, blue)
diff = cmax - cmin
# Luminance calculation
luminance = (cmax + cmin) / 2
# Saturation calculation
saturation = diff / (1 - abs(2 * luminance - 1)) if diff else 0
# Hue calculation
if diff == 0:
hue = 0
return (hue, saturation, luminance)
if cmax == red:
hue = round(60 * (((green - blue) / diff) % 6))
return (hue, saturation, luminance)
if cmax == green:
hue = round(60 * (((blue - red) / diff) + 2))
return (hue, saturation, luminance)
if cmax == blue:
hue = round(60 * (((red - green) / diff) + 4))
return (hue, saturation, luminance)
# A function to convert an RGB color to YIQ color
@staticmethod
def rgb2yiq(
red: typing.Union[int, float] = 0,
green: typing.Union[int, float] = 0,
blue: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent YIQ values of an RGB color value
NOTE: 0 ≤ red, green, blue ≤ 255, all other values will be clamped
"""
red = clamp(round(red), 0, 255) / 255
green = clamp(round(green), 0, 255) / 255
blue = clamp(round(blue), 0, 255) / 255
y = clamp(0.299*red+0.587*green+0.114*blue)
i = clamp(0.596*red-0.274*green-0.322*blue, -0.5959, 0.5959)
q = clamp(0.211*red-0.523*green+0.312*blue, -0.5229, 0.5229)
return (y, i, q)
# A function to convert an RGB color to CMYK color
@staticmethod
def rgb2cmyk(
red: typing.Union[int, float] = 0,
green: typing.Union[int, float] = 0,
blue: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent CMYK values of an RGB color value
NOTE: 0 ≤ red, green, blue ≤ 255, all other values will be clamped
"""
red = clamp(round(red), 0, 255) / 255
green = clamp(round(green), 0, 255) / 255
blue = clamp(round(blue), 0, 255) / 255
black_key = 1 - max(red, green, blue)
if black_key == 1:
return (0, 0, 0, 1)
cyan = (1 - black_key - red) / (1 - black_key)
magenta = (1 - black_key - green) / (1 - black_key)
yellow = (1 - black_key - blue) / (1 - black_key)
return (cyan, magenta, yellow, black_key)
# A function to convert an HSV color to an RGB color
@staticmethod
def hsv2rgb(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
value: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent RGB values of an HSV color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, value ≤ 1; any other value will be clamped
"""
# Cycle clamping *hue* in [0, 360)
hue = hue-360*(hue//360) if hue >= 0 else hue+360*(-hue//360+1)
saturation = clamp(saturation)
value = clamp(value)
c = value * saturation
x = c * (1 - abs((hue / 60) % 2 - 1))
m = value - c
if 0 <= hue < 60:
return (round((c+m)*255), round((x+m)*255), round(m*255))
if 60 <= hue < 120:
return (round((x+m)*255), round((c+m)*255), round(m*255))
if 120 <= hue < 180:
return (round(m*255), round((c+m)*255), round((x+m)*255))
if 180 <= hue < 240:
return (round(m*255), round((x+m)*255), round((c+m)*255))
if 240 <= hue < 300:
return (round((x+m)*255), round(m*255), round((c+m)*255))
if 300 <= hue < 360:
return (round((c+m)*255), round(m*255), round((x+m)*255))
# A function to convert an HSV color to an HSL color
@staticmethod
def hsv2hsl(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
value: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSL values of an HSV color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, value ≤ 1; any other value will be clamped
"""
return Converters.rgb2hsl(*Converters.hsv2rgb(hue, saturation, value))
# A function to convert an HSV color to a YIQ color
@staticmethod
def hsv2yiq(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
value: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent YIQ values of an HSV color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, value ≤ 1; any other value will be clamped
"""
return Converters.rgb2yiq(*Converters.hsv2rgb(hue, saturation, value))
# A function to convert an HSV color to a CMYK color
@staticmethod
def hsv2cmyk(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
value: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent CMYK values of an HSV color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, value ≤ 1; any other value will be clamped
"""
return Converters.rgb2cmyk(*Converters.hsv2rgb(hue, saturation, value))
# A function to convert HSL color to an RGB color
@staticmethod
def hsl2rgb(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
luminance: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent RGB values of an HSL color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, luminance ≤ 1; any other value will be clamped
"""
# Cycling clamping *hue* in [0, 360)
hue = hue-360*(hue//360) if hue >= 0 else hue+360*(-hue//360+1)
saturation = clamp(saturation)
luminance = clamp(luminance)
c = (1 - abs(2 * luminance - 1)) * saturation
x = c * (1 - abs((hue / 60) % 2 - 1))
m = luminance - c / 2
if 0 <= hue < 60:
return (round((c+m)*255), round((x+m)*255), round(m*255))
if 60 <= hue < 120:
return (round((x+m)*255), round((c+m)*255), round(m*255))
if 120 <= hue < 180:
return (round(m*255), round((c+m)*255), round((x+m)*255))
if 180 <= hue < 240:
return (round(m*255), round((x+m)*255), round((c+m)*255))
if 240 <= hue < 300:
return (round((x+m)*255), round(m*255), round((c+m)*255))
if 300 <= hue < 360:
return (round((c+m)*255), round(m*255), round((x+m)*255))
# A function to convert an HSL color to an HSV color
@staticmethod
def hsl2hsv(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
luminance: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSV values of an HSL color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, luminance ≤ 1; any other value will be clamped
"""
return Converters.rgb2hsv(*Converters.hsl2rgb(hue, saturation, luminance))
# A function to convert an HSL color to a YIQ color
@staticmethod
def hsl2yiq(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
luminance: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent YIQ values of an HSL color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, luminance ≤ 1; any other value will be clamped
"""
return Converters.rgb2yiq(*Converters.hsl2rgb(hue, saturation, luminance))
# A function to convert an HSL color to a CMYK color
@staticmethod
def hsl2cmyk(
hue: typing.Union[int, float] = 0,
saturation: typing.Union[int, float] = 0,
luminance: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent CMYK values of an HSL color value
0 ≤ hue ≤ 360; although other values are also acceptable
0 ≤ saturation, luminance ≤ 1; any other value will be clamped
"""
return Converters.rgb2cmyk(*Converters.hsl2rgb(hue, saturation, luminance))
# A function to convert a YIQ color to an RGB color
@staticmethod
def yiq2rgb(
y: typing.Union[int, float] = 0,
i: typing.Union[int, float] = 0,
q: typing.Union[int, float] = 0
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent RGB values of a YIQ color value
NOTE: The following are the acceptable values:
0 ≤ y ≤ 1
-0.5959 ≤ i ≤ 0.5959
-0.5229 ≤ q ≤ 0.5229
The values will be clamped between these ranges
"""
y = clamp(y)
i = clamp(i, -0.5959, 0.5959)
q = clamp(q, -0.5229, 0.5229)
red = y + 0.956 * i + 0.621 * q
green = y - 0.272 * i - 0.647 * q
blue = y - 1.11 * i + 1.7 * q
return (
round(clamp(red) * 255),
round(clamp(green) * 255),
round(clamp(blue) * 255)
)
# A function to convert a YIQ color to an HSV color
@staticmethod
def yiq2hsv(
y: typing.Union[int, float] = 0,
i: typing.Union[int, float] = 0,
q: typing.Union[int, float] = 0,
) -> typing.Tuple[typing.Union[int, float]]:
"""
Returns the equivalent HSV values of a YIQ color value
NOTE: The following are the acceptable values:
0 ≤ y ≤ 1
-0.5959 ≤ i ≤ 0.5959
-0.5229 ≤ q ≤ 0.5229