Mercurial > libervia-backend
comparison libervia/frontends/tools/css_color.py @ 4074:26b7ed2817da
refactoring: rename `sat_frontends` to `libervia.frontends`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 14:12:38 +0200 |
parents | sat_frontends/tools/css_color.py@4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4073:7c5654c54fed | 4074:26b7ed2817da |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 | |
4 # CSS color parsing | |
5 # Copyright (C) 2009-2021 Jérome-Poisson | |
6 | |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 from libervia.backend.core.log import getLogger | |
21 | |
22 log = getLogger(__name__) | |
23 | |
24 | |
25 CSS_COLORS = { | |
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", | |
172 } | |
173 DEFAULT = "000000" | |
174 | |
175 | |
176 def parse(raw_value, as_string=True): | |
177 """parse CSS color value and return normalised value | |
178 | |
179 @param raw_value(unicode): CSS value | |
180 @param as_string(bool): if True return a string, | |
181 else return a tuple of int | |
182 @return (unicode, tuple): normalised value | |
183 if as_string is True, value is 3 or 4 hex words (e.g. u"ff00aabb") | |
184 else value is a 3 or 4 tuple of int (e.g.: (255, 0, 170, 187)). | |
185 If present, the 4th value is the alpha channel | |
186 If value can't be parsed, a warning message is logged, and DEFAULT is returned | |
187 """ | |
188 raw_value = raw_value.strip().lower() | |
189 if raw_value.startswith("#"): | |
190 # we have a hexadecimal value | |
191 str_value = raw_value[1:] | |
192 if len(raw_value) in (3, 4): | |
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(")") | |
197 rgb_values = [v.strip() for v in raw_value[left_p + 1 : right_p].split(",")] | |
198 expected_len = 4 if raw_value.startswith("rgba") else 3 | |
199 if len(rgb_values) != expected_len: | |
200 log.warning("incorrect value: {}".format(raw_value)) | |
201 str_value = DEFAULT | |
202 else: | |
203 int_values = [] | |
204 for rgb_v in rgb_values: | |
205 p_idx = rgb_v.find("%") | |
206 if p_idx == -1: | |
207 # base 10 value | |
208 try: | |
209 int_v = int(rgb_v) | |
210 if int_v > 255: | |
211 raise ValueError("value exceed 255") | |
212 int_values.append(int_v) | |
213 except ValueError: | |
214 log.warning("invalid int: {}".format(rgb_v)) | |
215 int_values.append(0) | |
216 else: | |
217 # percentage | |
218 try: | |
219 int_v = int(int(rgb_v[:p_idx]) / 100.0 * 255) | |
220 if int_v > 255: | |
221 raise ValueError("value exceed 255") | |
222 int_values.append(int_v) | |
223 except ValueError: | |
224 log.warning("invalid percent value: {}".format(rgb_v)) | |
225 int_values.append(0) | |
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 | |
229 str_value = DEFAULT | |
230 else: | |
231 try: | |
232 str_value = CSS_COLORS[raw_value] | |
233 except KeyError: | |
234 log.warning("unrecognised format: {}".format(raw_value)) | |
235 str_value = DEFAULT | |
236 | |
237 if as_string: | |
238 return str_value | |
239 else: | |
240 return tuple( | |
241 [ | |
242 int(str_value[i] + str_value[i + 1], 16) | |
243 for i in range(0, len(str_value), 2) | |
244 ] | |
245 ) |