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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
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
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
26 "black": "000000",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
27 "silver": "c0c0c0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
28 "gray": "808080",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
29 "white": "ffffff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
30 "maroon": "800000",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
31 "red": "ff0000",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
32 "purple": "800080",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
33 "fuchsia": "ff00ff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
34 "green": "008000",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
35 "lime": "00ff00",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
36 "olive": "808000",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
37 "yellow": "ffff00",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
38 "navy": "000080",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 "blue": "0000ff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
40 "teal": "008080",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
41 "aqua": "00ffff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
42 "orange": "ffa500",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
43 "aliceblue": "f0f8ff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
44 "antiquewhite": "faebd7",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
45 "aquamarine": "7fffd4",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
46 "azure": "f0ffff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 "beige": "f5f5dc",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
48 "bisque": "ffe4c4",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
49 "blanchedalmond": "ffebcd",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
50 "blueviolet": "8a2be2",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
51 "brown": "a52a2a",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
52 "burlywood": "deb887",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
53 "cadetblue": "5f9ea0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
54 "chartreuse": "7fff00",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 "chocolate": "d2691e",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 "coral": "ff7f50",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
57 "cornflowerblue": "6495ed",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 "cornsilk": "fff8dc",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
59 "crimson": "dc143c",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
60 "darkblue": "00008b",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 "darkcyan": "008b8b",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 "darkgoldenrod": "b8860b",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
63 "darkgray": "a9a9a9",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 "darkgreen": "006400",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
65 "darkgrey": "a9a9a9",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
66 "darkkhaki": "bdb76b",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
67 "darkmagenta": "8b008b",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
68 "darkolivegreen": "556b2f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 "darkorange": "ff8c00",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
70 "darkorchid": "9932cc",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
71 "darkred": "8b0000",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
72 "darksalmon": "e9967a",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 "darkseagreen": "8fbc8f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
74 "darkslateblue": "483d8b",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
75 "darkslategray": "2f4f4f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
76 "darkslategrey": "2f4f4f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
77 "darkturquoise": "00ced1",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
78 "darkviolet": "9400d3",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
79 "deeppink": "ff1493",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
80 "deepskyblue": "00bfff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
81 "dimgray": "696969",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
82 "dimgrey": "696969",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
83 "dodgerblue": "1e90ff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
84 "firebrick": "b22222",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
85 "floralwhite": "fffaf0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
86 "forestgreen": "228b22",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
87 "gainsboro": "dcdcdc",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 "ghostwhite": "f8f8ff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
89 "gold": "ffd700",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
90 "goldenrod": "daa520",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
91 "greenyellow": "adff2f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
92 "grey": "808080",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
93 "honeydew": "f0fff0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
94 "hotpink": "ff69b4",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
95 "indianred": "cd5c5c",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
96 "indigo": "4b0082",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
97 "ivory": "fffff0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 "khaki": "f0e68c",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
99 "lavender": "e6e6fa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
100 "lavenderblush": "fff0f5",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
101 "lawngreen": "7cfc00",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
102 "lemonchiffon": "fffacd",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
103 "lightblue": "add8e6",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
104 "lightcoral": "f08080",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
105 "lightcyan": "e0ffff",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
106 "lightgoldenrodyellow": "fafad2",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
107 "lightgray": "d3d3d3",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 "lightgreen": "90ee90",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
109 "lightgrey": "d3d3d3",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
110 "lightpink": "ffb6c1",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
111 "lightsalmon": "ffa07a",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
112 "lightseagreen": "20b2aa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
113 "lightskyblue": "87cefa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
114 "lightslategray": "778899",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
115 "lightslategrey": "778899",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
116 "lightsteelblue": "b0c4de",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
117 "lightyellow": "ffffe0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
118 "limegreen": "32cd32",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
119 "linen": "faf0e6",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
120 "mediumaquamarine": "66cdaa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
121 "mediumblue": "0000cd",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
122 "mediumorchid": "ba55d3",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
123 "mediumpurple": "9370db",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
124 "mediumseagreen": "3cb371",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
125 "mediumslateblue": "7b68ee",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
126 "mediumspringgreen": "00fa9a",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
127 "mediumturquoise": "48d1cc",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
128 "mediumvioletred": "c71585",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
129 "midnightblue": "191970",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
130 "mintcream": "f5fffa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
131 "mistyrose": "ffe4e1",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
132 "moccasin": "ffe4b5",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
133 "navajowhite": "ffdead",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
134 "oldlace": "fdf5e6",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
135 "olivedrab": "6b8e23",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
136 "orangered": "ff4500",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
137 "orchid": "da70d6",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
138 "palegoldenrod": "eee8aa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
139 "palegreen": "98fb98",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
140 "paleturquoise": "afeeee",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
141 "palevioletred": "db7093",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
142 "papayawhip": "ffefd5",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
143 "peachpuff": "ffdab9",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
144 "peru": "cd853f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
145 "pink": "ffc0cb",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
146 "plum": "dda0dd",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
147 "powderblue": "b0e0e6",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
148 "rosybrown": "bc8f8f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
149 "royalblue": "4169e1",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
150 "saddlebrown": "8b4513",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
151 "salmon": "fa8072",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
152 "sandybrown": "f4a460",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
153 "seagreen": "2e8b57",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
154 "seashell": "fff5ee",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
155 "sienna": "a0522d",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
156 "skyblue": "87ceeb",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
157 "slateblue": "6a5acd",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
158 "slategray": "708090",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
159 "slategrey": "708090",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
160 "snow": "fffafa",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
161 "springgreen": "00ff7f",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
162 "steelblue": "4682b4",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
163 "tan": "d2b48c",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
164 "thistle": "d8bfd8",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
165 "tomato": "ff6347",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
166 "turquoise": "40e0d0",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
167 "violet": "ee82ee",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
168 "wheat": "f5deb3",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
169 "whitesmoke": "f5f5f5",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
170 "yellowgreen": "9acd32",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
171 "rebeccapurple": "663399",
2077
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
172 }
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
193 str_value = "".join([2 * v for v in str_value])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
194 elif raw_value.startswith("rgb"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
195 left_p = raw_value.find("(")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
226 str_value = "".join(["{:02x}".format(v) for v in int_values])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
227 elif raw_value.startswith("hsl"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
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 )