annotate sat_frontends/tools/css_color.py @ 2870:2f39682f6d40

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