aboutsummaryrefslogtreecommitdiff
path: root/thcolor/builtin/_css.py
blob: 457d560711887eb16405043dab4527730393ac55 (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
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
#!/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.
	Taken from: https://www.w3schools.com/cssref/css_colors.asp """

from .._color import Color as _Color
from .._ref import Reference as _Reference
from .._exc import InvalidArgumentValueError as _InvalidArgumentValueError
from ..syntax import (number as _number, percentage as _percentage,
	angle as _angle, color as _color, alias as _alias)

__all__ = ["CSS1Reference", "CSS2Reference", "CSS3Reference",
	"CSS4Reference"]


def _rgb(raw):
	r = int(raw[1:3], 16)
	g = int(raw[3:5], 16)
	b = int(raw[5:7], 16)

	return _Color(_Color.Type.RGB, r, g, b)


class CSS1Reference(_Reference):
	""" Named colors from `CSS Level 1 <https://www.w3.org/TR/CSS1/>`_. """

	black = _rgb('#000000')
	silver = _rgb('#c0c0c0')
	gray = _rgb('#808080')
	white = _rgb('#ffffff')

	maroon = _rgb('#800000')
	red = _rgb('#ff0000')
	purple = _rgb('#800080')
	fuchsia = _rgb('#ff00ff')
	green = _rgb('#008000')
	lime = _rgb('#00ff00')
	olive = _rgb('#808000')
	yellow = _rgb('#ffff00')
	navy = _rgb('#000080')
	blue = _rgb('#0000ff')
	teal = _rgb('#008080')
	aqua = _rgb('#00ffff')

	transparent = _RGBColor(0, 0, 0, 0)

	# ---
	# Functions.
	# ---

	def rgb(self,
		r: _number | _percentage = _number(0),
		g: _number | _percentage = _number(0),
		b: _number | _percentage = _number(0)):
		try:
			r = r.to_byte()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		try:
			g = g.to_byte()
		except ValueError as e:
			raise _InvalidArgumentValueError(1, str(e))

		try:
			b = b.to_byte()
		except ValueError as e:
			raise _InvalidArgumentValueError(2, str(e))

		return _Reference.color(_RGBColor(r / 255, g / 255, b / 255, 1.0))

class CSS2Reference(CSS1Reference):
	""" Named colors from `CSS Level 2 (Revision 1)
		<https://www.w3.org/TR/CSS2/>`_. """

	orange = _rgb('#ffa500')

class CSS3Reference(CSS2Reference):
	""" Named colors and functions from `CSS Color Module Level 3
		<https://drafts.csswg.org/css-color-3/>`_. """

	darkblue = _rgb('#00008B')
	mediumblue = _rgb('#0000CD')
	darkgreen = _rgb('#006400')
	darkcyan = _rgb('#008B8B')
	deepskyblue = _rgb('#00BFFF')
	darkturquoise = _rgb('#00CED1')
	mediumspringgreen = _rgb('#00FA9A')
	springgreen = _rgb('#00FF7F')
	cyan = _rgb('#00FFFF')
	midnightblue = _rgb('#191970')
	dodgerblue = _rgb('#1E90FF')
	lightseagreen = _rgb('#20B2AA')
	forestgreen = _rgb('#228B22')
	seagreen = _rgb('#2E8B57')
	darkslategray = _rgb('#2F4F4F')
	darkslategrey = _rgb('#2F4F4F')
	limegreen = _rgb('#32CD32')
	mediumseagreen = _rgb('#3CB371')
	turquoise = _rgb('#40E0D0')
	royalblue = _rgb('#4169E1')
	steelblue = _rgb('#4682B4')
	darkslateblue = _rgb('#483D8B')
	mediumturquoise = _rgb('#48D1CC')
	indigo = _rgb('#4B0082')
	darkolivegreen = _rgb('#556B2F')
	cadetblue = _rgb('#5F9EA0')
	cornflowerblue = _rgb('#6495ED')
	mediumaquamarine = _rgb('#66CDAA')
	dimgray = _rgb('#696969')
	dimgrey = _rgb('#696969')
	slateblue = _rgb('#6A5ACD')
	olivedrab = _rgb('#6B8E23')
	slategray = _rgb('#708090')
	slategrey = _rgb('#708090')
	lightslategray = _rgb('#778899')
	lightslategrey = _rgb('#778899')
	mediumslateblue = _rgb('#7B68EE')
	lawngreen = _rgb('#7CFC00')
	chartreuse = _rgb('#7FFF00')
	aquamarine = _rgb('#7FFFD4')
	grey = _rgb('#808080')
	skyblue = _rgb('#87CEEB')
	lightskyblue = _rgb('#87CEFA')
	blueviolet = _rgb('#8A2BE2')
	darkred = _rgb('#8B0000')
	darkmagenta = _rgb('#8B008B')
	saddlebrown = _rgb('#8B4513')
	darkseagreen = _rgb('#8FBC8F')
	lightgreen = _rgb('#90EE90')
	mediumpurple = _rgb('#9370DB')
	darkviolet = _rgb('#9400D3')
	palegreen = _rgb('#98FB98')
	darkorchid = _rgb('#9932CC')
	yellowgreen = _rgb('#9ACD32')
	sienna = _rgb('#A0522D')
	brown = _rgb('#A52A2A')
	darkgray = _rgb('#A9A9A9')
	darkgrey = _rgb('#A9A9A9')
	lightblue = _rgb('#ADD8E6')
	greenyellow = _rgb('#ADFF2F')
	paleturquoise = _rgb('#AFEEEE')
	lightsteelblue = _rgb('#B0C4DE')
	powderblue = _rgb('#B0E0E6')
	firebrick = _rgb('#B22222')
	darkgoldenrod = _rgb('#B8860B')
	mediumorchid = _rgb('#BA55D3')
	rosybrown = _rgb('#BC8F8F')
	darkkhaki = _rgb('#BDB76B')
	mediumvioletred = _rgb('#C71585')
	indianred = _rgb('#CD5C5C')
	peru = _rgb('#CD853F')
	chocolate = _rgb('#D2691E')
	tan = _rgb('#D2B48C')
	lightgray = _rgb('#D3D3D3')
	lightgrey = _rgb('#D3D3D3')
	thistle = _rgb('#D8BFD8')
	orchid = _rgb('#DA70D6')
	goldenrod = _rgb('#DAA520')
	palevioletred = _rgb('#DB7093')
	crimson = _rgb('#DC143C')
	gainsboro = _rgb('#DCDCDC')
	plum = _rgb('#DDA0DD')
	burlywood = _rgb('#DEB887')
	lightcyan = _rgb('#E0FFFF')
	lavender = _rgb('#E6E6FA')
	darksalmon = _rgb('#E9967A')
	violet = _rgb('#EE82EE')
	palegoldenrod = _rgb('#EEE8AA')
	lightcoral = _rgb('#F08080')
	khaki = _rgb('#F0E68C')
	aliceblue = _rgb('#F0F8FF')
	honeydew = _rgb('#F0FFF0')
	azure = _rgb('#F0FFFF')
	sandybrown = _rgb('#F4A460')
	wheat = _rgb('#F5DEB3')
	beige = _rgb('#F5F5DC')
	whitesmoke = _rgb('#F5F5F5')
	mintcream = _rgb('#F5FFFA')
	ghostwhite = _rgb('#F8F8FF')
	salmon = _rgb('#FA8072')
	antiquewhite = _rgb('#FAEBD7')
	linen = _rgb('#FAF0E6')
	lightgoldenrodyellow = _rgb('#FAFAD2')
	oldlace = _rgb('#FDF5E6')
	magenta = _rgb('#FF00FF')
	deeppink = _rgb('#FF1493')
	orangered = _rgb('#FF4500')
	tomato = _rgb('#FF6347')
	hotpink = _rgb('#FF69B4')
	coral = _rgb('#FF7F50')
	darkorange = _rgb('#FF8C00')
	lightsalmon = _rgb('#FFA07A')
	lightpink = _rgb('#FFB6C1')
	pink = _rgb('#FFC0CB')
	gold = _rgb('#FFD700')
	peachpuff = _rgb('#FFDAB9')
	navajowhite = _rgb('#FFDEAD')
	moccasin = _rgb('#FFE4B5')
	bisque = _rgb('#FFE4C4')
	mistyrose = _rgb('#FFE4E1')
	blanchedalmond = _rgb('#FFEBCD')
	papayawhip = _rgb('#FFEFD5')
	lavenderblush = _rgb('#FFF0F5')
	seashell = _rgb('#FFF5EE')
	cornsilk = _rgb('#FFF8DC')
	lemonchiffon = _rgb('#FFFACD')
	floralwhite = _rgb('#FFFAF0')
	snow = _rgb('#FFFAFA')
	lightyellow = _rgb('#FFFFE0')
	ivory = _rgb('#FFFFF0')

	# ---
	# Functions.
	# ---

	def hsl(self,
		h: _number | _angle,
		s: _number | _percentage,
		l: _number | _percentage,
		alpha: _number | _percentage = _number(1.0)):

		try:
			h = h.to_hue()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

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

		try:
			l = l.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(_HSLColor(h, s, l, alpha))

	rgba = _alias('rgb')
	hsla = _alias('hsla')

class CSS4Reference(CSS3Reference):
	""" Named colors and functions from `CSS Color Module Level 4
		<https://drafts.csswg.org/css-color/>`_. """

	rebeccapurple = _rgb('#663399')

	# ---
	# Functions.
	# ---

	def hwb(self, h: _number | _angle,
		w: _number | _percentage = _number(0),
		b: _number | _percentage = _number(0),
		alpha: _number | _percentage = _number(1.0)):

		try:
			h = h.to_hue()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

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

		try:
			b = b.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(_HWBColor(h, w, b, alpha))

	def gray(self, g: _number | _percentage,
		alpha: _number | _percentage = _number(1.0)):

		try:
			g = g.to_byte()
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

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

		g /= 255
		return _Reference.color(_RGBColor(g, g, g, alpha))

	def lab(self, l: _number, a: _number, b: _number,
		alpha: _number | _percentage = _number(1.0)):

		try:
			l = l.value
			if l < 0:
				l = 0
			l /= 100
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		a = a.value
		b = b.value

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

		return _Reference.color(_LABColor(l, a, b, alpha))

	def lch(self, l: _number, c: _number, h: _number | _angle,
		alpha: _number | _percentage = _number(1.0)):

		try:
			l = l.value
			if l < 0:
				l = 0
			l /= 100
		except ValueError as e:
			raise _InvalidArgumentValueError(0, str(e))

		c = c.value
		if c < 0:
			c = 0

		try:
			h = h.to_hue()
		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(_LCHColor(l, c, h, alpha))

	hwba = _alias('hwb')

# End of file.