Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/ImageColor.py: 39%
53 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
1#
2# The Python Imaging Library
3# $Id$
4#
5# map CSS3-style colour description strings to RGB
6#
7# History:
8# 2002-10-24 fl Added support for CSS-style color strings
9# 2002-12-15 fl Added RGBA support
10# 2004-03-27 fl Fixed remaining int() problems for Python 1.5.2
11# 2004-07-19 fl Fixed gray/grey spelling issues
12# 2009-03-05 fl Fixed rounding error in grayscale calculation
13#
14# Copyright (c) 2002-2004 by Secret Labs AB
15# Copyright (c) 2002-2004 by Fredrik Lundh
16#
17# See the README file for information on usage and redistribution.
18#
20import re
22from . import Image
25def getrgb(color):
26 """
27 Convert a color string to an RGB or RGBA tuple. If the string cannot be
28 parsed, this function raises a :py:exc:`ValueError` exception.
30 .. versionadded:: 1.1.4
32 :param color: A color string
33 :return: ``(red, green, blue[, alpha])``
34 """
35 if len(color) > 100: 35 ↛ 36line 35 didn't jump to line 36, because the condition on line 35 was never true
36 raise ValueError("color specifier is too long")
37 color = color.lower()
39 rgb = colormap.get(color, None)
40 if rgb:
41 if isinstance(rgb, tuple):
42 return rgb
43 colormap[color] = rgb = getrgb(rgb)
44 return rgb
46 # check for known string formats
47 if re.match("#[a-f0-9]{3}$", color): 47 ↛ 48line 47 didn't jump to line 48, because the condition on line 47 was never true
48 return int(color[1] * 2, 16), int(color[2] * 2, 16), int(color[3] * 2, 16)
50 if re.match("#[a-f0-9]{4}$", color): 50 ↛ 51line 50 didn't jump to line 51, because the condition on line 50 was never true
51 return (
52 int(color[1] * 2, 16),
53 int(color[2] * 2, 16),
54 int(color[3] * 2, 16),
55 int(color[4] * 2, 16),
56 )
58 if re.match("#[a-f0-9]{6}$", color): 58 ↛ 61line 58 didn't jump to line 61, because the condition on line 58 was never false
59 return int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16)
61 if re.match("#[a-f0-9]{8}$", color):
62 return (
63 int(color[1:3], 16),
64 int(color[3:5], 16),
65 int(color[5:7], 16),
66 int(color[7:9], 16),
67 )
69 m = re.match(r"rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
70 if m:
71 return int(m.group(1)), int(m.group(2)), int(m.group(3))
73 m = re.match(r"rgb\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$", color)
74 if m:
75 return (
76 int((int(m.group(1)) * 255) / 100.0 + 0.5),
77 int((int(m.group(2)) * 255) / 100.0 + 0.5),
78 int((int(m.group(3)) * 255) / 100.0 + 0.5),
79 )
81 m = re.match(
82 r"hsl\(\s*(\d+\.?\d*)\s*,\s*(\d+\.?\d*)%\s*,\s*(\d+\.?\d*)%\s*\)$", color
83 )
84 if m:
85 from colorsys import hls_to_rgb
87 rgb = hls_to_rgb(
88 float(m.group(1)) / 360.0,
89 float(m.group(3)) / 100.0,
90 float(m.group(2)) / 100.0,
91 )
92 return (
93 int(rgb[0] * 255 + 0.5),
94 int(rgb[1] * 255 + 0.5),
95 int(rgb[2] * 255 + 0.5),
96 )
98 m = re.match(
99 r"hs[bv]\(\s*(\d+\.?\d*)\s*,\s*(\d+\.?\d*)%\s*,\s*(\d+\.?\d*)%\s*\)$", color
100 )
101 if m:
102 from colorsys import hsv_to_rgb
104 rgb = hsv_to_rgb(
105 float(m.group(1)) / 360.0,
106 float(m.group(2)) / 100.0,
107 float(m.group(3)) / 100.0,
108 )
109 return (
110 int(rgb[0] * 255 + 0.5),
111 int(rgb[1] * 255 + 0.5),
112 int(rgb[2] * 255 + 0.5),
113 )
115 m = re.match(r"rgba\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
116 if m:
117 return int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))
118 raise ValueError(f"unknown color specifier: {repr(color)}")
121def getcolor(color, mode):
122 """
123 Same as :py:func:`~PIL.ImageColor.getrgb`, but converts the RGB value to a
124 greyscale value if ``mode`` is not color or a palette image. If the string
125 cannot be parsed, this function raises a :py:exc:`ValueError` exception.
127 .. versionadded:: 1.1.4
129 :param color: A color string
130 :param mode: Convert result to this mode
131 :return: ``(graylevel[, alpha]) or (red, green, blue[, alpha])``
132 """
133 # same as getrgb, but converts the result to the given mode
134 color, alpha = getrgb(color), 255
135 if len(color) == 4: 135 ↛ 136line 135 didn't jump to line 136, because the condition on line 135 was never true
136 color, alpha = color[:3], color[3]
138 if Image.getmodebase(mode) == "L": 138 ↛ 139line 138 didn't jump to line 139, because the condition on line 138 was never true
139 r, g, b = color
140 # ITU-R Recommendation 601-2 for nonlinear RGB
141 # scaled to 24 bits to match the convert's implementation.
142 color = (r * 19595 + g * 38470 + b * 7471 + 0x8000) >> 16
143 if mode[-1] == "A":
144 return color, alpha
145 else:
146 if mode[-1] == "A": 146 ↛ 147line 146 didn't jump to line 147, because the condition on line 146 was never true
147 return color + (alpha,)
148 return color
151colormap = {
152 # X11 colour table from https://drafts.csswg.org/css-color-4/, with
153 # gray/grey spelling issues fixed. This is a superset of HTML 4.0
154 # colour names used in CSS 1.
155 "aliceblue": "#f0f8ff",
156 "antiquewhite": "#faebd7",
157 "aqua": "#00ffff",
158 "aquamarine": "#7fffd4",
159 "azure": "#f0ffff",
160 "beige": "#f5f5dc",
161 "bisque": "#ffe4c4",
162 "black": "#000000",
163 "blanchedalmond": "#ffebcd",
164 "blue": "#0000ff",
165 "blueviolet": "#8a2be2",
166 "brown": "#a52a2a",
167 "burlywood": "#deb887",
168 "cadetblue": "#5f9ea0",
169 "chartreuse": "#7fff00",
170 "chocolate": "#d2691e",
171 "coral": "#ff7f50",
172 "cornflowerblue": "#6495ed",
173 "cornsilk": "#fff8dc",
174 "crimson": "#dc143c",
175 "cyan": "#00ffff",
176 "darkblue": "#00008b",
177 "darkcyan": "#008b8b",
178 "darkgoldenrod": "#b8860b",
179 "darkgray": "#a9a9a9",
180 "darkgrey": "#a9a9a9",
181 "darkgreen": "#006400",
182 "darkkhaki": "#bdb76b",
183 "darkmagenta": "#8b008b",
184 "darkolivegreen": "#556b2f",
185 "darkorange": "#ff8c00",
186 "darkorchid": "#9932cc",
187 "darkred": "#8b0000",
188 "darksalmon": "#e9967a",
189 "darkseagreen": "#8fbc8f",
190 "darkslateblue": "#483d8b",
191 "darkslategray": "#2f4f4f",
192 "darkslategrey": "#2f4f4f",
193 "darkturquoise": "#00ced1",
194 "darkviolet": "#9400d3",
195 "deeppink": "#ff1493",
196 "deepskyblue": "#00bfff",
197 "dimgray": "#696969",
198 "dimgrey": "#696969",
199 "dodgerblue": "#1e90ff",
200 "firebrick": "#b22222",
201 "floralwhite": "#fffaf0",
202 "forestgreen": "#228b22",
203 "fuchsia": "#ff00ff",
204 "gainsboro": "#dcdcdc",
205 "ghostwhite": "#f8f8ff",
206 "gold": "#ffd700",
207 "goldenrod": "#daa520",
208 "gray": "#808080",
209 "grey": "#808080",
210 "green": "#008000",
211 "greenyellow": "#adff2f",
212 "honeydew": "#f0fff0",
213 "hotpink": "#ff69b4",
214 "indianred": "#cd5c5c",
215 "indigo": "#4b0082",
216 "ivory": "#fffff0",
217 "khaki": "#f0e68c",
218 "lavender": "#e6e6fa",
219 "lavenderblush": "#fff0f5",
220 "lawngreen": "#7cfc00",
221 "lemonchiffon": "#fffacd",
222 "lightblue": "#add8e6",
223 "lightcoral": "#f08080",
224 "lightcyan": "#e0ffff",
225 "lightgoldenrodyellow": "#fafad2",
226 "lightgreen": "#90ee90",
227 "lightgray": "#d3d3d3",
228 "lightgrey": "#d3d3d3",
229 "lightpink": "#ffb6c1",
230 "lightsalmon": "#ffa07a",
231 "lightseagreen": "#20b2aa",
232 "lightskyblue": "#87cefa",
233 "lightslategray": "#778899",
234 "lightslategrey": "#778899",
235 "lightsteelblue": "#b0c4de",
236 "lightyellow": "#ffffe0",
237 "lime": "#00ff00",
238 "limegreen": "#32cd32",
239 "linen": "#faf0e6",
240 "magenta": "#ff00ff",
241 "maroon": "#800000",
242 "mediumaquamarine": "#66cdaa",
243 "mediumblue": "#0000cd",
244 "mediumorchid": "#ba55d3",
245 "mediumpurple": "#9370db",
246 "mediumseagreen": "#3cb371",
247 "mediumslateblue": "#7b68ee",
248 "mediumspringgreen": "#00fa9a",
249 "mediumturquoise": "#48d1cc",
250 "mediumvioletred": "#c71585",
251 "midnightblue": "#191970",
252 "mintcream": "#f5fffa",
253 "mistyrose": "#ffe4e1",
254 "moccasin": "#ffe4b5",
255 "navajowhite": "#ffdead",
256 "navy": "#000080",
257 "oldlace": "#fdf5e6",
258 "olive": "#808000",
259 "olivedrab": "#6b8e23",
260 "orange": "#ffa500",
261 "orangered": "#ff4500",
262 "orchid": "#da70d6",
263 "palegoldenrod": "#eee8aa",
264 "palegreen": "#98fb98",
265 "paleturquoise": "#afeeee",
266 "palevioletred": "#db7093",
267 "papayawhip": "#ffefd5",
268 "peachpuff": "#ffdab9",
269 "peru": "#cd853f",
270 "pink": "#ffc0cb",
271 "plum": "#dda0dd",
272 "powderblue": "#b0e0e6",
273 "purple": "#800080",
274 "rebeccapurple": "#663399",
275 "red": "#ff0000",
276 "rosybrown": "#bc8f8f",
277 "royalblue": "#4169e1",
278 "saddlebrown": "#8b4513",
279 "salmon": "#fa8072",
280 "sandybrown": "#f4a460",
281 "seagreen": "#2e8b57",
282 "seashell": "#fff5ee",
283 "sienna": "#a0522d",
284 "silver": "#c0c0c0",
285 "skyblue": "#87ceeb",
286 "slateblue": "#6a5acd",
287 "slategray": "#708090",
288 "slategrey": "#708090",
289 "snow": "#fffafa",
290 "springgreen": "#00ff7f",
291 "steelblue": "#4682b4",
292 "tan": "#d2b48c",
293 "teal": "#008080",
294 "thistle": "#d8bfd8",
295 "tomato": "#ff6347",
296 "turquoise": "#40e0d0",
297 "violet": "#ee82ee",
298 "wheat": "#f5deb3",
299 "white": "#ffffff",
300 "whitesmoke": "#f5f5f5",
301 "yellow": "#ffff00",
302 "yellowgreen": "#9acd32",
303}