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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
|
#!/usr/bin/python
# -*- coding: utf-8 -*-#
# Copyright (c) 2008, Xavier Basty
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''GrapeFruit - Color manipulation in Python'''
# $Id: grapefruit.py 31 2008-06-15 10:48:06Z xbasty $
__author__ = 'Xavier Basty <xbasty@gmail.com>'
__version__ = '0.1a3'
# The default white reference, use 2° Standard Observer, D65 (daylight)
_DEFAULT_WREF = (0.95043, 1.00000, 1.08890)
_oneThird = 1.0 / 3
_srgbGammaCorrInv = 0.03928 / 12.92
_sixteenHundredsixteenth = 16.0 / 116
_RybWheel = (
0, 26, 52,
83, 120, 130,
141, 151, 162,
177, 190, 204,
218, 232, 246,
261, 275, 288,
303, 317, 330,
338, 345, 352,
360)
_RgbWheel = (
0, 8, 17,
26, 34, 41,
48, 54, 60,
81, 103, 123,
138, 155, 171,
187, 204, 219,
234, 251, 267,
282, 298, 329,
360)
class Color:
'''Hold a color value.
Example usage:
To create an instance of the grapefruit.Color from RGB values:
>>> import grapefruit
>>> r, g, b = 1, 0.5, 0
>>> col = grapefruit.Color.NewFromRgb(r, g, b)
To get the values of the color in another colorspace:
>>> h, s, v = col.hsv
>>> l, a, b = col.lab
To get the complementary of a color:
>>> compl = col.ComplementaryColor()
>>> print compl.hsl
(210.0, 1.0, 0.5)
To directly convert RGB values to their HSL equivalent:
>>> h, s, l = Color.RgbToHsl(r, g, b)
'''
WHITE_REFERENCE = {
'std_A' : (1.09847, 1.00000, 0.35582),
'std_B' : (0.99093, 1.00000, 0.85313),
'std_C' : (0.98071, 1.00000, 1.18225),
'std_D50' : (0.96421, 1.00000, 0.82519),
'std_D55' : (0.95680, 1.00000, 0.92148),
'std_D65' : (0.95043, 1.00000, 1.08890),
'std_D75' : (0.94972, 1.00000, 1.22639),
'std_E' : (1.00000, 1.00000, 1.00000),
'std_F1' : (0.92834, 1.00000, 1.03665),
'std_F2' : (0.99145, 1.00000, 0.67316),
'std_F3' : (1.03753, 1.00000, 0.49861),
'std_F4' : (1.09147, 1.00000, 0.38813),
'std_F5' : (0.90872, 1.00000, 0.98723),
'std_F6' : (0.97309, 1.00000, 0.60191),
'std_F7' : (0.95017, 1.00000, 1.08630),
'std_F8' : (0.96413, 1.00000, 0.82333),
'std_F9' : (1.00365, 1.00000, 0.67868),
'std_F10' : (0.96174, 1.00000, 0.81712),
'std_F11' : (1.00899, 1.00000, 0.64262),
'std_F12' : (1.08046, 1.00000, 0.39228),
'sup_A' : (1.11142, 1.00000, 0.35200),
'sup_B' : (0.99178, 1.00000, 0.84349),
'sup_C' : (0.97286, 1.00000, 1.16145),
'sup_D50' : (0.96721, 1.00000, 0.81428),
'sup_D55' : (0.95797, 1.00000, 0.90925),
'sup_D65' : (0.94810, 1.00000, 1.07305),
'sup_D75' : (0.94417, 1.00000, 1.20643),
'sup_E' : (1.00000, 1.00000, 1.00000),
'sup_F1' : (0.94791, 1.00000, 1.03191),
'sup_F2' : (1.03245, 1.00000, 0.68990),
'sup_F3' : (1.08968, 1.00000, 0.51965),
'sup_F4' : (1.14961, 1.00000, 0.40963),
'sup_F5' : (0.93369, 1.00000, 0.98636),
'sup_F6' : (1.02148, 1.00000, 0.62074),
'sup_F7' : (0.95780, 1.00000, 1.07618),
'sup_F8' : (0.97115, 1.00000, 0.81135),
'sup_F9' : (1.02116, 1.00000, 0.67826),
'sup_F10' : (0.99001, 1.00000, 0.83134),
'sup_F11' : (1.03820, 1.00000, 0.65555),
'sup_F12' : (1.11428, 1.00000, 0.40353)}
NAMED_COLOR = {
'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': '#d2691e',
'coral': '#ff7f50',
'cornflowerblue': '#6495ed',
'cornsilk': '#fff8dc',
'crimson': '#dc143c',
'cyan': '#00ffff',
'darkblue': '#00008b',
'darkcyan': '#008b8b',
'darkgoldenrod': '#b8860b',
'darkgray': '#a9a9a9',
'darkgrey': '#a9a9a9',
'darkgreen': '#006400',
'darkkhaki': '#bdb76b',
'darkmagenta': '#8b008b',
'darkolivegreen': '#556b2f',
'darkorange': '#ff8c00',
'darkorchid': '#9932cc',
'darkred': '#8b0000',
'darksalmon': '#e9967a',
'darkseagreen': '#8fbc8f',
'darkslateblue': '#483d8b',
'darkslategray': '#2f4f4f',
'darkslategrey': '#2f4f4f',
'darkturquoise': '#00ced1',
'darkviolet': '#9400d3',
'deeppink': '#ff1493',
'deepskyblue': '#00bfff',
'dimgray': '#696969',
'dimgrey': '#696969',
'dodgerblue': '#1e90ff',
'firebrick': '#b22222',
'floralwhite': '#fffaf0',
'forestgreen': '#228b22',
'fuchsia': '#ff00ff',
'gainsboro': '#dcdcdc',
'ghostwhite': '#f8f8ff',
'gold': '#ffd700',
'goldenrod': '#daa520',
'gray': '#808080',
'grey': '#808080',
'green': '#008000',
'greenyellow': '#adff2f',
'honeydew': '#f0fff0',
'hotpink': '#ff69b4',
'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',
'lightgray': '#d3d3d3',
'lightgrey': '#d3d3d3',
'lightpink': '#ffb6c1',
'lightsalmon': '#ffa07a',
'lightseagreen': '#20b2aa',
'lightskyblue': '#87cefa',
'lightslategray': '#778899',
'lightslategrey': '#778899',
'lightsteelblue': '#b0c4de',
'lightyellow': '#ffffe0',
'lime': '#00ff00',
'limegreen': '#32cd32',
'linen': '#faf0e6',
'magenta': '#ff00ff',
'maroon': '#800000',
'mediumaquamarine': '#66cdaa',
'mediumblue': '#0000cd',
'mediumorchid': '#ba55d3',
'mediumpurple': '#9370db',
'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': '#db7093',
'papayawhip': '#ffefd5',
'peachpuff': '#ffdab9',
'peru': '#cd853f',
'pink': '#ffc0cb',
'plum': '#dda0dd',
'powderblue': '#b0e0e6',
'purple': '#800080',
'red': '#ff0000',
'rosybrown': '#bc8f8f',
'royalblue': '#4169e1',
'saddlebrown': '#8b4513',
'salmon': '#fa8072',
'sandybrown': '#f4a460',
'seagreen': '#2e8b57',
'seashell': '#fff5ee',
'sienna': '#a0522d',
'silver': '#c0c0c0',
'skyblue': '#87ceeb',
'slateblue': '#6a5acd',
'slategray': '#708090',
'slategrey': '#708090',
'snow': '#fffafa',
'springgreen': '#00ff7f',
'steelblue': '#4682b4',
'tan': '#d2b48c',
'teal': '#008080',
'thistle': '#d8bfd8',
'tomato': '#ff6347',
'turquoise': '#40e0d0',
'violet': '#ee82ee',
'wheat': '#f5deb3',
'white': '#ffffff',
'whitesmoke': '#f5f5f5',
'yellow': '#ffff00',
'yellowgreen': '#9acd32'}
def __init__(self, values, mode='rgb', alpha=1.0, wref=_DEFAULT_WREF):
'''Instantiate a new grapefruit.Color object.
Parameters:
:values:
The values of this color, in the specified representation.
:mode:
The representation mode used for values.
:alpha:
the alpha value (transparency) of this color.
:wref:
The whitepoint reference, default is 2° D65.
'''
if not(isinstance(values, tuple)):
raise TypeError, 'values must be a tuple'
if mode=='rgb':
self.__rgb = values
self.__hsl = Color.RgbToHsl(*values)
elif mode=='hsl':
self.__hsl = values
self.__rgb = Color.HslToRgb(*values)
else:
raise ValueError('Invalid color mode: ' + mode)
self.__a = alpha
self.__wref = wref
def __ne__(self, other):
return not self.__eq__(other)
def __eq__(self, other):
try:
if isinstance(other, Color):
return (self.__rgb==other.__rgb) and (self.__a==other.__a)
if len(other) != 4:
return False
rgba = self.__rgb + (self.__a,)
return reduce(lambda x, y: x and (y[0]==y[1]), zip(rgba, other), True)
except TypeError:
return False
except AttributeError:
return False
def __repr__(self):
return str(self.__rgb + (self.__a,))
def __str__(self):
'''A string representation of this grapefruit.Color instance.
Returns:
The RGBA representation of this grapefruit.Color instance.
'''
return '(%g, %g, %g, %g)' % (self.__rgb + (self.__a,))
def __unicode__(self):
'''A unicode string representation of this grapefruit.Color instance.
Returns:
The RGBA representation of this grapefruit.Color instance.
'''
return u'(%g, %g, %g, %g)' % (self.__rgb + (self.__a,))
def __iter__(self):
return iter(self.__rgb + (self.__a,))
def __len__(self):
return 4
@staticmethod
def RgbToHsl(r, g, b):
'''Convert the color from RGB coordinates to HSL.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (h, s, l) tuple in the range:
h[0...360],
s[0...1],
l[0...1]
>>> Color.RgbToHsl(1, 0.5, 0)
(30.0, 1.0, 0.5)
'''
minVal = min(r, g, b) # min RGB value
maxVal = max(r, g, b) # max RGB value
l = (maxVal + minVal) / 2.0
if minVal==maxVal:
return (0.0, 0.0, l) # achromatic (gray)
d = maxVal - minVal # delta RGB value
if l < 0.5: s = d / (maxVal + minVal)
else: s = d / (2.0 - maxVal - minVal)
dr, dg, db = [(maxVal-val) / d for val in (r, g, b)]
if r==maxVal:
h = db - dg
elif g==maxVal:
h = 2.0 + dr - db
else:
h = 4.0 + dg - dr
h = (h*60.0) % 360.0
return (h, s, l)
@staticmethod
def _HueToRgb(n1, n2, h):
h %= 6.0
if h < 1.0: return n1 + ((n2-n1) * h)
if h < 3.0: return n2
if h < 4.0: return n1 + ((n2-n1) * (4.0 - h))
return n1
@staticmethod
def HslToRgb(h, s, l):
'''Convert the color from HSL coordinates to RGB.
Parameters:
:h:
The Hue component value [0...1]
:s:
The Saturation component value [0...1]
:l:
The Lightness component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
>>> Color.HslToRgb(30.0, 1.0, 0.5)
(1.0, 0.5, 0.0)
'''
if s==0: return (l, l, l) # achromatic (gray)
if l<0.5: n2 = l * (1.0 + s)
else: n2 = l+s - (l*s)
n1 = (2.0 * l) - n2
h /= 60.0
hueToRgb = Color._HueToRgb
r = hueToRgb(n1, n2, h + 2)
g = hueToRgb(n1, n2, h)
b = hueToRgb(n1, n2, h - 2)
return (r, g, b)
@staticmethod
def RgbToHsv(r, g, b):
'''Convert the color from RGB coordinates to HSV.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (h, s, v) tuple in the range:
h[0...360],
s[0...1],
v[0...1]
>>> Color.RgbToHsv(1, 0.5, 0)
(30.0, 1, 1)
'''
v = max(r, g, b)
d = v - min(r, g, b)
if d==0: return (0.0, 0.0, v)
s = d / v
dr, dg, db = [(v - val) / d for val in (r, g, b)]
if r==v:
h = db - dg # between yellow & magenta
elif g==v:
h = 2.0 + dr - db # between cyan & yellow
else: # b==v
h = 4.0 + dg - dr # between magenta & cyan
h = (h*60.0) % 360.0
return (h, s, v)
@staticmethod
def HsvToRgb(h, s, v):
'''Convert the color from RGB coordinates to HSV.
Parameters:
:h:
The Hus component value [0...1]
:s:
The Saturation component value [0...1]
:v:
The Value component [0...1]
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
>>> Color.HslToRgb(30.0, 1.0, 0.5)
(1.0, 0.5, 0.0)
'''
if s==0: return (v, v, v) # achromatic (gray)
h /= 60.0
h = h % 6.0
i = int(h)
f = h - i
if not(i&1): f = 1-f # if i is even
m = v * (1.0 - s)
n = v * (1.0 - (s * f))
if i==0: return (v, n, m)
if i==1: return (n, v, m)
if i==2: return (m, v, n)
if i==3: return (m, n, v)
if i==4: return (n, m, v)
return (v, m, n)
@staticmethod
def RgbToYiq(r, g, b):
'''Convert the color from RGB to YIQ.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (y, i, q) tuple in the range:
y[0...1],
i[0...1],
q[0...1]
>>> '(%g, %g, %g)' % Color.RgbToYiq(1, 0.5, 0)
'(0.592263, 0.458874, -0.0499818)'
'''
y = (r * 0.29895808) + (g * 0.58660979) + (b *0.11443213)
i = (r * 0.59590296) - (g * 0.27405705) - (b *0.32184591)
q = (r * 0.21133576) - (g * 0.52263517) + (b *0.31129940)
return (y, i, q)
@staticmethod
def YiqToRgb(y, i, q):
'''Convert the color from YIQ coordinates to RGB.
Parameters:
:y:
Tte Y component value [0...1]
:i:
The I component value [0...1]
:q:
The Q component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
>>> '(%g, %g, %g)' % Color.YiqToRgb(0.592263, 0.458874, -0.0499818)
'(1, 0.5, 5.442e-007)'
'''
r = y + (i * 0.9562) + (q * 0.6210)
g = y - (i * 0.2717) - (q * 0.6485)
b = y - (i * 1.1053) + (q * 1.7020)
return (r, g, b)
@staticmethod
def RgbToYuv(r, g, b):
'''Convert the color from RGB coordinates to YUV.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (y, u, v) tuple in the range:
y[0...1],
u[-0.436...0.436],
v[-0.615...0.615]
>>> '(%g, %g, %g)' % Color.RgbToYuv(1, 0.5, 0)
'(0.5925, -0.29156, 0.357505)'
'''
y = (r * 0.29900) + (g * 0.58700) + (b * 0.11400)
u = -(r * 0.14713) - (g * 0.28886) + (b * 0.43600)
v = (r * 0.61500) - (g * 0.51499) - (b * 0.10001)
return (y, u, v)
@staticmethod
def YuvToRgb(y, u, v):
'''Convert the color from YUV coordinates to RGB.
Parameters:
:y:
The Y component value [0...1]
:u:
The U component value [-0.436...0.436]
:v:
The V component value [-0.615...0.615]
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
>>> '(%g, %g, %g)' % Color.YuvToRgb(0.5925, -0.2916, 0.3575)
'(0.999989, 0.500015, -6.3276e-005)'
'''
r = y + (v * 1.13983)
g = y - (u * 0.39465) - (v * 0.58060)
b = y + (u * 2.03211)
return (r, g, b)
@staticmethod
def RgbToXyz(r, g, b):
'''Convert the color from sRGB to CIE XYZ.
The methods assumes that the RGB coordinates are given in the sRGB
colorspace (D65).
.. note::
Compensation for the sRGB gamma correction is applied before converting.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (x, y, z) tuple in the range:
x[0...1],
y[0...1],
z[0...1]
>>> '(%g, %g, %g)' % Color.RgbToXyz(1, 0.5, 0)
'(0.488941, 0.365682, 0.0448137)'
'''
r, g, b = [((v <= 0.03928) and [v / 12.92] or [((v+0.055) / 1.055) **2.4])[0] for v in (r, g, b)]
x = (r * 0.4124) + (g * 0.3576) + (b * 0.1805)
y = (r * 0.2126) + (g * 0.7152) + (b * 0.0722)
z = (r * 0.0193) + (g * 0.1192) + (b * 0.9505)
return (x, y, z)
@staticmethod
def XyzToRgb(x, y, z):
'''Convert the color from CIE XYZ coordinates to sRGB.
.. note::
Compensation for sRGB gamma correction is applied before converting.
Parameters:
:x:
The X component value [0...1]
:y:
The Y component value [0...1]
:z:
The Z component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
>>> '(%g, %g, %g)' % Color.XyzToRgb(0.488941, 0.365682, 0.0448137)
'(1, 0.5, 6.81883e-008)'
'''
r = (x * 3.2406255) - (y * 1.5372080) - (z * 0.4986286)
g = -(x * 0.9689307) + (y * 1.8757561) + (z * 0.0415175)
b = (x * 0.0557101) - (y * 0.2040211) + (z * 1.0569959)
return tuple((((v <= _srgbGammaCorrInv) and [v * 12.92] or [(1.055 * (v ** (1/2.4))) - 0.055])[0] for v in (r, g, b)))
@staticmethod
def XyzToLab(x, y, z, wref=_DEFAULT_WREF):
'''Convert the color from CIE XYZ to CIE L*a*b*.
Parameters:
:x:
The X component value [0...1]
:y:
The Y component value [0...1]
:z:
The Z component value [0...1]
:wref:
The whitepoint reference, default is 2° D65.
Returns:
The color as an (L, a, b) tuple in the range:
L[0...100],
a[-1...1],
b[-1...1]
>>> '(%g, %g, %g)' % Color.XyzToLab(0.488941, 0.365682, 0.0448137)
'(66.9518, 0.43084, 0.739692)'
>>> '(%g, %g, %g)' % Color.XyzToLab(0.488941, 0.365682, 0.0448137, Color.WHITE_REFERENCE['std_D50'])
'(66.9518, 0.411663, 0.67282)'
'''
# White point correction
x /= wref[0]
y /= wref[1]
z /= wref[2]
# Nonlinear distortion and linear transformation
x, y, z = [((v > 0.008856) and [v**_oneThird] or [(7.787 * v) + _sixteenHundredsixteenth])[0] for v in (x, y, z)]
# Vector scaling
l = (116 * y) - 16
a = 5.0 * (x - y)
b = 2.0 * (y - z)
return (l, a, b)
@staticmethod
def LabToXyz(l, a, b, wref=_DEFAULT_WREF):
'''Convert the color from CIE L*a*b* to CIE 1931 XYZ.
Parameters:
:l:
The L component [0...100]
:a:
The a component [-1...1]
:b:
The a component [-1...1]
:wref:
The whitepoint reference, default is 2° D65.
Returns:
The color as an (x, y, z) tuple in the range:
x[0...q],
y[0...1],
z[0...1]
>>> '(%g, %g, %g)' % Color.LabToXyz(66.9518, 0.43084, 0.739692)
'(0.488941, 0.365682, 0.0448137)'
>>> '(%g, %g, %g)' % Color.LabToXyz(66.9518, 0.411663, 0.67282, Color.WHITE_REFERENCE['std_D50'])
'(0.488941, 0.365682, 0.0448138)'
'''
y = (l + 16) / 116
x = (a / 5.0) + y
z = y - (b / 2.0)
return tuple((((v > 0.206893) and [v**3] or [(v - _sixteenHundredsixteenth) / 7.787])[0] * w for v, w in zip((x, y, z), wref)))
@staticmethod
def CmykToCmy(c, m, y, k):
'''Convert the color from CMYK coordinates to CMY.
Parameters:
:c:
The Cyan component value [0...1]
:m:
The Magenta component value [0...1]
:y:
The Yellow component value [0...1]
:k:
The Black component value [0...1]
Returns:
The color as an (c, m, y) tuple in the range:
c[0...1],
m[0...1],
y[0...1]
>>> '(%g, %g, %g)' % Color.CmykToCmy(1, 0.32, 0, 0.5)
'(1, 0.66, 0.5)'
'''
mk = 1-k
return ((c*mk + k), (m*mk + k), (y*mk + k))
@staticmethod
def CmyToCmyk(c, m, y):
'''Convert the color from CMY coordinates to CMYK.
Parameters:
:c:
The Cyan component value [0...1]
:m:
The Magenta component value [0...1]
:y:
The Yellow component value [0...1]
Returns:
The color as an (c, m, y, k) tuple in the range:
c[0...1],
m[0...1],
y[0...1],
k[0...1]
>>> '(%g, %g, %g, %g)' % Color.CmyToCmyk(1, 0.66, 0.5)
'(1, 0.32, 0, 0.5)'
'''
k = min(c, m, y)
if k==1.0: return (0.0, 0.0, 0.0, 1.0)
mk = 1-k
return ((c-k) / mk, (m-k) / mk, (y-k) / mk, k)
@staticmethod
def RgbToCmy(r, g, b):
'''Convert the color from RGB coordinates to CMY.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (c, m, y) tuple in the range:
c[0...1],
m[0...1],
y[0...1]
>>> Color.RgbToCmy(1, 0.5, 0)
(0, 0.5, 1)
'''
return (1-r, 1-g, 1-b)
@staticmethod
def CmyToRgb(c, m, y):
'''Convert the color from CMY coordinates to RGB.
Parameters:
:c:
The Cyan component value [0...1]
:m:
The Magenta component value [0...1]
:y:
The Yellow component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
>>> Color.CmyToRgb(0, 0.5, 1)
(1, 0.5, 0)
'''
return (1-c, 1-m, 1-y)
@staticmethod
def RgbToHtml(r, g, b):
'''Convert the color from (r, g, b) to #RRGGBB.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
A CSS string representation of this color (#RRGGBB).
>>> Color.RgbToHtml(1, 0.5, 0)
'#ff8000'
'''
return '#%02x%02x%02x' % tuple((min(round(v*255), 255) for v in (r, g, b)))
@staticmethod
def HtmlToRgb(html):
'''Convert the HTML color to (r, g, b).
Parameters:
:html:
the HTML definition of the color (#RRGGBB or #RGB or a color name).
Returns:
The color as an (r, g, b) tuple in the range:
r[0...1],
g[0...1],
b[0...1]
Throws:
:ValueError:
If html is neither a known color name or a hexadecimal RGB
representation.
>>> '(%g, %g, %g)' % Color.HtmlToRgb('#ff8000')
'(1, 0.501961, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('ff8000')
'(1, 0.501961, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('#f60')
'(1, 0.4, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('f60')
'(1, 0.4, 0)'
>>> '(%g, %g, %g)' % Color.HtmlToRgb('lemonchiffon')
'(1, 0.980392, 0.803922)'
'''
html = html.strip().lower()
if html[0]=='#':
html = html[1:]
elif Color.NAMED_COLOR.has_key(html):
html = Color.NAMED_COLOR[html][1:]
if len(html)==6:
rgb = html[:2], html[2:4], html[4:]
elif len(html)==3:
rgb = ['%c%c' % (v,v) for v in html]
else:
raise ValueError, 'input #%s is not in #RRGGBB format' % html
return tuple(((int(n, 16) / 255.0) for n in rgb))
@staticmethod
def RgbToPil(r, g, b):
'''Convert the color from RGB to a PIL-compatible integer.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
A PIL compatible integer (0xBBGGRR).
>>> '0x%06x' % Color.RgbToPil(1, 0.5, 0)
'0x0080ff'
'''
r, g, b = [min(int(round(v*255)), 255) for v in (r, g, b)]
return (b << 16) + (g << 8) + r
@staticmethod
def PilToRgb(pil):
'''Convert the color from a PIL-compatible integer to RGB.
Parameters:
pil: a PIL compatible color representation (0xBBGGRR)
Returns:
The color as an (r, g, b) tuple in the range:
the range:
r: [0...1]
g: [0...1]
b: [0...1]
>>> '(%g, %g, %g)' % Color.PilToRgb(0x0080ff)
'(1, 0.501961, 0)'
'''
r = 0xff & pil
g = 0xff & (pil >> 8)
b = 0xff & (pil >> 16)
return tuple((v / 255.0 for v in (r, g, b)))
@staticmethod
def _WebSafeComponent(c, alt=False):
'''Convert a color component to its web safe equivalent.
Parameters:
:c:
The component value [0...1]
:alt:
If True, return the alternative value instead of the nearest one.
Returns:
The web safe equivalent of the component value.
'''
# This sucks, but floating point between 0 and 1 is quite fuzzy...
# So we just change the scale a while to make the equality tests
# work, otherwise it gets wrong at some decimal far to the right.
sc = c * 100.0
# If the color is already safe, return it straight away
d = sc % 20
if d==0: return c
# Get the lower and upper safe values
l = sc - d
u = l + 20
# Return the 'closest' value according to the alt flag
if alt:
if (sc-l) >= (u-sc): return l/100.0
else: return u/100.0
else:
if (sc-l) >= (u-sc): return u/100.0
else: return l/100.0
@staticmethod
def RgbToWebSafe(r, g, b, alt=False):
'''Convert the color from RGB to 'web safe' RGB
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
:alt:
If True, use the alternative color instead of the nearest one.
Can be used for dithering.
Returns:
The color as an (r, g, b) tuple in the range:
the range:
r[0...1],
g[0...1],
b[0...1]
>>> '(%g, %g, %g)' % Color.RgbToWebSafe(1, 0.55, 0.0)
'(1, 0.6, 0)'
'''
webSafeComponent = Color._WebSafeComponent
return tuple((webSafeComponent(v, alt) for v in (r, g, b)))
@staticmethod
def RgbToGreyscale(r, g, b):
'''Convert the color from RGB to its greyscale equivalent
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
Returns:
The color as an (r, g, b) tuple in the range:
the range:
r[0...1],
g[0...1],
b[0...1]
>>> '(%g, %g, %g)' % Color.RgbToGreyscale(1, 0.8, 0)
'(0.6, 0.6, 0.6)'
'''
v = (r + g + b) / 3.0
return (v, v, v)
@staticmethod
def RgbToRyb(hue):
'''Maps a hue on the RGB color wheel to Itten's RYB wheel.
Parameters:
:hue:
The hue on the RGB color wheel [0...360]
Returns:
An approximation of the corresponding hue on Itten's RYB wheel.
>>> Color.RgbToRyb(15)
26
'''
d = hue % 15
i = int(hue / 15)
x0 = _RybWheel[i]
x1 = _RybWheel[i+1]
return x0 + (x1-x0) * d / 15
@staticmethod
def RybToRgb(hue):
'''Maps a hue on Itten's RYB color wheel to the standard RGB wheel.
Parameters:
:hue:
The hue on Itten's RYB color wheel [0...360]
Returns:
An approximation of the corresponding hue on the standard RGB wheel.
>>> Color.RybToRgb(15)
8
'''
d = hue % 15
i = int(hue / 15)
x0 = _RgbWheel[i]
x1 = _RgbWheel[i+1]
return x0 + (x1-x0) * d / 15
@staticmethod
def NewFromRgb(r, g, b, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed RGB values.
Parameters:
:r:
The Red component value [0...1]
:g:
The Green component value [0...1]
:b:
The Blue component value [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromRgb(1.0, 0.5, 0.0)
(1.0, 0.5, 0.0, 1.0)
>>> Color.NewFromRgb(1.0, 0.5, 0.0, 0.5)
(1.0, 0.5, 0.0, 0.5)
'''
return Color((r, g, b), 'rgb', alpha, wref)
@staticmethod
def NewFromHsl(h, s, l, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed HSL values.
Parameters:
:h:
The Hue component value [0...1]
:s:
The Saturation component value [0...1]
:l:
The Lightness component value [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5)
(1.0, 0.5, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5, 0.5)
(1.0, 0.5, 0.0, 0.5)
'''
return Color((h, s, l), 'hsl', alpha, wref)
@staticmethod
def NewFromHsv(h, s, v, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed HSV values.
Parameters:
:h:
The Hus component value [0...1]
:s:
The Saturation component value [0...1]
:v:
The Value component [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsv(30, 1, 1)
(1.0, 0.5, 0.0, 1.0)
>>> Color.NewFromHsv(30, 1, 1, 0.5)
(1.0, 0.5, 0.0, 0.5)
'''
h2, s, l = Color.RgbToHsl(*Color.HsvToRgb(h, s, v))
return Color((h, s, l), 'hsl', alpha, wref)
@staticmethod
def NewFromYiq(y, i, q, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed YIQ values.
Parameters:
:y:
The Y component value [0...1]
:i:
The I component value [0...1]
:q:
The Q component value [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromYiq(0.5922, 0.45885,-0.05))
'(0.999902, 0.499955, -6.6905e-005, 1)'
>>> str(Color.NewFromYiq(0.5922, 0.45885,-0.05, 0.5))
'(0.999902, 0.499955, -6.6905e-005, 0.5)'
'''
return Color(Color.YiqToRgb(y, i, q), 'rgb', alpha, wref)
@staticmethod
def NewFromYuv(y, u, v, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed YUV values.
Parameters:
:y:
The Y component value [0...1]
:u:
The U component value [-0.436...0.436]
:v:
The V component value [-0.615...0.615]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromYuv(0.5925, -0.2916, 0.3575))
'(0.999989, 0.500015, -6.3276e-005, 1)'
>>> str(Color.NewFromYuv(0.5925, -0.2916, 0.3575, 0.5))
'(0.999989, 0.500015, -6.3276e-005, 0.5)'
'''
return Color(Color.YuvToRgb(y, u, v), 'rgb', alpha, wref)
@staticmethod
def NewFromXyz(x, y, z, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed CIE-XYZ values.
Parameters:
:x:
The Red component value [0...1]
:y:
The Green component value [0...1]
:z:
The Blue component value [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromXyz(0.488941, 0.365682, 0.0448137))
'(1, 0.5, 6.81883e-008, 1)'
>>> str(Color.NewFromXyz(0.488941, 0.365682, 0.0448137, 0.5))
'(1, 0.5, 6.81883e-008, 0.5)'
'''
return Color(Color.XyzToRgb(x, y, z), 'rgb', alpha, wref)
@staticmethod
def NewFromLab(l, a, b, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed CIE-LAB values.
Parameters:
:l:
The L component [0...100]
:a:
The a component [-1...1]
:b:
The a component [-1...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692))
'(1, 0.5, 1.09491e-008, 1)'
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692, wref=Color.WHITE_REFERENCE['std_D50']))
'(1.01238, 0.492011, -0.14311, 1)'
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692, 0.5))
'(1, 0.5, 1.09491e-008, 0.5)'
>>> str(Color.NewFromLab(66.9518, 0.43084, 0.739692, 0.5, Color.WHITE_REFERENCE['std_D50']))
'(1.01238, 0.492011, -0.14311, 0.5)'
'''
return Color(Color.XyzToRgb(*Color.LabToXyz(l, a, b, wref)), 'rgb', alpha, wref)
@staticmethod
def NewFromCmy(c, m, y, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed CMY values.
Parameters:
:c:
The Cyan component value [0...1]
:m:
The Magenta component value [0...1]
:y:
The Yellow component value [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> Color.NewFromCmy(0, 0.5, 1)
(1, 0.5, 0, 1.0)
>>> Color.NewFromCmy(0, 0.5, 1, 0.5)
(1, 0.5, 0, 0.5)
'''
return Color(Color.CmyToRgb(c, m, y), 'rgb', alpha, wref)
@staticmethod
def NewFromCmyk(c, m, y, k, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed CMYK values.
Parameters:
:c:
The Cyan component value [0...1]
:m:
The Magenta component value [0...1]
:y:
The Yellow component value [0...1]
:k:
The Black component value [0...1]
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromCmyk(1, 0.32, 0, 0.5))
'(0, 0.34, 0.5, 1)'
>>> str(Color.NewFromCmyk(1, 0.32, 0, 0.5, 0.5))
'(0, 0.34, 0.5, 0.5)'
'''
return Color(Color.CmyToRgb(*Color.CmykToCmy(c, m, y, k)), 'rgb', alpha, wref)
@staticmethod
def NewFromHtml(html, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed HTML color definition.
Parameters:
:html:
The HTML definition of the color (#RRGGBB or #RGB or a color name).
:alpha:
The color transparency [0...1], default is opaque.
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromHtml('#ff8000'))
'(1, 0.501961, 0, 1)'
>>> str(Color.NewFromHtml('ff8000'))
'(1, 0.501961, 0, 1)'
>>> str(Color.NewFromHtml('#f60'))
'(1, 0.4, 0, 1)'
>>> str(Color.NewFromHtml('f60'))
'(1, 0.4, 0, 1)'
>>> str(Color.NewFromHtml('lemonchiffon'))
'(1, 0.980392, 0.803922, 1)'
>>> str(Color.NewFromHtml('#ff8000', 0.5))
'(1, 0.501961, 0, 0.5)'
'''
return Color(Color.HtmlToRgb(html), 'rgb', alpha, wref)
@staticmethod
def NewFromPil(pil, alpha=1.0, wref=_DEFAULT_WREF):
'''Create a new instance based on the specifed PIL color.
Parameters:
:pil:
A PIL compatible color representation (0xBBGGRR)
:alpha:
The color transparency [0...1], default is opaque
:wref:
The whitepoint reference, default is 2° D65.
Returns:
A grapefruit.Color instance.
>>> str(Color.NewFromPil(0x0080ff))
'(1, 0.501961, 0, 1)'
>>> str(Color.NewFromPil(0x0080ff, 0.5))
'(1, 0.501961, 0, 0.5)'
'''
return Color(Color.PilToRgb(pil), 'rgb', alpha, wref)
def __GetAlpha(self):
return self.__a
alpha = property(fget=__GetAlpha, doc='The transparency of this color. 0.0 is transparent and 1.0 is fully opaque.')
def __GetWRef(self):
return self.__wref
whiteRef = property(fget=__GetWRef, doc='the white reference point of this color.')
def __GetRGB(self):
return self.__rgb
rgb = property(fget=__GetRGB, doc='The RGB values of this Color.')
def __GetHue(self):
return self.__hsl[0]
hue = property(fget=__GetHue, doc='The hue of this color.')
def __GetHSL(self):
return self.__hsl
hsl = property(fget=__GetHSL, doc='The HSL values of this Color.')
def __GetHSV(self):
h, s, v = Color.RgbToHsv(*self.__rgb)
return (self.__hsl[0], s, v)
hsv = property(fget=__GetHSV, doc='The HSV values of this Color.')
def __GetYIQ(self):
return Color.RgbToYiq(*self.__rgb)
yiq = property(fget=__GetYIQ, doc='The YIQ values of this Color.')
def __GetYUV(self):
return Color.RgbToYuv(*self.__rgb)
yuv = property(fget=__GetYUV, doc='The YUV values of this Color.')
def __GetXYZ(self):
return Color.RgbToXyz(*self.__rgb)
xyz = property(fget=__GetXYZ, doc='The CIE-XYZ values of this Color.')
def __GetLAB(self):
return Color.XyzToLab(wref=self.__wref, *Color.RgbToXyz(*self.__rgb))
lab = property(fget=__GetLAB, doc='The CIE-LAB values of this Color.')
def __GetCMY(self):
return Color.RgbToCmy(*self.__rgb)
cmy = property(fget=__GetCMY, doc='The CMY values of this Color.')
def __GetCMYK(self):
return Color.CmyToCmyk(*Color.RgbToCmy(*self.__rgb))
cmyk = property(fget=__GetCMYK, doc='The CMYK values of this Color.')
def __GetHTML(self):
return Color.RgbToHtml(*self.__rgb)
html = property(fget=__GetHTML, doc='This Color as an HTML color definition.')
def __GetPIL(self):
return Color.RgbToPil(*self.__rgb)
pil = property(fget=__GetPIL, doc='This Color as a PIL compatible value.')
def __GetwebSafe(self):
return Color.RgbToWebSafe(*self.__rgb)
webSafe = property(fget=__GetwebSafe, doc='The web safe color nearest to this one (RGB).')
def __GetGreyscale(self):
return Color.RgbToGreyscale(*self.rgb)
greyscale = property(fget=__GetGreyscale, doc='The greyscale equivalent to this color (RGB).')
def ColorWithAlpha(self, alpha):
'''Create a new instance based on this one with a new alpha value.
Parameters:
:alpha:
The transparency of the new color [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromRgb(1.0, 0.5, 0.0, 1.0).ColorWithAlpha(0.5)
(1.0, 0.5, 0.0, 0.5)
'''
return Color(self.__rgb, 'rgb', alpha, self.__wref)
def ColorWithWhiteRef(self, wref, labAsRef=False):
'''Create a new instance based on this one with a new white reference.
Parameters:
:wref:
The whitepoint reference.
:labAsRef:
If True, the L*a*b* values of the current instance are used as reference
for the new color; otherwise, the RGB values are used as reference.
Returns:
A grapefruit.Color instance.
>>> c = Color.NewFromRgb(1.0, 0.5, 0.0, 1.0, Color.WHITE_REFERENCE['std_D65'])
>>> c2 = c.ColorWithWhiteRef(Color.WHITE_REFERENCE['sup_D50'])
>>> c2.rgb
(1.0, 0.5, 0.0)
>>> '(%g, %g, %g)' % c2.whiteRef
'(0.96721, 1, 0.81428)'
>>> c2 = c.ColorWithWhiteRef(Color.WHITE_REFERENCE['sup_D50'], labAsRef=True)
>>> '(%g, %g, %g)' % c2.rgb
'(1.01463, 0.490339, -0.148131)'
>>> '(%g, %g, %g)' % c2.whiteRef
'(0.96721, 1, 0.81428)'
>>> '(%g, %g, %g)' % c.lab
'(66.9518, 0.43084, 0.739692)'
>>> '(%g, %g, %g)' % c2.lab
'(66.9518, 0.43084, 0.739693)'
'''
if labAsRef:
l, a, b = self.__GetLAB()
return Color.NewFromLab(l, a, b, self.__a, wref)
else:
return Color(self.__rgb, 'rgb', self.__a, wref)
def ColorWithHue(self, hue):
'''Create a new instance based on this one with a new hue.
Parameters:
:hue:
The hue of the new color [0...360].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithHue(60)
(1.0, 1.0, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithHue(60).hsl
(60, 1, 0.5)
'''
h, s, l = self.__hsl
return Color((hue, s, l), 'hsl', self.__a, self.__wref)
def ColorWithSaturation(self, saturation):
'''Create a new instance based on this one with a new saturation value.
.. note::
The saturation is defined for the HSL mode.
Parameters:
:saturation:
The saturation of the new color [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithSaturation(0.5)
(0.75, 0.5, 0.25, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithSaturation(0.5).hsl
(30, 0.5, 0.5)
'''
h, s, l = self.__hsl
return Color((h, saturation, l), 'hsl', self.__a, self.__wref)
def ColorWithLightness(self, lightness):
'''Create a new instance based on this one with a new lightness value.
Parameters:
:lightness:
The lightness of the new color [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithLightness(0.25)
(0.5, 0.25, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ColorWithLightness(0.25).hsl
(30, 1, 0.25)
'''
h, s, l = self.__hsl
return Color((h, s, lightness), 'hsl', self.__a, self.__wref)
def DarkerColor(self, level):
'''Create a new instance based on this one but darker.
Parameters:
:level:
The amount by which the color should be darkened to produce
the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).DarkerColor(0.25)
(0.5, 0.25, 0.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).DarkerColor(0.25).hsl
(30, 1, 0.25)
'''
h, s, l = self.__hsl
return Color((h, s, max(l - level, 0)), 'hsl', self.__a, self.__wref)
def LighterColor(self, level):
'''Create a new instance based on this one but lighter.
Parameters:
:level:
The amount by which the color should be lightened to produce
the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).LighterColor(0.25)
(1.0, 0.75, 0.5, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).LighterColor(0.25).hsl
(30, 1, 0.75)
'''
h, s, l = self.__hsl
return Color((h, s, min(l + level, 1)), 'hsl', self.__a, self.__wref)
def Saturate(self, level):
'''Create a new instance based on this one but more saturated.
Parameters:
:level:
The amount by which the color should be saturated to produce
the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 0.5, 0.5).Saturate(0.25)
(0.875, 0.5, 0.125, 1.0)
>>> Color.NewFromHsl(30, 0.5, 0.5).Saturate(0.25).hsl
(30, 0.75, 0.5)
'''
h, s, l = self.__hsl
return Color((h, min(s + level, 1), l), 'hsl', self.__a, self.__wref)
def Desaturate(self, level):
'''Create a new instance based on this one but less saturated.
Parameters:
:level:
The amount by which the color should be desaturated to produce
the new one [0...1].
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 0.5, 0.5).Desaturate(0.25)
(0.625, 0.5, 0.375, 1.0)
>>> Color.NewFromHsl(30, 0.5, 0.5).Desaturate(0.25).hsl
(30, 0.25, 0.5)
'''
h, s, l = self.__hsl
return Color((h, max(s - level, 0), l), 'hsl', self.__a, self.__wref)
def WebSafeDither(self):
'''Return the two websafe colors nearest to this one.
Returns:
A tuple of two grapefruit.Color instances which are the two
web safe colors closest this one.
>>> c = Color.NewFromRgb(1.0, 0.45, 0.0)
>>> c1, c2 = c.WebSafeDither()
>>> str(c1)
'(1, 0.4, 0, 1)'
>>> str(c2)
'(1, 0.6, 0, 1)'
'''
return (
Color(Color.RgbToWebSafe(*self.__rgb), 'rgb', self.__a, self.__wref),
Color(Color.RgbToWebSafe(alt=True, *self.__rgb), 'rgb', self.__a, self.__wref))
def Gradient(self, target, steps=100):
'''Create a list with the gradient colors between this and the other color.
Parameters:
:target:
The grapefruit.Color at the other end of the gradient.
:steps:
The number of gradients steps to create.
Returns:
A list of grapefruit.Color instances.
>>> c1 = Color.NewFromRgb(1.0, 0.0, 0.0, alpha=1)
>>> c2 = Color.NewFromRgb(0.0, 1.0, 0.0, alpha=0)
>>> c1.Gradient(c2, 3)
[(0.75, 0.25, 0.0, 0.75), (0.5, 0.5, 0.0, 0.5), (0.25, 0.75, 0.0, 0.25)]
'''
gradient = []
rgba1 = self.__rgb + (self.__a,)
rgba2 = target.__rgb + (target.__a,)
steps += 1
for n in xrange(1, steps):
d = 1.0*n/steps
r = (rgba1[0]*(1-d)) + (rgba2[0]*d)
g = (rgba1[1]*(1-d)) + (rgba2[1]*d)
b = (rgba1[2]*(1-d)) + (rgba2[2]*d)
a = (rgba1[3]*(1-d)) + (rgba2[3]*d)
gradient.append(Color((r, g, b), 'rgb', a, self.__wref))
return gradient
def ComplementaryColor(self, mode='ryb'):
'''Create a new instance which is the complementary color of this one.
Parameters:
:mode:
Select which color wheel to use for the generation (ryb/rgb).
Returns:
A grapefruit.Color instance.
>>> Color.NewFromHsl(30, 1, 0.5).ComplementaryColor()
(0.0, 0.5, 1.0, 1.0)
>>> Color.NewFromHsl(30, 1, 0.5).ComplementaryColor().hsl
(210, 1, 0.5)
'''
h, s, l = self.__hsl
if mode == 'ryb': h = Color.RgbToRyb(h)
h = (h+180)%360
if mode == 'ryb': h = Color.RybToRgb(h)
return Color((h, s, l), 'hsl', self.__a, self.__wref)
def MonochromeScheme(self):
'''Return 4 colors in the same hue with varying saturation/lightness.
Returns:
A tuple of 4 grapefruit.Color in the same hue as this one,
with varying saturation/lightness.
>>> c = Color.NewFromHsl(30, 0.5, 0.5)
>>> ['(%g, %g, %g)' % clr.hsl for clr in c.MonochromeScheme()]
['(30, 0.2, 0.8)', '(30, 0.5, 0.3)', '(30, 0.2, 0.6)', '(30, 0.5, 0.8)']
'''
def _wrap(x, min, thres, plus):
if (x-min) < thres: return x + plus
else: return x-min
h, s, l = self.__hsl
s1 = _wrap(s, 0.3, 0.1, 0.3)
l1 = _wrap(l, 0.5, 0.2, 0.3)
s2 = s
l2 = _wrap(l, 0.2, 0.2, 0.6)
s3 = s1
l3 = max(0.2, l + (1-l)*0.2)
s4 = s
l4 = _wrap(l, 0.5, 0.2, 0.3)
return (
Color((h, s1, l1), 'hsl', self.__a, self.__wref),
Color((h, s2, l2), 'hsl', self.__a, self.__wref),
Color((h, s3, l3), 'hsl', self.__a, self.__wref),
Color((h, s4, l4), 'hsl', self.__a, self.__wref))
def TriadicScheme(self, angle=120, mode='ryb'):
'''Return two colors forming a triad or a split complementary with this one.
Parameters:
:angle:
The angle between the hues of the created colors.
The default value makes a triad.
:mode:
Select which color wheel to use for the generation (ryb/rgb).
Returns:
A tuple of two grapefruit.Color forming a color triad with
this one or a split complementary.
>>> c1 = Color.NewFromHsl(30, 1, 0.5)
>>> c2, c3 = c1.TriadicScheme()
>>> c2.hsl
(150.0, 1, 0.5)
>>> c3.hsl
(270.0, 1, 0.5)
>>> c2, c3 = c1.TriadicScheme(40)
>>> c2.hsl
(190.0, 1, 0.5)
>>> c3.hsl
(230.0, 1, 0.5)
'''
h, s, l = self.__hsl
angle = min(angle, 120) / 2.0
if mode == 'ryb': h = Color.RgbToRyb(h)
h += 180
h1 = (h - angle) % 360
h2 = (h + angle) % 360
if mode == 'ryb':
h1 = Color.RybToRgb(h1)
h2 = Color.RybToRgb(h2)
return (
Color((h1, s, l), 'hsl', self.__a, self.__wref),
Color((h2, s, l), 'hsl', self.__a, self.__wref))
def TetradicScheme(self, angle=30, mode='ryb'):
'''Return three colors froming a tetrad with this one.
Parameters:
:angle:
The angle to substract from the adjacent colors hues [-90...90].
You can use an angle of zero to generate a square tetrad.
:mode:
Select which color wheel to use for the generation (ryb/rgb).
Returns:
A tuple of three grapefruit.Color forming a color tetrad with
this one.
>>> col = Color.NewFromHsl(30, 1, 0.5)
>>> [c.hsl for c in col.TetradicScheme(mode='rgb', angle=30)]
[(90, 1, 0.5), (210, 1, 0.5), (270, 1, 0.5)]
'''
h, s, l = self.__hsl
if mode == 'ryb': h = Color.RgbToRyb(h)
h1 = (h + 90 - angle) % 360
h2 = (h + 180) % 360
h3 = (h + 270 - angle) % 360
if mode == 'ryb':
h1 = Color.RybToRgb(h1)
h2 = Color.RybToRgb(h2)
h3 = Color.RybToRgb(h3)
return (
Color((h1, s, l), 'hsl', self.__a, self.__wref),
Color((h2, s, l), 'hsl', self.__a, self.__wref),
Color((h3, s, l), 'hsl', self.__a, self.__wref))
def AnalogousScheme(self, angle=30, mode='ryb'):
'''Return two colors analogous to this one.
Args:
:angle:
The angle between the hues of the created colors and this one.
:mode:
Select which color wheel to use for the generation (ryb/rgb).
Returns:
A tuple of grapefruit.Colors analogous to this one.
>>> c1 = Color.NewFromHsl(30, 1, 0.5)
>>> c2, c3 = c1.AnalogousScheme()
>>> c2.hsl
(330, 1, 0.5)
>>> c3.hsl
(90, 1, 0.5)
>>> c2, c3 = c1.AnalogousScheme(10)
>>> c2.hsl
(20, 1, 0.5)
>>> c3.hsl
(40, 1, 0.5)
'''
h, s, l = self.__hsl
if mode == 'ryb': h = Color.RgbToRyb(h)
h += 360
h1 = (h - angle) % 360
h2 = (h + angle) % 360
if mode == 'ryb':
h1 = Color.RybToRgb(h1)
h2 = Color.RybToRgb(h2)
return (Color((h1, s, l), 'hsl', self.__a, self.__wref),
Color((h2, s, l), 'hsl', self.__a, self.__wref))
def AlphaBlend(self, other):
'''Alpha-blend this color on the other one.
Args:
:other:
The grapefruit.Color to alpha-blend with this one.
Returns:
A grapefruit.Color instance which is the result of alpha-blending
this color on the other one.
>>> c1 = Color.NewFromRgb(1, 0.5, 0, 0.2)
>>> c2 = Color.NewFromRgb(1, 1, 1, 0.8)
>>> c3 = c1.AlphaBlend(c2)
>>> str(c3)
'(1, 0.875, 0.75, 0.84)'
'''
# get final alpha channel
fa = self.__a + other.__a - (self.__a * other.__a)
# get percentage of source alpha compared to final alpha
if fa==0: sa = 0
else: sa = min(1.0, self.__a/other.__a)
# destination percentage is just the additive inverse
da = 1.0 - sa
sr, sg, sb = [v * sa for v in self.__rgb]
dr, dg, db = [v * da for v in other.__rgb]
return Color((sr+dr, sg+dg, sb+db), 'rgb', fa, self.__wref)
def Blend(self, other, percent=0.5):
'''Blend this color with the other one.
Args:
:other:
the grapefruit.Color to blend with this one.
Returns:
A grapefruit.Color instance which is the result of blending
this color on the other one.
>>> c1 = Color.NewFromRgb(1, 0.5, 0, 0.2)
>>> c2 = Color.NewFromRgb(1, 1, 1, 0.6)
>>> c3 = c1.Blend(c2)
>>> str(c3)
'(1, 0.75, 0.5, 0.4)'
'''
dest = 1.0 - percent
rgb = tuple(((u * percent) + (v * dest) for u, v in zip(self.__rgb, other.__rgb)))
a = (self.__a * percent) + (other.__a * dest)
return Color(rgb, 'rgb', a, self.__wref)
def _test():
import doctest
reload(doctest)
doctest.testmod()
if __name__=='__main__':
_test()
|