-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolour.py
1334 lines (1133 loc) · 42.5 KB
/
colour.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
"""
colour
Bart Nagel <[email protected]>
https://github.com/tremby/py-colour
licensed under the Gnu GPL version 3
Originally ported from Colour.php, a colour manipulation class for PHP by the
same author, but now quite different.
Provides a Colour class and various supporting functions, plus the list of CSS3
named colours.
Colours are internally stored as float RGB values. RGB values are the
intensities of the red, green and blue channels.
RGB
---
Conceptually RGB colour space can be thought of as a cube along whose three axes
the intensities of red, green and blue increase, respectively. The best way to
think of the RGB colour cube is balancing on its point, so the black corner as
at the bottom and the white corner is at the top. Shades of grey are then in a
column up the middle. Colours get more intense as we move upwards through the
cube.
The RGB color cube is not so useful for thinking about and transforming colours.
For that we can more intuitively use other models.
HSV
---
The HSV model is best thought of as the RGB colour cube on its point but
deformed so that the middle six corners (red, yellow, green, cyan, blue,
magenta) are all in the same horizontal plane as the top corner (white). This
makes a hexagonal cone. Just like the RGB cube, black is still at the point at
the bottom, and shades of grey are still in a column in the centre. It's easiest
to think of it then as stretched out sideways into a cylinder, where the whole
bottom face is black and the top face has bright colours around the edge, all
fading to white at a point in its centre.
All the primary and secondary colours, then, are on the circumference of the top
face of the cylinder. The angle from the red primary, in the direction of
yellow, is called the hue (the H in HSV) and is measured in degrees.
As we move inwards from the circumference of the cylinder towards the central
vertical axis, colour is lost. The distance outwards from grey to the colour is
called the saturation (the S in HSV) -- 0 at grey and 1 at the edge of the
cylinder.
Moving upwards from the bottom face gives lighter colours, but since we could be
at the top and not be at white (white is only in the centre of the top), the
vertical axis can't very well be called "lightness". So the distance upwards is
called the value (the V in HSV).
HSL
---
The HSL model is similar to HSV but instead of moving the primaries and
secondaries up to the same horizontal plane as white, the primaries are moved up
a little and the secondaries down a little so that they are all in a horizontal
plane half way up the model. This results in a double hexagonal cone. Stretch
this out into a cylinder again and we have the HSL model. The bottom face is
still all black and now the top face is all white.
Hue is the same as in HSV, but obviously the primaries and secondaries are now
in a plane halfway up the model rather than at the top.
Saturation is similar to in the HSV model -- it is still the distance from the
central vertical axis to the colour -- but most colours will have different
saturation values in the HSV and HSL models. The difference is particularly
large for very light colours, since that is where the two models are most
different.
The distance up the vertical axis running from black to white is then called
lightness (the L in HSL), and now it is a fitting name since we will always end
up with white if we increase it enough.
YIQ
---
The YIQ model is mostly used in broadcasting, particularly in the NTSC standard.
Its lightness metric is called luma and has the abbreviation Y. This is useful
to have in broadcasting since it is all the information a black and white TV set
needs. It is called luma rather than lightness since it measures the perceived
lightness of the colour rather than an intensity based on the actual amounts of
light. This is important since the eye perceives the same amount of light of
different colours as different lightnesses -- for instance, we see #0f0 (the
green primary) as being much brighter than #00f (the blue primary), though both
have the same lightness according to the metrics of RGB (average intensity), HSV
(value) and HSL (lightness). In the YIQ model, the green has a much higher luma
than the blue.
Luma is calculated by weighting the red, green and blue channels by finely tuned
coefficients and then summing.
Colour information, then (called chrominance), is in the other two channels, I
and Q. (I and Q stand for "in-phase" and "quadrature", which have something to
do with how they are modulated for broadcast transmission.) These are simply two
axes of colour space. With luma at 0.5 the IQ plane would look like a plane
halfway up the HSL cylinder (grey in the centre, colours towards the edges) but
rotated somewhat and with all points appearing to be at the same lightness. (In
the HSL plane some parts would look lighter than others, such as yellow looking
brighter than blue).
"""
NAME = "py-colour"
DESCRIPTION = "Colour manipulation class"
AUTHOR = "Bart Nagel"
AUTHOR_EMAIL = "[email protected]"
URL = "https://github.com/tremby/py-colour"
VERSION = "1.1.0"
LICENSE = "GNU GPLv3"
COPYRIGHT_YEAR = "2011~2017"
import colorsys
import re
import hashlib
from six import string_types
class Colour:
"""
Colours can be made from CSS3 named colours, hex strings, RGB values, HSV
values, HSL values, YIQ values, lightness (for shades of grey), arbitrary
bits of data (through hashing their string representations) or copied from
other Colour objects.
They can be manipulated in various ways -- setting or shifting luma, hue,
value, saturation and so on, or mixing with other colours.
Colours can be output as hex strings, 3-tuples of RGB, HSV, HSL or YIQ
values and HTML colour swatches.
Very basic examples:
# Import the library
from colour import Colour
# Print a hex representation of a CSS3 named colour
print(Colour("red"))
# To produce modified versions of a base colour (in this case shifted in
# lightness towards black or white) we can make a start Colour object
green = Colour("#006310")
# ...then make modified copies by putting the start colour through the
# constructor and then calling one of its colour modification methods
darkgreen = Colour(green).shiftluma(-0.5)
lightgreen = Colour(green).shiftluma(0.5)
# If we hadn't used the constructor to make copies the original Colour
# object would have been modified.
# Instead of shifting by a proprtion we can produce shades of a colour
# on an absolute scale
verydarkgreen = Colour(green).luma(0.1)
verylightgreen = Colour(green).luma(0.9)
# Produce a rainbow by repeatedly shifting the hue of a Colour object
c = Colour(hsv=(0, 0.8, 0.6))
for x in range(12):
print(c.shifthue(30))
# A similar rainbow with uniform brightness (with copies of the Colour
# object rather than by modifying the original)
c = Colour(hsv=(0, 0.8, 0.6))
for x in range(12):
print(Colour(c).shifthue(x * 30, perceptual=True))
# An almost-grey version of our green from earlier
almostgrey = Colour(green).saturation_hsv(0.2)
# Another almost-grey version, but this time with a perceptually similar
# level of lightness
almostgrey2 = Colour(green).saturation_hsv(0.2, perceptual=True)
# Make a set of colours to colour-code some usernames
usernames = ["tremby", "yappy", "mon", "bill"]
for username in usernames:
print("<li style=\"color: %s\">%s</li>" \\
% (Colour(hash=username), username))
# The same list of usernames but only allowing reds and oranges
for username in usernames:
print("<li style=\"color: %s\">%s</li>" \\
% (Colour().hash(username, minh=0, maxh=30), username))
To test the class and see lots more examples you can use the test.py script
(distributed with this module) which outputs HTML, then view the result in
your browser:
python test.py >/tmp/colour.html
x-www-browser /tmp/colour.html
"""
__colour = (0.0, 0.0, 0.0)
def __init__(self, arg=None,
grey=None,
rgb=None, rgb100=None, rgb255=None,
hsv=None, hsv100=None, hsv255=None,
hsl=None, hsl100=None, hsl255=None,
yiq=None,
hex=None, css3=None, hash=None,
colour=None):
"""
Constructor
This can be called without any arguments, in which case the colour is
set to black.
If the first argument is used its type and value are used to choose the
action to take. It can be
a float or integer in the range 0~1
act as if the grey argument was used
a 3-tuple
act as if the rgb argument was used
a valid hex RGB string
act as if the hex argument was used
a string corresponding with a CSS3 named colour
act as if the css3 argument was used
a Colour object
act as if the colour argument was used
Otherwise exactly one of the other arguments must be used.
grey=i
Set the colour to a grey of this intensity (in the range 0~1).
rgb=(r, g, b), rgb100=(r, g, b), rgb255=(r, g, b)
Set colour to these RGB values (in the range 0~1, 0~100 or 0~255
depending on the argument used).
hsv=(h, s, v), hsv100=(h, s, v), hsv255=(h, s, v)
hsl=(h, s, l), hsl100=(h, s, l), hsl255=(h, s, l)
Set the colour to these HSV or HSL values (h in degrees, s and v
in the range 0~1, 0~100 or 0~255 depending on the argument
used).
yiq=(y, i, q)
Set the colour to these YIQ values (y in the range 0~1, i and q
in the range -1~1).
hex=string
Set colour to this hex representation of an RGB colour. See the
hex() method for what is accepted.
css3=string
Set colour to the CSS3 named colour of this name.
hash=something
Convert whatever was passed to a string, hash it and make a
colour from the result.
colour=colour_object
Set colour to match the given Colour object
"""
# ensure there is at maximum one non-None argument
if sum((arg is not None,
grey is not None,
rgb is not None, rgb100 is not None, rgb255 is not None,
hsv is not None, hsv100 is not None, hsv255 is not None,
hsl is not None, hsl100 is not None, hsl255 is not None,
yiq is not None,
hex is not None, css3 is not None, hash is not None,
colour is not None,
)) > 1:
raise ValueError("expected at most one non-None argument")
if arg is not None:
# determine what is meant by looking at the type and value
try:
if arg >= 0 and arg <= 1:
self.grey(arg)
return
except TypeError:
pass
if _is_sequence(arg) and len(arg) == 3:
self.rgb(arg)
return
if _validhex(arg):
self.hex(arg)
return
if isinstance(arg, string_types):
self.css3(arg)
return
if isinstance(arg, self.__class__):
self.rgb(arg.rgb())
return
raise ValueError("unrecognized constructor option")
if grey is not None:
self.grey(grey)
elif rgb is not None:
self.rgb(rgb)
elif rgb100 is not None:
self.rgb100(rgb100)
elif rgb255 is not None:
self.rgb255(rgb255)
elif hsv is not None:
self.hsv(hsv)
elif hsv100 is not None:
self.hsv100(hsv100)
elif hsv255 is not None:
self.hsv255(hsv255)
elif hsl is not None:
self.hsl(hsl)
elif hsl100 is not None:
self.hsl100(hsl100)
elif hsl255 is not None:
self.hsl255(hsl255)
elif yiq is not None:
self.yiq(yiq)
elif hex is not None:
self.hex(hex)
elif css3 is not None:
self.css3(css3)
elif hash is not None:
self.hash(hash)
elif colour is not None:
self.rgb(colour.rgb())
else:
# no argument was given -- default to black
self.grey(0)
# base methods for the various colour models
# --------------------------------------------------------------------------
def rgb(self, rgb=None, min=0.0, max=1.0):
"""
Get or set the colour as a 3-tuple of RGB values in a particular range
Called with no rgb argument, return the object's colour.
If both min and max are integer types rather than floats, values are
rounded to the nearest integer.
Called with a 3-tuple, the colour is set to the given colour.
Any missing channels (that is, where None is given rather than a number)
are not changed.
"""
if rgb is None:
if min == 0.0 and max == 1.0:
return self.__colour
values = (min + i * (max - min) for i in self.__colour)
if not isinstance(min, float) \
and not isinstance(max, float):
# round to integers
return tuple(map(int, map(round, values)))
return tuple(values)
if len(rgb) != 3:
raise ValueError("expected a 3-tuple")
for i in rgb:
if i is not None and (i < min or i > max):
raise ValueError("expected values in the range %s~%s" % (min, max))
if min == 0.0 and max == 1.0:
newrgb = tuple(rgb[i] \
if rgb[i] is not None else self.__colour[i] \
for i in range(3))
else:
newrgb = tuple(float(rgb[i] - min) / float(max - min) \
if rgb[i] is not None else self.__colour[i] \
for i in range(3))
self.__colour = newrgb
return self
def rgb100(self, *args, **kwargs):
"""Same as rgb() with min set to 0 and max to 100"""
return self.rgb(min=0, max=100, *args, **kwargs)
def rgb255(self, *args, **kwargs):
"""Same as rgb() with min set to 0 and max to 255"""
return self.rgb(min=0, max=255, *args, **kwargs)
def __hsx(self, hsl, hsx=None, perceptual=False,
hmin=0.0, hmax=360.0, sxmin=0.0, sxmax=1.0):
"""
Internal method, logic behind hsv() and hsl()
"""
if hsx is None:
h, s, x = rgbtohsl(self.__colour) if hsl \
else rgbtohsv(self.__colour)
if hmin != 0.0 or hmax != 360.0:
h = hmin + (h / 360.0) * (hmax - hmin)
if not isinstance(hmin, float) \
and not isinstance(hmax, float):
# round to integer
h = int(round(h))
if sxmin != 0.0 or sxmax != 1.0:
s = sxmin + s * (sxmax - sxmin)
x = sxmin + x * (sxmax - sxmin)
if not isinstance(sxmin, float) \
and not isinstance(sxmax, float):
s = int(round(s))
x = int(round(x))
return (h, s, x)
if len(hsx) != 3:
raise ValueError("expected a 3-tuple")
h, s, x = hsx
if h is not None:
h = (h - hmin) % (hmax - hmin) + hmin
for i in [s, x]:
if i is not None and (i < sxmin or i > sxmax):
raise ValueError(\
"expected saturation and %s values in the range %s~%s" \
% ("lightness" if hsl else "value", sxmin, sxmax))
oldhsx = self.__hsx(hsl)
if h is None:
h = oldhsx[0]
elif hmin != 0.0 or hmax != 360.0:
h = float(h - hmin) / float(hmax - hmin)
if s is None:
s = oldhsx[1]
elif sxmin != 0.0 or sxmax != 1.0:
s = float(s - sxmin) / float(sxmax - sxmin)
if x is None:
x = oldhsx[2]
elif sxmin != 0.0 or sxmax != 1.0:
x = float(x - sxmin) / float(sxmax - sxmin)
if perceptual:
oldluma = self.luma()
if hsl:
self.rgb(hsltorgb((h, s, x)))
else:
self.rgb(hsvtorgb((h, s, x)))
if perceptual:
self.luma(oldluma)
return self
def hsv(self, hsv=None, perceptual=False,
hmin=0.0, hmax=360.0, svmin=0.0, svmax=1.0):
"""
Get or set the colour as a 3-tuple of HSV values in a particular range
Called with no hsv argument, return the object's colour.
If both min and max are integer types rather than floats, values are
rounded to the nearest integer.
Called with a 3-tuple, the colour is set to the given colour.
Any missing channels (that is, where None is given rather than a number)
are not changed.
Hues out of the range are accepted since hue is circular.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
return self.__hsx(False, hsx=hsv, perceptual=perceptual, \
hmin=hmin, hmax=hmax, sxmin=svmin, sxmax=svmax)
def hsv100(self, *args, **kwargs):
"""Same as hsv() with hmin=0, hmax=360, svmin=0, svmax=100"""
return self.hsv(hmin=0, hmax=360, svmin=0, svmax=100, *args, **kwargs)
def hsv255(self, *args, **kwargs):
"""Same as hsv() with hmin=0, hmax=360, svmin=0, svmax=255"""
return self.hsv(hmin=0, hmax=360, svmin=0, svmax=255, *args, **kwargs)
def hsl(self, hsl=None, perceptual=False,
hmin=0.0, hmax=360.0, slmin=0.0, slmax=1.0):
"""
Get or set the colour as a 3-tuple of HSL values in a particular range
Called with no hsl argument, return the object's colour.
If both min and max are integer types rather than floats, values are
rounded to the nearest integer.
Called with a 3-tuple, the colour is set to the given colour.
Any missing channels (that is, where None is given rather than a number)
are not changed.
Hues out of the range are accepted since hue is circular.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
return self.__hsx(True, hsx=hsl, perceptual=perceptual, \
hmin=hmin, hmax=hmax, sxmin=slmin, sxmax=slmax)
def hsl100(self, *args, **kwargs):
"""Same as hsl() with hmin=0, hmax=360, slmin=0, slmax=100"""
return self.hsl(hmin=0, hmax=360, slmin=0, slmax=100, *args, **kwargs)
def hsl255(self, *args, **kwargs):
"""Same as hsl() with hmin=0, hmax=360, slmin=0, slmax=255"""
return self.hsl(hmin=0, hmax=360, slmin=0, slmax=255, *args, **kwargs)
def yiq(self, yiq=None,
ymin=0.0, ymax=1.0, iqmin=-1.0, iqmax=1.0):
"""
Get or set the colour as a 3-tuple of YIQ values in a particular range
Called with no yiq argument, return the object's colour.
If both min and max are integer types rather than floats, values are
rounded to the nearest integer.
Called with a 3-tuple, the colour is set to the given colour.
Any missing channels (that is, where None is given rather than a number)
are not changed.
"""
if yiq is None:
y, i, q = rgbtoyiq(self.__colour)
if ymin != 0.0 or ymax != 1.0:
y = ymin + y * (ymax - ymin)
if not isinstance(ymin, float) \
and not isinstance(ymax, float):
# round to integer
y = int(round(y))
if iqmin != -1.0 or iqmax != 1.0:
i = iqmin + i * (iqmax - iqmin)
q = iqmin + q * (iqmax - iqmin)
if not isinstance(iqmin, float) \
and not isinstance(iqmax, float):
i = int(round(i))
q = int(round(q))
return (y, i, q)
if len(yiq) != 3:
raise ValueError("expected a 3-tuple")
y, i, q = yiq
if y is not None and (y < ymin or y > ymax):
raise ValueError("expected a luma value in the range %s~%s" % (ymin, ymax))
for x in [i, q]:
if x is not None and (x < iqmin or x > iqmax):
raise ValueError("expected in-phase and quadrature values" \
+ " in the range %s~%s" % (iqmin, iqmax))
oldyiq = self.yiq()
if y is None:
y = oldyiq[0]
elif ymin != 0.0 or ymax != 1.0:
y = float(y - ymin) / float(ymax - ymin)
if i is None:
i = oldyiq[1]
elif iqmin != -1.0 or iqmax != 1.0:
i = float(i - iqmin) / float(iqmax - iqmin)
if q is None:
q = oldyiq[2]
elif iqmin != -1.0 or iqmax != 1.0:
q = float(q - iqmin) / float(iqmax - iqmin)
return self.rgb(yiqtorgb((y, i, q)))
# set a colour without individual values for one of the colour models
# --------------------------------------------------------------------------
def hex(self, hex=None, hash=True, allowshort=False, forceshort=False):
"""
Get or set the colour as a hex RGB string, with or without a leading
hash
Called with no hex argument, return a hex representation of the object's
colour.
The hash argument controls whether or not a leading hash will be
present.
The allowshort argument controls whether or not a short (3-digit)
version is used if it can losslessly be.
The forceshort argument forces a short (3-digit) hex representation by
snapping to the closest available colour.
Called with the hex argument, attempt to parse the string as a hex RGB
colour and set the colour to the result. Three or six digit strings are
accepted.
"""
if hex is None:
return rgbtohex(self.rgb(), hash=hash, allowshort=allowshort,
forceshort=forceshort)
return self.rgb(hextorgb(hex))
def css3(self, name=None):
"""
Get or set the colour as a CSS3 named colour
Called with no name argument, attempt to find a CSS3 named colour equal
to this colour and return its name if found.
Called with a string, the colour is set to the CSS3 named colour
corresponding to the given string.
"""
if name is None:
rgb255 = self.rgb255()
for key in CSS3.keys():
if rgb255 == Colour(css3=key).rgb255():
return key
return None
try:
return self.hex(CSS3[name.lower()])
except KeyError:
raise ValueError("no such CSS3 named colour")
def grey(self, i=None, min=0.0, max=1.0):
"""
Set the colour to a shade of grey with the given intensity, or return
the current intensity if this colour is a shade of grey, in a particular
range
Called with no i argument, return the intensity of this colour as long
as the colour is a shade of gray, otherwise return False.
Called with a number, the colour is set to a shade of grey with the
given intensity.
"""
if i is None:
if self.saturation_hsv() != 0:
return False
i = min + self.intensity() * (max - min)
if not isinstance(min, float) \
and not isinstance(max, float):
# round to integer
return int(round(i))
return i
if i < min or i > max:
raise ValueError("expected value in the range %s~%s" % (min, max))
if min != 0.0 or max != 0.0:
i = (i - min) / (max - min)
return self.rgb((i, i, i))
def hash(self, tohash,
minh=None, maxh=None, mins=0.2, maxs=1.0, miny=0.3, maxy=0.7):
"""
Make a colour to be associated with the given input
The tohash argument is converted to a string and then put through the
MD5 algorithm. The resulting hash is then used to seed a hue, saturation
and luma.
Pass minh and maxh values to constrain the hues (for instance -20
degrees to 140 degrees, which is not the same as 140 degrees to 340
degrees). Default is all hues.
Pass mins and maxs values to constrain the saturation values. By default
only colours close to grey are disallowed.
Pass miny and maxy to constrain the luma. By default anything difficult
to see against black or white is disallowed.
"""
hash = hashlib.md5(str(tohash).encode('utf-8')).hexdigest()
if maxh is None or minh is None:
maxh = 360
minh = 0
else:
while minh <= -360:
minh += 360
while minh >= 360:
minh -= 360
while maxh <= -360:
maxh += 360
while maxh >= 360:
maxh -= 360
if mins < 0 or mins > 1 or maxs < 0 or maxs > 1 \
or miny < 0 or miny > 1 or maxy < 0:
raise ValueError(
"expected mins, maxs, miny and maxy to be in the range 0~1")
if mins > maxs:
raise ValueError("expected mins to be less than or equal to maxs")
if miny > maxy:
raise ValueError("expected miny to be less than or equal to maxy")
h = minh + (maxh - minh) * int(hash[0:8], 16) / float(16**8)
s = mins + (maxs - mins) * int(hash[8:16], 16) / float(16**8)
y = miny + (maxy - miny) * int(hash[16:24], 16) / float(16**8)
return self.hsv((h, s, y)).luma(y)
# hue
# --------------------------------------------------------------------------
def hue(self, h=None, perceptual=False):
"""
Get or set the hue in degrees
Called with no h argument, return the colour's hue in degrees.
Called with a number in degrees, set the colour's hue.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
if h is None:
return self.hsv()[0]
return self.hsv((h, None, None), perceptual=perceptual)
def shifthue(self, angle, perceptual=False):
"""
Shift the hue of this colour relatively by a given number of degrees
Return a new colour like this one but with its hue shifted by the given
number of degrees.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
if angle == 0:
return self
return self.hue(self.hue() + angle, perceptual=perceptual)
# saturation of various kinds
# --------------------------------------------------------------------------
def __saturation_hsx(self, hsl, s=None, perceptual=False):
"""
Internal method, logic behind saturation_hsv() and saturation_hsl()
"""
if s is None:
return self.hsl()[1] if hsl else self.hsv()[1]
if s < 0 or s > 1:
raise ValueError("expected a value in the range 0~1")
if hsl:
method = self.hsl
else:
method = self.hsv
return method((None, s, None), perceptual=perceptual)
def saturation_hsv(self, s=None, perceptual=False):
"""
Get or set the saturation in HSV space in the range 0~1
Called with no s argument, return the colour's saturation in HSV space.
Called with a number in the range 0~1, set the colour's saturation in
HSV space. The hue and value are not changed.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
return self.__saturation_hsx(False, s, perceptual=perceptual)
def saturation_hsl(self, s=None, perceptual=False):
"""
Get or set the saturation in HSL space in the range 0~1
Called with no s argument, return the colour's saturation in HSL space.
Called with a number in the range 0~1, set the colour's saturation in
HSL space. The hue and value are not changed.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
return self.__saturation_hsx(True, s, perceptual=perceptual)
def __shiftsaturation_hsx(self, hsl, scale, perceptual=False):
"""
Internal method, logic behind shiftsaturation_hsv() and
shiftsaturation_hsl()
"""
if scale == 0:
return self
if scale < -1 or scale > 1:
return ValueError("expected a value in the range -1~1")
s = self.saturation_hsl() if hsl else self.saturation_hsv()
if scale > 0:
s += (1 - s) * scale
else:
s *= (scale + 1)
return self.__hsx(hsl, (None, s, None), perceptual=perceptual)
def shiftsaturation_hsv(self, scale, perceptual=False):
"""
Shift the saturation of this colour relatively in HSV space
Called with a float argument in the range -1~1, return a new colour like
this one but shifted by the given proportion along the saturation axis
of HSV space.
Passing 1 would saturate the colour completely, passing -1 would
desatuarate the colour completely.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
return self.__shiftsaturation_hsx(False, scale, perceptual=perceptual)
def shiftsaturation_hsl(self, scale, perceptual=False):
"""
Shift the saturation of this colour relatively in HSL space
Called with a float argument in the range -1~1, return a new colour like
this one but shifted by the given proportion along the saturation axis
of HSL space.
Passing 1 would saturate the colour completely, passing -1 would
desatuarate the colour completely.
If the perceptual argument is True attempt to preserve the colour's luma
in order to retain the colour's perceived brightness.
"""
return self.__shiftsaturation_hsx(True, scale, perceptual=perceptual)
# lightness of various kinds
# --------------------------------------------------------------------------
def intensity(self, i=None):
"""
Get or set the colour's intensity as a float in the range 0~1
The intensity of a colour is the average of its red, green and blue
components. So the scale of intensity for this colour ranges from black
with intensity 0, in a straight line in the RGB cube to this colour,
then in another straight line in the RGB cube towards white.
Called with no i argument, return the colour's intensity.
Called with a number, set the colour's intensity to the given value.
"""
if i is None:
return sum(self.rgb()) / 3.0
if i < 0 or i > 1:
raise ValueError("expected value in the range 0~1")
if i == 0 or i == 1:
return self.grey(i, False)
if i == self.intensity():
return self
diff = i - self.intensity()
if diff > 0:
scale = diff / (1 - self.intensity())
else:
scale = diff / self.intensity()
return self.shiftintensity(scale)
def shiftintensity(self, scale):
"""
Shift the intensity of this colour relatively towards black or white
See intensity() for what exactly "intensity" means.
Called with a float argument in the range -1~1, shift the colour towards
black (-1 <= scale < 0) or white (0 < scale <= 1) linearly in the RGB
cube.
"""
if scale == 0:
return self
if scale < -1 or scale > 1:
return ValueError("expected a value in the range -1~1")
if scale > 0:
return self.mix(1, scale)
return self.mix(0, -scale)
def __value_lightness(self, hsl, x=None):
"""
Internal method, logic behind value() and lightness()
"""
if x is None:
return self.hsl()[2] if hsl else self.hsv()[2]
if x < 0 or x > 1:
raise ValueError("expected a value in the range 0~1")
if hsl:
return self.hsl((None, None, x))
return self.hsv((None, None, x))
def value(self, v=None):
"""
Get or set the value in HSV space in the range 0~1
Called with no v argument, return the colour's value in HSV space.
Called with a number in the range 0~1, set the colour's value in HSV
space. The hue and saturation are not changed.
"""
return self.__value_lightness(False, v)
def lightness(self, l=None):
"""
Get or set the lightness in HSL space in the range 0~1
Called with no v argument, return the colour's lightness in HSL space.
Called with a number in the range 0~1, set the colour's lightness in HSV
space. The hue and saturation are not changed.
"""
return self.__value_lightness(True, l)
def __shiftvalue_lightness(self, hsl, scale):
"""
Internal method, logic behind shiftvalue() and shiftlightness()
"""
if scale == 0:
return self
if scale < -1 or scale > 1:
return ValueError("expected a value in the range -1~1")
x = self.lightness() if hsl else self.value()
if scale > 0:
x += (1 - x) * scale
else:
x *= scale + 1
return self.__value_lightness(hsl, x)
def shiftvalue(self, scale):
"""
Shift the value of this colour relatively
Called with a float argument in the range -1~1, return a new colour like
this one but shifted by the given proportion along the value axis of HSV
space.
Passing 1 would maximise value (but unless the colour is fully saturated
it will not reach white), passing 0 would cause no change and passing -1
would result in black.
"""
return self.__shiftvalue_lightness(False, scale)
def shiftlightness(self, scale):
"""
Shift the lightness of this colour relatively
Called with a float argument in the range -1~1, return a new colour like
this one but shifted by the given proportion along the lightness axis of
HSL space.
Passing 1 would result in white, passing 0 would cause no change and
passing -1 would result in black.
"""
return self.__shiftvalue_lightness(True, scale)
def luma(self, y=None):
"""
Get or set the luma of the colour in the range 0~1
Called with no y argument, return the colour's luma.
Called with a number in the range 0~1, set the colour's luma as close to
the given value as possible while keeping its colour intact.
"""
if y is None:
return self.yiq()[0]
if y < 0 or y > 1:
raise ValueError("expected a value in the range 0~1")
return self.yiq((y, None, None))
def shiftluma(self, scale):
"""
Shift the luma of this colour relatively
Called with a float argument in the range -1~1, return a new colour like
this one but brightened or darkened by the given proportion in luma.
Passing 1 would result in the brightest possible colour for this colour,
passing 0 would cause no change and passing -1 would result in the
darkest.
"""
if scale == 0:
return self
if scale < -1 or scale > 1:
return ValueError("expected a value in the range -1~1")
y = self.luma()
if scale > 0:
y += (1 - y) * scale
else:
y *= scale + 1
return self.luma(y)
# mix colours
# --------------------------------------------------------------------------
def mix(self, colour, proportion):
"""
Mix this colour with another
The colour argument is usually another Colour object. If it is not, it
is passed to the constructor to attempt to produce a new colour.
The proportion argument is a float in the range 0~1 controlling how far
towards the given colour to move, so 0 is no change and 1 is a complete
change.
"""
rgb = self.rgb()
if not isinstance(colour, self.__class__):
colour = self.__class__(colour)
colour = colour.rgb()
if proportion < 0 or proportion > 1:
return ValueError("expected a value in the range 0~1")
return self.rgb(tuple(rgb[x] + (colour[x] - rgb[x]) * proportion \
for x in range(3)))
# miscellaneous output
# --------------------------------------------------------------------------
def __str__(self):
"""
Return a string representation of the object
The hex() method is called, returning a hex RGB string with a leading
hash.
"""
return self.hex()
def swatch(self, showhex=True, cssclass=None):
"""
Return an HTML colour swatch string