annotate frontends/src/tools/css_color.py @ 2306:07deebea71f3

jp (common): fixed the use of --last-item when "-n [node]" is used
author Goffi <goffi@goffi.org>
date Wed, 05 Jul 2017 15:04:45 +0200
parents 95ad70ad815c
children 8b37a62336c3
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
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérome-Poisson
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
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
21 log = getLogger(__name__)
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
22
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 CSS_COLORS = {
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
25 u"black": u"000000",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
26 u"silver": u"c0c0c0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
27 u"gray": u"808080",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
28 u"white": u"ffffff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
29 u"maroon": u"800000",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
30 u"red": u"ff0000",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
31 u"purple": u"800080",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
32 u"fuchsia": u"ff00ff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
33 u"green": u"008000",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
34 u"lime": u"00ff00",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
35 u"olive": u"808000",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
36 u"yellow": u"ffff00",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
37 u"navy": u"000080",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
38 u"blue": u"0000ff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
39 u"teal": u"008080",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
40 u"aqua": u"00ffff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
41 u"orange": u"ffa500",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
42 u"aliceblue": u"f0f8ff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
43 u"antiquewhite": u"faebd7",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
44 u"aquamarine": u"7fffd4",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
45 u"azure": u"f0ffff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
46 u"beige": u"f5f5dc",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
47 u"bisque": u"ffe4c4",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
48 u"blanchedalmond": u"ffebcd",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
49 u"blueviolet": u"8a2be2",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
50 u"brown": u"a52a2a",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
51 u"burlywood": u"deb887",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
52 u"cadetblue": u"5f9ea0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
53 u"chartreuse": u"7fff00",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
54 u"chocolate": u"d2691e",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
55 u"coral": u"ff7f50",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
56 u"cornflowerblue": u"6495ed",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
57 u"cornsilk": u"fff8dc",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
58 u"crimson": u"dc143c",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
59 u"darkblue": u"00008b",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
60 u"darkcyan": u"008b8b",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
61 u"darkgoldenrod": u"b8860b",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
62 u"darkgray": u"a9a9a9",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
63 u"darkgreen": u"006400",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
64 u"darkgrey": u"a9a9a9",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
65 u"darkkhaki": u"bdb76b",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
66 u"darkmagenta": u"8b008b",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
67 u"darkolivegreen": u"556b2f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
68 u"darkorange": u"ff8c00",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
69 u"darkorchid": u"9932cc",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
70 u"darkred": u"8b0000",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
71 u"darksalmon": u"e9967a",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
72 u"darkseagreen": u"8fbc8f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
73 u"darkslateblue": u"483d8b",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
74 u"darkslategray": u"2f4f4f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
75 u"darkslategrey": u"2f4f4f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
76 u"darkturquoise": u"00ced1",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
77 u"darkviolet": u"9400d3",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
78 u"deeppink": u"ff1493",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
79 u"deepskyblue": u"00bfff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
80 u"dimgray": u"696969",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
81 u"dimgrey": u"696969",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
82 u"dodgerblue": u"1e90ff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
83 u"firebrick": u"b22222",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
84 u"floralwhite": u"fffaf0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
85 u"forestgreen": u"228b22",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
86 u"gainsboro": u"dcdcdc",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
87 u"ghostwhite": u"f8f8ff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
88 u"gold": u"ffd700",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
89 u"goldenrod": u"daa520",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
90 u"greenyellow": u"adff2f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
91 u"grey": u"808080",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
92 u"honeydew": u"f0fff0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
93 u"hotpink": u"ff69b4",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
94 u"indianred": u"cd5c5c",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
95 u"indigo": u"4b0082",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
96 u"ivory": u"fffff0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
97 u"khaki": u"f0e68c",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
98 u"lavender": u"e6e6fa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
99 u"lavenderblush": u"fff0f5",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
100 u"lawngreen": u"7cfc00",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
101 u"lemonchiffon": u"fffacd",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
102 u"lightblue": u"add8e6",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
103 u"lightcoral": u"f08080",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
104 u"lightcyan": u"e0ffff",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
105 u"lightgoldenrodyellow": u"fafad2",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
106 u"lightgray": u"d3d3d3",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
107 u"lightgreen": u"90ee90",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
108 u"lightgrey": u"d3d3d3",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
109 u"lightpink": u"ffb6c1",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
110 u"lightsalmon": u"ffa07a",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
111 u"lightseagreen": u"20b2aa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
112 u"lightskyblue": u"87cefa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
113 u"lightslategray": u"778899",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
114 u"lightslategrey": u"778899",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
115 u"lightsteelblue": u"b0c4de",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
116 u"lightyellow": u"ffffe0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
117 u"limegreen": u"32cd32",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
118 u"linen": u"faf0e6",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
119 u"mediumaquamarine": u"66cdaa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
120 u"mediumblue": u"0000cd",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
121 u"mediumorchid": u"ba55d3",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
122 u"mediumpurple": u"9370db",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
123 u"mediumseagreen": u"3cb371",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
124 u"mediumslateblue": u"7b68ee",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
125 u"mediumspringgreen": u"00fa9a",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
126 u"mediumturquoise": u"48d1cc",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
127 u"mediumvioletred": u"c71585",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
128 u"midnightblue": u"191970",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
129 u"mintcream": u"f5fffa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
130 u"mistyrose": u"ffe4e1",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
131 u"moccasin": u"ffe4b5",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
132 u"navajowhite": u"ffdead",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
133 u"oldlace": u"fdf5e6",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
134 u"olivedrab": u"6b8e23",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
135 u"orangered": u"ff4500",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
136 u"orchid": u"da70d6",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
137 u"palegoldenrod": u"eee8aa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
138 u"palegreen": u"98fb98",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
139 u"paleturquoise": u"afeeee",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
140 u"palevioletred": u"db7093",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
141 u"papayawhip": u"ffefd5",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
142 u"peachpuff": u"ffdab9",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
143 u"peru": u"cd853f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
144 u"pink": u"ffc0cb",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
145 u"plum": u"dda0dd",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
146 u"powderblue": u"b0e0e6",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
147 u"rosybrown": u"bc8f8f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
148 u"royalblue": u"4169e1",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
149 u"saddlebrown": u"8b4513",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
150 u"salmon": u"fa8072",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
151 u"sandybrown": u"f4a460",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
152 u"seagreen": u"2e8b57",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
153 u"seashell": u"fff5ee",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
154 u"sienna": u"a0522d",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
155 u"skyblue": u"87ceeb",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
156 u"slateblue": u"6a5acd",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
157 u"slategray": u"708090",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
158 u"slategrey": u"708090",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
159 u"snow": u"fffafa",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
160 u"springgreen": u"00ff7f",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
161 u"steelblue": u"4682b4",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
162 u"tan": u"d2b48c",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
163 u"thistle": u"d8bfd8",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
164 u"tomato": u"ff6347",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
165 u"turquoise": u"40e0d0",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
166 u"violet": u"ee82ee",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
167 u"wheat": u"f5deb3",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
168 u"whitesmoke": u"f5f5f5",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
169 u"yellowgreen": u"9acd32",
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
170 u"rebeccapurple": u"663399"
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
171 }
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
172 DEFAULT = u"000000"
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
173
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 def parse(raw_value, as_string=True):
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
176 """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
177
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
178 @param raw_value(unicode): CSS value
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
179 @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
180 else return a tuple of int
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
181 @return (unicode, tuple): normalised value
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
182 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
183 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
184 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
185 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
186 """
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
187 raw_value = raw_value.strip().lower()
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
188 if raw_value.startswith(u'#'):
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
189 # we have a hexadecimal value
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
190 str_value = raw_value[1:]
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
191 if len(raw_value) in (3,4):
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
192 str_value = u''.join([2*v for v in str_value])
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
193 elif raw_value.startswith(u'rgb'):
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
194 left_p = raw_value.find(u'(')
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
195 right_p = raw_value.find(u')')
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
196 rgb_values = [v.strip() for v in raw_value[left_p+1:right_p].split(',')]
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
197 expected_len = 4 if raw_value.startswith(u'rgba') else 3
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
198 if len(rgb_values) != expected_len:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
199 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
200 str_value = DEFAULT
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
201 else:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
202 int_values = []
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
203 for rgb_v in rgb_values:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
204 p_idx = rgb_v.find(u'%')
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
205 if p_idx == -1:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
206 # base 10 value
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
207 try:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
208 int_v = int(rgb_v)
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
209 if int_v > 255:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
210 raise ValueError(u"value exceed 255")
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
211 int_values.append(int_v)
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
212 except ValueError:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
213 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
214 int_values.append(0)
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
215 else:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
216 # percentage
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
217 try:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
218 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
219 if int_v > 255:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
220 raise ValueError(u"value exceed 255")
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
221 int_values.append(int_v)
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
222 except ValueError:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
223 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
224 int_values.append(0)
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
225 str_value = u''.join([u"{:02x}".format(v) for v in int_values])
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
226 elif raw_value.startswith(u'hsl'):
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
227 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
228 str_value = DEFAULT
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
229 else:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
230 try:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
231 str_value = CSS_COLORS[raw_value]
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
232 except KeyError:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
233 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
234 str_value = DEFAULT
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
235
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
236 if as_string:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
237 return str_value
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
238 else:
95ad70ad815c frontends (tools): added a module to parse CSS colors
Goffi <goffi@goffi.org>
parents:
diff changeset
239 return tuple([int(str_value[i]+str_value[i+1], 16) for i in xrange(0, len(str_value), 2)])