Mercurial > libervia-backend
annotate libervia/frontends/tools/css_color.py @ 4153:9162d3480b9e
plugin XEP-0280: minor parsing improvement.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 22 Nov 2023 14:53:07 +0100 |
parents | 26b7ed2817da |
children |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # CSS color parsing |
3479 | 5 # Copyright (C) 2009-2021 Jérome-Poisson |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
20 from libervia.backend.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
21 |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 log = getLogger(__name__) |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 CSS_COLORS = { |
3028 | 26 "black": "000000", |
27 "silver": "c0c0c0", | |
28 "gray": "808080", | |
29 "white": "ffffff", | |
30 "maroon": "800000", | |
31 "red": "ff0000", | |
32 "purple": "800080", | |
33 "fuchsia": "ff00ff", | |
34 "green": "008000", | |
35 "lime": "00ff00", | |
36 "olive": "808000", | |
37 "yellow": "ffff00", | |
38 "navy": "000080", | |
39 "blue": "0000ff", | |
40 "teal": "008080", | |
41 "aqua": "00ffff", | |
42 "orange": "ffa500", | |
43 "aliceblue": "f0f8ff", | |
44 "antiquewhite": "faebd7", | |
45 "aquamarine": "7fffd4", | |
46 "azure": "f0ffff", | |
47 "beige": "f5f5dc", | |
48 "bisque": "ffe4c4", | |
49 "blanchedalmond": "ffebcd", | |
50 "blueviolet": "8a2be2", | |
51 "brown": "a52a2a", | |
52 "burlywood": "deb887", | |
53 "cadetblue": "5f9ea0", | |
54 "chartreuse": "7fff00", | |
55 "chocolate": "d2691e", | |
56 "coral": "ff7f50", | |
57 "cornflowerblue": "6495ed", | |
58 "cornsilk": "fff8dc", | |
59 "crimson": "dc143c", | |
60 "darkblue": "00008b", | |
61 "darkcyan": "008b8b", | |
62 "darkgoldenrod": "b8860b", | |
63 "darkgray": "a9a9a9", | |
64 "darkgreen": "006400", | |
65 "darkgrey": "a9a9a9", | |
66 "darkkhaki": "bdb76b", | |
67 "darkmagenta": "8b008b", | |
68 "darkolivegreen": "556b2f", | |
69 "darkorange": "ff8c00", | |
70 "darkorchid": "9932cc", | |
71 "darkred": "8b0000", | |
72 "darksalmon": "e9967a", | |
73 "darkseagreen": "8fbc8f", | |
74 "darkslateblue": "483d8b", | |
75 "darkslategray": "2f4f4f", | |
76 "darkslategrey": "2f4f4f", | |
77 "darkturquoise": "00ced1", | |
78 "darkviolet": "9400d3", | |
79 "deeppink": "ff1493", | |
80 "deepskyblue": "00bfff", | |
81 "dimgray": "696969", | |
82 "dimgrey": "696969", | |
83 "dodgerblue": "1e90ff", | |
84 "firebrick": "b22222", | |
85 "floralwhite": "fffaf0", | |
86 "forestgreen": "228b22", | |
87 "gainsboro": "dcdcdc", | |
88 "ghostwhite": "f8f8ff", | |
89 "gold": "ffd700", | |
90 "goldenrod": "daa520", | |
91 "greenyellow": "adff2f", | |
92 "grey": "808080", | |
93 "honeydew": "f0fff0", | |
94 "hotpink": "ff69b4", | |
95 "indianred": "cd5c5c", | |
96 "indigo": "4b0082", | |
97 "ivory": "fffff0", | |
98 "khaki": "f0e68c", | |
99 "lavender": "e6e6fa", | |
100 "lavenderblush": "fff0f5", | |
101 "lawngreen": "7cfc00", | |
102 "lemonchiffon": "fffacd", | |
103 "lightblue": "add8e6", | |
104 "lightcoral": "f08080", | |
105 "lightcyan": "e0ffff", | |
106 "lightgoldenrodyellow": "fafad2", | |
107 "lightgray": "d3d3d3", | |
108 "lightgreen": "90ee90", | |
109 "lightgrey": "d3d3d3", | |
110 "lightpink": "ffb6c1", | |
111 "lightsalmon": "ffa07a", | |
112 "lightseagreen": "20b2aa", | |
113 "lightskyblue": "87cefa", | |
114 "lightslategray": "778899", | |
115 "lightslategrey": "778899", | |
116 "lightsteelblue": "b0c4de", | |
117 "lightyellow": "ffffe0", | |
118 "limegreen": "32cd32", | |
119 "linen": "faf0e6", | |
120 "mediumaquamarine": "66cdaa", | |
121 "mediumblue": "0000cd", | |
122 "mediumorchid": "ba55d3", | |
123 "mediumpurple": "9370db", | |
124 "mediumseagreen": "3cb371", | |
125 "mediumslateblue": "7b68ee", | |
126 "mediumspringgreen": "00fa9a", | |
127 "mediumturquoise": "48d1cc", | |
128 "mediumvioletred": "c71585", | |
129 "midnightblue": "191970", | |
130 "mintcream": "f5fffa", | |
131 "mistyrose": "ffe4e1", | |
132 "moccasin": "ffe4b5", | |
133 "navajowhite": "ffdead", | |
134 "oldlace": "fdf5e6", | |
135 "olivedrab": "6b8e23", | |
136 "orangered": "ff4500", | |
137 "orchid": "da70d6", | |
138 "palegoldenrod": "eee8aa", | |
139 "palegreen": "98fb98", | |
140 "paleturquoise": "afeeee", | |
141 "palevioletred": "db7093", | |
142 "papayawhip": "ffefd5", | |
143 "peachpuff": "ffdab9", | |
144 "peru": "cd853f", | |
145 "pink": "ffc0cb", | |
146 "plum": "dda0dd", | |
147 "powderblue": "b0e0e6", | |
148 "rosybrown": "bc8f8f", | |
149 "royalblue": "4169e1", | |
150 "saddlebrown": "8b4513", | |
151 "salmon": "fa8072", | |
152 "sandybrown": "f4a460", | |
153 "seagreen": "2e8b57", | |
154 "seashell": "fff5ee", | |
155 "sienna": "a0522d", | |
156 "skyblue": "87ceeb", | |
157 "slateblue": "6a5acd", | |
158 "slategray": "708090", | |
159 "slategrey": "708090", | |
160 "snow": "fffafa", | |
161 "springgreen": "00ff7f", | |
162 "steelblue": "4682b4", | |
163 "tan": "d2b48c", | |
164 "thistle": "d8bfd8", | |
165 "tomato": "ff6347", | |
166 "turquoise": "40e0d0", | |
167 "violet": "ee82ee", | |
168 "wheat": "f5deb3", | |
169 "whitesmoke": "f5f5f5", | |
170 "yellowgreen": "9acd32", | |
171 "rebeccapurple": "663399", | |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 } |
3028 | 173 DEFAULT = "000000" |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 def parse(raw_value, as_string=True): |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 """parse CSS color value and return normalised value |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 @param raw_value(unicode): CSS value |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 @param as_string(bool): if True return a string, |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 else return a tuple of int |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 @return (unicode, tuple): normalised value |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 if as_string is True, value is 3 or 4 hex words (e.g. u"ff00aabb") |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 else value is a 3 or 4 tuple of int (e.g.: (255, 0, 170, 187)). |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 If present, the 4th value is the alpha channel |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 If value can't be parsed, a warning message is logged, and DEFAULT is returned |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 """ |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 raw_value = raw_value.strip().lower() |
3028 | 189 if raw_value.startswith("#"): |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 # we have a hexadecimal value |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 str_value = raw_value[1:] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
192 if len(raw_value) in (3, 4): |
3028 | 193 str_value = "".join([2 * v for v in str_value]) |
194 elif raw_value.startswith("rgb"): | |
195 left_p = raw_value.find("(") | |
196 right_p = raw_value.find(")") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 rgb_values = [v.strip() for v in raw_value[left_p + 1 : right_p].split(",")] |
3028 | 198 expected_len = 4 if raw_value.startswith("rgba") else 3 |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 if len(rgb_values) != expected_len: |
3028 | 200 log.warning("incorrect value: {}".format(raw_value)) |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 str_value = DEFAULT |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 else: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 int_values = [] |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 for rgb_v in rgb_values: |
3028 | 205 p_idx = rgb_v.find("%") |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 if p_idx == -1: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 # base 10 value |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 try: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 int_v = int(rgb_v) |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 if int_v > 255: |
3028 | 211 raise ValueError("value exceed 255") |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 int_values.append(int_v) |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 except ValueError: |
3028 | 214 log.warning("invalid int: {}".format(rgb_v)) |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 int_values.append(0) |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 else: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 # percentage |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 try: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 int_v = int(int(rgb_v[:p_idx]) / 100.0 * 255) |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 if int_v > 255: |
3028 | 221 raise ValueError("value exceed 255") |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 int_values.append(int_v) |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 except ValueError: |
3028 | 224 log.warning("invalid percent value: {}".format(rgb_v)) |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 int_values.append(0) |
3028 | 226 str_value = "".join(["{:02x}".format(v) for v in int_values]) |
227 elif raw_value.startswith("hsl"): | |
228 log.warning("hue-saturation-lightness not handled yet") # TODO | |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 str_value = DEFAULT |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 else: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 try: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 str_value = CSS_COLORS[raw_value] |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 except KeyError: |
3028 | 234 log.warning("unrecognised format: {}".format(raw_value)) |
2077
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 str_value = DEFAULT |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 if as_string: |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 return str_value |
95ad70ad815c
frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
240 return tuple( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
241 [ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
242 int(str_value[i] + str_value[i + 1], 16) |
3028 | 243 for i in range(0, len(str_value), 2) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 ] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
245 ) |