aboutsummaryrefslogtreecommitdiff
path: root/thcolor/_sys.py
blob: 23c1ec05c55f7e8a03faa51548fbaf6fec8d7bcd (plain)
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
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
#******************************************************************************
""" Conversions between color systems. """

from math import (ceil as _ceil, atan2 as _atan2, sqrt as _sqrt, cos as _cos,
	sin as _sin, pow as _pow)

from ._angle import Angle as _Angle

__all__ = ["hls_to_rgb", "rgb_to_hls", "rgb_to_hwb", "hwb_to_rgb",
	"cmyk_to_rgb", "rgb_to_cmyk", "lab_to_rgb", "rgb_to_lab",
	"lch_to_lab", "lab_to_lch", "netscape_color"]

# ---
# Color systems conversion utilities.
# ---

def _rgb(r, g, b):
	return tuple(map(lambda x: int(round(x * 255, 0)), (r, g, b)))
def _hls(hue, s, l):
	return _Angle(_Angle.Type.DEG, round(hue, 2)), round(l, 2), round(s, 2)

def _rgb_to_lrgb(r, g, b):
	def _linearize(val):
		# Undo gamma encoding.

		val /= 255
		if val < 0.04045:
			return val / 12.92

		return _pow((val + 0.055) / 1.055, 2.4)

	return (*map(_linearize, (r, g, b)))

def hls_to_rgb(hue, l, s):
	""" Convert HLS to RGB. """

	if s == 0:
		# Achromatic color.

		return l, l, l

	def _hue_to_rgb(t1, t2, hue):
		hue %= 6

		if   hue < 1:
			return t1 + (t2 - t1) * hue
		elif hue < 3:
			return t2
		elif hue < 4:
			return t1 + (t2 - t1) * (4 - hue)
		return t1

	hue = (hue.degrees % 360) / 60
	if l <= 0.5:
		t2 = l * (s + 1)
	else:
		t2 = l + s - (l * s)

	t1 = l * 2 - t2

	return _rgb(\
		_hue_to_rgb(t1, t2, hue + 2),
		_hue_to_rgb(t1, t2, hue),
		_hue_to_rgb(t1, t2, hue - 2))

def hwb_to_rgb(hue, w, bl):
	""" Convert HWB to RGB color.
		https://drafts.csswg.org/css-color/#hwb-to-rgb """

	r, g, b = map(lambda x: x / 255, hls_to_rgb(hue, 0.5, 1.0))
	if w + bl > 1:
		w, bl = map(lambda x: x / (w + bl), (w, bl))
	return _rgb(*map(lambda x: x * (1 - w - bl) + w, (r, g, b)))

def cmyk_to_rgb(c, m, y, k):
	""" Convert CMYK to RGB. """

	r = 1 - min(1, c * (1 - k) + k)
	g = 1 - min(1, m * (1 - k) + k)
	b = 1 - min(1, y * (1 - k) + k)

	return _rgb(r, g, b)

def lab_to_rgb(l, a, b):
	""" Convert LAB to RGB. """

	# TODO
	raise NotImplementedError

def rgb_to_lab(r, g, b):
	""" Convert RGB to LAB. """

	# TODO
	raise NotImplementedError

def lab_to_lch(l, a, b):
	""" Convert RGB to LAB. """

	h = _Angle(_Angle.Type.RAD, _atan2(b, a))
	c = _sqrt(a * a + b * b)

	return (l, c, h)

def lch_to_lab(l, c, h):
	""" Convert LCH to LAB. """

	a = c * _cos(h.radians)
	b = c * _sin(h.radians)

	return (l, a, b)

def rgb_to_hls(r, g, b):
	""" Convert RGB to HLS. """

	r, g, b = map(lambda x: x / 255, (r, g, b))

	min_value = min((r, g, b))
	max_value = max((r, g, b))
	chroma = max_value - min_value

	if   chroma == 0:
		hue = 0
	elif r == max_value:
		hue = (g - b) / chroma
	elif g == max_value:
		hue = (b - r) / chroma + 2
	else:
		hue = (r - g) / chroma + 4

	hue = hue * 60 + (hue < 0) * 360
	l = (min_value + max_value) / 2
	if min_value == max_value:
		s = 0
	else:
		s = max_value - min_value
		if l < 0.5:
			s /= max_value + min_value
		else:
			s /= 2 - max_value - min_value

	return _hls(hue, l, s)

def rgb_to_hwb(r, g, b):
	""" Convert RGB to HWB. """

	r, g, b = map(lambda x: x / 255, (r, g, b))

	max_value = max((r, g, b))
	min_value = min((r, g, b))
	chroma = max_value - min_value

	if   chroma == 0:
		hue = 0
	elif r == max_value:
		hue = (g - b) / chroma
	elif g == max_value:
		hue = (b - r) / chroma + 2
	elif g == max_value:
		hue = (r - g) / chroma + 4

	hue = (hue % 6) * 360
	w = min_value
	b = max_value

	return _Angle(_Angle.Type.DEG, hue), w, b

def rgb_to_cmyk(r, g, b):
	""" Convert RGB to CMYK. """

	r, g, b = map(lambda x: x / 255, (r, g, b))

	k = 1 - max((r, g, b))
	if k == 1:
		c, m, y = 0, 0, 0
	else:
		c = (1 - r - k) / (1 - k)
		m = (1 - g - k) / (1 - k)
		y = (1 - b - k) / (1 - k)

	return (c, m, y, k)

# ---
# Other utilities.
# ---

def netscape_color(name):
	""" Produce a color from a name (which can be all-text, all-digits or
		both), using the Netscape behaviour. """

	# Find more about this here: https://stackoverflow.com/a/8333464
	#
	# First of all:
	# - we sanitize our input by replacing invalid characters
	#   by '0' characters (the 0xFFFF limit is due to how
	#   UTF-16 was managed at the time).
	# - we truncate our input to 128 characters.

	name = name.lower()
	name = ''.join(c if c in '0123456789abcdef' \
		else ('0', '00')[ord(c) > 0xFFFF] \
		for c in name[:128])[:128]

	# Then we calculate some values we're going to need.
	# `iv` is the size of the zone for a member.
	# `sz` is the size of the digits slice to take in that zone
	# (max. 8).
	# `of` is the offset in the zone of the slice to take.

	iv = _ceil(len(name) / 3)
	of = iv - 8 if iv > 8 else 0
	sz = iv - of

	# Then we isolate the slices using the values calculated
	# above. `gr` will be an array of 3 or 4 digit strings
	# (depending on the number of members).

	gr = list(map(lambda i: name[i * iv + of:i * iv + iv] \
		.ljust(sz, '0'), range(3)))

	# Check how many digits we can skip at the beginning of
	# each slice.

	pre = min(map(lambda x: len(x) - len(x.lstrip('0')), gr))
	pre = min(pre, sz - 2)

	# Then we extract the values.

	it = map(lambda x: int('0' + x[pre:pre + 2], 16), gr)
	r, g, b = it

	return (r, g, b)

# End of file.