-
Notifications
You must be signed in to change notification settings - Fork 0
/
colour.py
152 lines (123 loc) · 4.38 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
import colorsys
import dataclasses
import re
from typing import Tuple
import cairo
@dataclasses.dataclass(frozen=True)
class HLSColour:
h: float
l: float
s: float
a: float
def lighten(self, by: float) -> 'HLSColour':
return HLSColour(self.h, min(self.l + by, 1.0), self.s, self.a)
def darken(self, by: float) -> 'HLSColour':
return HLSColour(self.h, max(self.l - by, 0.0), self.s, self.a)
def rgb(self) -> 'Colour':
r, g, b = colorsys.hls_to_rgb(self.h, self.l, self.s)
return Colour(r, g, b, self.a)
def apply_to(self, context: cairo.Context):
self.rgb().apply_to(context)
rgb_expr = re.compile(r"rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)")
rgba_expr = re.compile(r"rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)")
hsl_expr = re.compile(r"hsl\((\d+),\s+(\d+)%,\s*(\d+)%\)")
hsla_expr = re.compile(r"hsla\((\d+),\s+(\d+)%,\s*(\d+)%,\s*([\d.]+)\s*\)")
@dataclasses.dataclass(frozen=True)
class Colour:
r: float
g: float
b: float
a: float = 1.0
def rgba(self) -> Tuple[float, float, float, float]:
return self.r, self.g, self.b, self.a
def rgb(self) -> Tuple[float, float, float]:
return self.r, self.g, self.b
def hls(self) -> HLSColour:
h, l, s = colorsys.rgb_to_hls(self.r, self.g, self.b)
return HLSColour(h, l, s, self.a)
def darken(self, by: float) -> 'Colour':
return self.hls().darken(by).rgb()
def lighten(self, by: float) -> 'Colour':
return self.hls().lighten(by).rgb()
def alpha(self, new_alpha: float):
return Colour(self.r, self.g, self.b, new_alpha)
@staticmethod
def _rescale(t):
return map(lambda v: v / 255.0, t)
def as_hex(self):
return "".join([
"%x" % int(self.r * 256),
"%x" % int(self.g * 256),
"%x" % int(self.b * 256),
])
@staticmethod
def hex(hexcolour: str, alpha=1.0):
if hexcolour.startswith("#"):
hexcolour = hexcolour[1:]
r, g, b = Colour._rescale(tuple(int(hexcolour[i:i + 2], 16) for i in (0, 2, 4)))
return Colour(r, g, b, alpha)
@staticmethod
def from_pil(r, g, b, a=255.0):
return Colour(*Colour._rescale((r, g, b, a)))
def apply_to(self, context: cairo.Context):
context.set_source_rgba(*self.rgba())
@classmethod
def from_spec(cls, colour_spec) -> 'Colour':
m = rgb_expr.match(colour_spec)
if m is not None:
return Colour.from_pil(
int(m.group(1)),
int(m.group(2)),
int(m.group(3)),
)
m = rgba_expr.match(colour_spec)
if m is not None:
return Colour.from_pil(
int(m.group(1)),
int(m.group(2)),
int(m.group(3)),
float(m.group(4)) * 255
)
if colour_spec.startswith("#"):
hex_spec = colour_spec[1:]
if len(hex_spec) == 3:
return Colour.from_pil(
int(hex_spec[0]*2, 16),
int(hex_spec[1]*2, 16),
int(hex_spec[2]*2, 16),
)
elif len(hex_spec) == 6:
return Colour.from_pil(
int(hex_spec[0:2], 16),
int(hex_spec[2:4], 16),
int(hex_spec[4:6], 16),
)
elif len(hex_spec) == 8:
return Colour.from_pil(
int(hex_spec[0:2], 16),
int(hex_spec[2:4], 16),
int(hex_spec[4:6], 16),
int(hex_spec[6:8], 16),
)
m = hsl_expr.match(colour_spec)
if m is not None:
return hsl(
int(m.group(1)) / 360.0,
int(m.group(2)) / 100.0,
int(m.group(3)) / 100.0).rgb()
m = hsla_expr.match(colour_spec)
if m is not None:
return hsl(
int(m.group(1)) / 360.0,
int(m.group(2)) / 100.0,
int(m.group(3)) / 100.0,
float(m.group(4)),
).rgb()
print(f"Can't parse {colour_spec}")
return Colour(1.0, 1.0, 1.0)
def hsl(h, s, l, a=1.0) -> HLSColour:
return HLSColour(h, l, s, a)
if __name__ == "__main__":
c = Colour.from_spec("hsla(35, 57%, 88%, 0.49)")
print(c)
print(c.as_hex())