aboutsummaryrefslogtreecommitdiff
path: root/thcolor/builtin/_default.py
blob: 31b1434542be49063dc4dc0a006746202bd4cecd (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2019 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
#******************************************************************************
""" Named colors and function definitions. Color names are case-insensitive.
	Extends the CSS references. """

from .._color import Color as _Color
from .._ref import Reference as _Reference
from ._css import CSS4Reference as _CSS4Reference

__all__ = ["DefaultReference"]

class DefaultReference(_CSS4Reference):
	""" Functions extending the CSS Color Module Level 4 reference. """

	def __init__(self, *args, **kwargs):
		super().__init__(*args, **kwargs)

	number = _Reference.number
	percentage = _Reference.percentage
	angle = _Reference.angle
	color = _Reference.color

	# ---
	# RGB functions.
	# ---

	def rbg(self, r: number | percentage,
		b: number | percentage = number(0), g: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (0, 2, 1))

	def rbga(self, r: number | percentage,
		b: number | percentage = number(0), g: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (0, 2, 1))

	def brg(self, b: number | percentage,
		r: number | percentage = number(0), g: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (1, 2, 0))

	def brga(self, b: number | percentage,
		r: number | percentage = number(0), g: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (1, 2, 0))

	def bgr(self, b: number | percentage,
		g: number | percentage = number(0), r: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (2, 1, 0))

	def bgra(self, b: number | percentage,
		g: number | percentage = number(0), r: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (2, 1, 0))

	def gbr(self, g: number | percentage,
		b: number | percentage = number(0), r: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (2, 0, 1))

	def gbra(self, g: number | percentage,
		b: number | percentage = number(0), r: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (2, 0, 1))

	def grb(self, g: number | percentage,
		r: number | percentage = number(0), b: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (1, 0, 2))

	def grba(self, g: number | percentage,
		r: number | percentage = number(0), b: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._rgb((r, g, b, alpha), (1, 0, 2))

	# ---
	# HLS and HWB aliases.
	# ---

	def hls(self, h: number | angle, l: number | percentage,
		s: number | percentage, alpha: number | percentage = number(1.0)):
		return self._hsl((h, s, l, alpha), (0, 2, 1))

	def hlsa(self, h: number | angle, l: number | percentage,
		s: number | percentage, alpha: number | percentage = number(1.0)):
		return self._hsl((h, s, l, alpha), (0, 2, 1))

	def hbw(self, h: number | angle, b: number | percentage = number(0),
		w: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._hwb((h, w, b, alpha), (0, 2, 1))

	def hbwa(self, h: number | angle, b: number | percentage = number(0),
		w: number | percentage = number(0),
		alpha: number | percentage = number(1.0)):
		return self._hwb((h, w, b, alpha), (0, 2, 1))

	# ---
	# CMYK utilities and extensions.
	# ---

	def cmyk(self, c: number | percentage,
		m: number | percentage = percentage(0),
		y: number | percentage = percentage(0),
		k = number | percentage(0),
		alpha: number | percentage = number(1.0)):
		ci, mi, yi, ki = 0, 1, 2, 3

		try:
			c = c.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(ci, str(e))

		try:
			m = m.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(mi, str(e))

		try:
			y = y.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(yi, str(e))

		try:
			k = k.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(ki, str(e))

		try:
			alpha = alpha.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(4, str(e))

		return _Reference.color(_Color(_Color.Type.CMYK, c, m, y, k, alpha))

	def xyz(self, x: number | percentage, y: number | percentage,
		z: number | percentage, alpha: number | percentage = number(1.0)):

		try:
			x = x.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		try:
			y = y.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(1, str(e))

		try:
			z = z.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(2, str(e))

		try:
			alpha = alpha.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(3, str(e))

		return _Reference.color(_Color(_Color.Type.XYZ, x, y, z, alpha))

	# ---
	# Get the RGB components of a color.
	# ---

	def red(self, col: color) -> number:
		r, g, b = col.to_color().rgb()
		return _Reference.number(r)

	def green(self, col: color) -> number:
		r, g, b = col.to_color().rgb()
		return _Reference.number(g)

	def blue(self, col: color) -> number:
		r, g, b = col.to_color().rgb()
		return _Reference.number(b)

	# ---
	# Manage the lightness and saturation for HSL colors.
	# ---

	def darker(self, by: number | percentage, col: color) -> color:
		try:
			by = by.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		try:
			col = col.to_color()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		h, l, s, a = col.hlsa()
		l = max(l - by, 0.0)

		return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a))

	def lighter(self, by: number | percentage, col: color) -> color:
		try:
			by = by.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		try:
			col = col.to_color()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		h, l, s, a = col.hlsa()
		l = min(l + by, 1.0)

		return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a))

	def desaturate(self, by: number | percentage, col: color) -> color:
		try:
			by = by.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		try:
			col = col.to_color()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		h, l, s, a = col.hlsa()
		s = max(s - by, 0.0)

		return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a))

	def saturate(self, by: number | percentage, col: color) -> color:
		try:
			by = by.to_factor()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		try:
			col = col.to_color()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		h, l, s, a = col.hlsa()
		s = min(s + by, 1.0)

		return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a))

	# ---
	# Others.
	# ---

	def ncol(self, col: color) -> color:
		# Compatibility with w3color.js! NCols are managed directly without
		# the function, so the functio doesn't do anything.

		return col

# End of file.