| 1 |
# Copyright (c) 2005 Gavin E. Crooks |
| 2 |
# |
| 3 |
# This software is distributed under the MIT Open Source License. |
| 4 |
# <http://www.opensource.org/licenses/mit-license.html> |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a |
| 7 |
# copy of this software and associated documentation files (the "Software"), |
| 8 |
# to deal in the Software without restriction, including without limitation |
| 9 |
# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 |
# and/or sell copies of the Software, and to permit persons to whom the |
| 11 |
# Software is furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included |
| 14 |
# in all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
|
| 24 |
"""Color specifications using CSS2 (Cascading Style Sheet) syntax.""" |
| 25 |
|
| 26 |
from typing import Any, List |
| 27 |
|
| 28 |
|
| 29 |
class Color(object): |
| 30 |
"""Color specifications using CSS2 (Cascading Style Sheet) syntax. |
| 31 |
|
| 32 |
http://www.w3.org/TR/REC-CSS2/syndata.html#color-units |
| 33 |
|
| 34 |
Usage: |
| 35 |
|
| 36 |
red = Color(255,0,0) |
| 37 |
red = Color(1., 0., 0.) |
| 38 |
red = Color.by_name("red") |
| 39 |
red = Color.from_rgb(1.,0.,0.) |
| 40 |
red = Color.from_rgb(255,0,0) |
| 41 |
red = Color.from_hsl(0.,1., 0.5) |
| 42 |
|
| 43 |
red = Color.from_string("red") |
| 44 |
red = Color.from_string("RED") |
| 45 |
red = Color.from_string("#F00") |
| 46 |
red = Color.from_string("#FF0000") |
| 47 |
red = Color.from_string("rgb(255, 0, 0)") |
| 48 |
red = Color.from_string("rgb(100%, 0%, 0%)") |
| 49 |
red = Color.from_string("hsl(0, 100%, 50%)") |
| 50 |
|
| 51 |
""" |
| 52 |
|
| 53 |
def __init__(self, red: float, green: float, blue: float) -> None: |
| 54 |
if not (type(red) is type(green) is type(blue)): |
| 55 |
raise TypeError("Mixed floats and integers?") |
| 56 |
# Convert integer RBG values in [0, 255] to floats in [0, 1] |
| 57 |
if isinstance(red, int): |
| 58 |
red /= 255.0 |
| 59 |
if isinstance(green, int): |
| 60 |
green /= 255.0 |
| 61 |
if isinstance(blue, int): |
| 62 |
blue /= 255.0 |
| 63 |
# Clip RBG values to [0, 1] |
| 64 |
self.red = max(0.0, min(red, 1.0)) |
| 65 |
self.green = max(0.0, min(green, 1.0)) |
| 66 |
self.blue = max(0.0, min(blue, 1.0)) |
| 67 |
|
| 68 |
@staticmethod |
| 69 |
def names() -> List[str]: |
| 70 |
"Return a list of standard color names." |
| 71 |
return list(_std_colors.keys()) |
| 72 |
|
| 73 |
@classmethod |
| 74 |
def from_rgb(cls, r: float, g: float, b: float) -> "Color": |
| 75 |
return cls(r, g, b) |
| 76 |
|
| 77 |
@classmethod |
| 78 |
def from_hsl(cls, hue_angle: float, saturation: float, lightness: float) -> "Color": |
| 79 |
def hue_to_rgb(v1: float, v2: float, vH: float) -> float: |
| 80 |
if vH < 0.0: |
| 81 |
vH += 1.0 |
| 82 |
if vH > 1.0: |
| 83 |
vH -= 1.0 # pragma: no cover (Grandfathered in) |
| 84 |
if vH * 6.0 < 1.0: |
| 85 |
return v1 + (v2 - v1) * 6.0 * vH |
| 86 |
if vH * 2.0 < 1.0: |
| 87 |
return v2 |
| 88 |
if vH * 3.0 < 2.0: |
| 89 |
return v1 + (v2 - v1) * ((2.0 / 3.0) - vH) * 6.0 # pragma: no cover |
| 90 |
return v1 |
| 91 |
|
| 92 |
hue = (((hue_angle % 360.0) + 360.0) % 360.0) / 360.0 |
| 93 |
|
| 94 |
if not (saturation >= 0.0 and saturation <= 1.0): |
| 95 |
raise ValueError( |
| 96 |
"Out-of-range saturation %f" % saturation |
| 97 |
) # pragma: no cover |
| 98 |
if not (lightness >= 0.0 and lightness <= 1.0): |
| 99 |
raise ValueError( |
| 100 |
"Out-of-range lightness %f" % lightness |
| 101 |
) # pragma: no cover |
| 102 |
|
| 103 |
if saturation == 0: |
| 104 |
# greyscale |
| 105 |
return cls.from_rgb(lightness, lightness, lightness) |
| 106 |
|
| 107 |
if lightness < 0.5: |
| 108 |
v2 = lightness * (1.0 + saturation) |
| 109 |
else: |
| 110 |
v2 = (lightness + saturation) - (saturation * lightness) |
| 111 |
|
| 112 |
v1 = 2.0 * lightness - v2 |
| 113 |
r = hue_to_rgb(v1, v2, hue + (1.0 / 3.0)) |
| 114 |
g = hue_to_rgb(v1, v2, hue) |
| 115 |
b = hue_to_rgb(v1, v2, hue - (1.0 / 3.0)) |
| 116 |
|
| 117 |
return cls(r, g, b) |
| 118 |
|
| 119 |
@staticmethod |
| 120 |
def by_name(string: str) -> "Color": |
| 121 |
s = string.strip().lower().replace(" ", "") |
| 122 |
try: |
| 123 |
return _std_colors[s] |
| 124 |
except KeyError: |
| 125 |
raise ValueError("Unknown color name: %s" % s) |
| 126 |
|
| 127 |
@classmethod |
| 128 |
def from_string(cls, string: str) -> "Color": |
| 129 |
def to_frac(string: str) -> float: |
| 130 |
# string can be "255" or "100%" |
| 131 |
if string[-1] == "%": |
| 132 |
return float(string[0:-1]) / 100.0 |
| 133 |
else: |
| 134 |
return float(string) / 255.0 |
| 135 |
|
| 136 |
s = string.strip().lower().replace(" ", "").replace("_", "") |
| 137 |
|
| 138 |
if s in _std_colors: # "red" |
| 139 |
return _std_colors[s] |
| 140 |
|
| 141 |
if s[0] == "#": # "#fef" |
| 142 |
if len(s) == 4: |
| 143 |
r = int(s[1] + s[1], 16) |
| 144 |
g = int(s[2] + s[2], 16) |
| 145 |
b = int(s[3] + s[3], 16) |
| 146 |
return cls(r, g, b) |
| 147 |
elif len(s) == 7: # "#ff00aa" |
| 148 |
r = int(s[1:3], 16) |
| 149 |
g = int(s[3:5], 16) |
| 150 |
b = int(s[5:7], 16) |
| 151 |
return cls(r, g, b) |
| 152 |
else: |
| 153 |
raise ValueError("Cannot parse string: %s" % s) |
| 154 |
|
| 155 |
if s[0:4] == "rgb(" and s[-1] == ")": |
| 156 |
rgb = s[4:-1].split(",") |
| 157 |
if len(rgb) != 3: |
| 158 |
raise ValueError("Cannot parse string a: %s" % s) |
| 159 |
return cls(to_frac(rgb[0]), to_frac(rgb[1]), to_frac(rgb[2])) |
| 160 |
|
| 161 |
if s[0:4] == "hsl(" and s[-1] == ")": |
| 162 |
hsl = s[4:-1].split(",") |
| 163 |
if len(hsl) != 3: |
| 164 |
raise ValueError("Cannot parse string a: %s" % s) |
| 165 |
return cls.from_hsl(int(hsl[0]), to_frac(hsl[1]), to_frac(hsl[2])) |
| 166 |
|
| 167 |
raise ValueError("Cannot parse string: %s" % s) |
| 168 |
|
| 169 |
def __eq__(self, other: Any) -> bool: |
| 170 |
if not isinstance(other, self.__class__): |
| 171 |
return False |
| 172 |
req = int(0.5 + 255.0 * self.red) == int(0.5 + 255.0 * other.red) |
| 173 |
beq = int(0.5 + 255.0 * self.blue) == int(0.5 + 255.0 * other.blue) |
| 174 |
geq = int(0.5 + 255.0 * self.green) == int(0.5 + 255.0 * other.green) |
| 175 |
return req and beq and geq |
| 176 |
|
| 177 |
def __repr__(self) -> str: |
| 178 |
return "Color(%f,%f,%f)" % (self.red, self.green, self.blue) |
| 179 |
|
| 180 |
|
| 181 |
_std_colors = dict( |
| 182 |
aliceblue=Color(240, 248, 255), # f0f8ff |
| 183 |
antiquewhite=Color(250, 235, 215), # faebd7 |
| 184 |
aqua=Color(0, 255, 255), # 00ffff |
| 185 |
aquamarine=Color(127, 255, 212), # 7fffd4 |
| 186 |
azure=Color(240, 255, 255), # f0ffff |
| 187 |
beige=Color(245, 245, 220), # f5f5dc |
| 188 |
bisque=Color(255, 228, 196), # ffe4c4 |
| 189 |
black=Color(0, 0, 0), # 000000 |
| 190 |
blanchedalmond=Color(255, 235, 205), # ffebcd |
| 191 |
blue=Color(0, 0, 255), # 0000ff |
| 192 |
blueviolet=Color(138, 43, 226), # 8a2be2 |
| 193 |
brown=Color(165, 42, 42), # a52a2a |
| 194 |
burlywood=Color(222, 184, 135), # deb887 |
| 195 |
cadetblue=Color(95, 158, 160), # 5f9ea0 |
| 196 |
chartreuse=Color(127, 255, 0), # 7fff00 |
| 197 |
chocolate=Color(210, 105, 30), # d2691e |
| 198 |
coral=Color(255, 127, 80), # ff7f50 |
| 199 |
cornflowerblue=Color(100, 149, 237), # 6495ed |
| 200 |
cornsilk=Color(255, 248, 220), # fff8dc |
| 201 |
crimson=Color(220, 20, 60), # dc143c |
| 202 |
cyan=Color(0, 255, 255), # 00ffff |
| 203 |
darkblue=Color(0, 0, 139), # 00008b |
| 204 |
darkcyan=Color(0, 139, 139), # 008b8b |
| 205 |
darkgoldenrod=Color(184, 134, 11), # b8860b |
| 206 |
darkgray=Color(169, 169, 169), # a9a9a9 |
| 207 |
darkgreen=Color(0, 100, 0), # 006400 |
| 208 |
darkgrey=Color(169, 169, 169), # a9a9a9 |
| 209 |
darkkhaki=Color(189, 183, 107), # bdb76b |
| 210 |
darkmagenta=Color(139, 0, 139), # 8b008b |
| 211 |
darkolivegreen=Color(85, 107, 47), # 556b2f |
| 212 |
darkorange=Color(255, 140, 0), # ff8c00 |
| 213 |
darkorchid=Color(153, 50, 204), # 9932cc |
| 214 |
darkred=Color(139, 0, 0), # 8b0000 |
| 215 |
darksalmon=Color(233, 150, 122), # e9967a |
| 216 |
darkseagreen=Color(143, 188, 143), # 8fbc8f |
| 217 |
darkslateblue=Color(72, 61, 139), # 483d8b |
| 218 |
darkslategray=Color(47, 79, 79), # 2f4f4f |
| 219 |
darkslategrey=Color(47, 79, 79), # 2f4f4f |
| 220 |
darkturquoise=Color(0, 206, 209), # 00ced1 |
| 221 |
darkviolet=Color(148, 0, 211), # 9400d3 |
| 222 |
deeppink=Color(255, 20, 147), # ff1493 |
| 223 |
deepskyblue=Color(0, 191, 255), # 00bfff |
| 224 |
dimgray=Color(105, 105, 105), # 696969 |
| 225 |
dimgrey=Color(105, 105, 105), # 696969 |
| 226 |
dodgerblue=Color(30, 144, 255), # 1e90ff |
| 227 |
firebrick=Color(178, 34, 34), # b22222 |
| 228 |
floralwhite=Color(255, 250, 240), # fffaf0 |
| 229 |
forestgreen=Color(34, 139, 34), # 228b22 |
| 230 |
fuchsia=Color(255, 0, 255), # ff00ff |
| 231 |
gainsboro=Color(220, 220, 220), # dcdcdc |
| 232 |
ghostwhite=Color(248, 248, 255), # f8f8ff |
| 233 |
gold=Color(255, 215, 0), # ffd700 |
| 234 |
goldenrod=Color(218, 165, 32), # daa520 |
| 235 |
gray=Color(128, 128, 128), # 808080 |
| 236 |
green=Color(0, 128, 0), # 008000 |
| 237 |
greenyellow=Color(173, 255, 47), # adff2f |
| 238 |
grey=Color(128, 128, 128), # 808080 |
| 239 |
honeydew=Color(240, 255, 240), # f0fff0 |
| 240 |
hotpink=Color(255, 105, 180), # ff69b4 |
| 241 |
indianred=Color(205, 92, 92), # cd5c5c |
| 242 |
indigo=Color(75, 0, 130), # 4b0082 |
| 243 |
ivory=Color(255, 255, 240), # fffff0 |
| 244 |
khaki=Color(240, 230, 140), # f0e68c |
| 245 |
lavender=Color(230, 230, 250), # e6e6fa |
| 246 |
lavenderblush=Color(255, 240, 245), # fff0f5 |
| 247 |
lawngreen=Color(124, 252, 0), # 7cfc00 |
| 248 |
lemonchiffon=Color(255, 250, 205), # fffacd |
| 249 |
lightblue=Color(173, 216, 230), # add8e6 |
| 250 |
lightcoral=Color(240, 128, 128), # f08080 |
| 251 |
lightcyan=Color(224, 255, 255), # e0ffff |
| 252 |
lightgoldenrodyellow=Color(250, 250, 210), # fafad2 |
| 253 |
lightgray=Color(211, 211, 211), # d3d3d3 |
| 254 |
lightgreen=Color(144, 238, 144), # 90ee90 |
| 255 |
lightgrey=Color(211, 211, 211), # d3d3d3 |
| 256 |
lightpink=Color(255, 182, 193), # ffb6c1 |
| 257 |
lightsalmon=Color(255, 160, 122), # ffa07a |
| 258 |
lightseagreen=Color(32, 178, 170), # 20b2aa |
| 259 |
lightskyblue=Color(135, 206, 250), # 87cefa |
| 260 |
lightslategray=Color(119, 136, 153), # 778899 |
| 261 |
lightslategrey=Color(119, 136, 153), # 778899 |
| 262 |
lightsteelblue=Color(176, 196, 222), # b0c4de |
| 263 |
lightyellow=Color(255, 255, 224), # ffffe0 |
| 264 |
lime=Color(0, 255, 0), # 00ff00 |
| 265 |
limegreen=Color(50, 205, 50), # 32cd32 |
| 266 |
linen=Color(250, 240, 230), # faf0e6 |
| 267 |
magenta=Color(255, 0, 255), # ff00ff |
| 268 |
maroon=Color(128, 0, 0), # 800000 |
| 269 |
mediumaquamarine=Color(102, 205, 170), # 66cdaa |
| 270 |
mediumblue=Color(0, 0, 205), # 0000cd |
| 271 |
mediumorchid=Color(186, 85, 211), # ba55d3 |
| 272 |
mediumpurple=Color(147, 112, 219), # 9370db |
| 273 |
mediumseagreen=Color(60, 179, 113), # 3cb371 |
| 274 |
mediumslateblue=Color(123, 104, 238), # 7b68ee |
| 275 |
mediumspringgreen=Color(0, 250, 154), # 00fa9a |
| 276 |
mediumturquoise=Color(72, 209, 204), # 48d1cc |
| 277 |
mediumvioletred=Color(199, 21, 133), # c71585 |
| 278 |
midnightblue=Color(25, 25, 112), # 191970 |
| 279 |
mintcream=Color(245, 255, 250), # f5fffa |
| 280 |
mistyrose=Color(255, 228, 225), # ffe4e1 |
| 281 |
moccasin=Color(255, 228, 181), # ffe4b5 |
| 282 |
navajowhite=Color(255, 222, 173), # ffdead |
| 283 |
navy=Color(0, 0, 128), # 000080 |
| 284 |
oldlace=Color(253, 245, 230), # fdf5e6 |
| 285 |
olive=Color(128, 128, 0), # 808000 |
| 286 |
olivedrab=Color(107, 142, 35), # 6b8e23 |
| 287 |
orange=Color(255, 165, 0), # ffa500 |
| 288 |
orangered=Color(255, 69, 0), # ff4500 |
| 289 |
orchid=Color(218, 112, 214), # da70d6 |
| 290 |
palegoldenrod=Color(238, 232, 170), # eee8aa |
| 291 |
palegreen=Color(152, 251, 152), # 98fb98 |
| 292 |
paleturquoise=Color(175, 238, 238), # afeeee |
| 293 |
palevioletred=Color(219, 112, 147), # db7093 |
| 294 |
papayawhip=Color(255, 239, 213), # ffefd5 |
| 295 |
peachpuff=Color(255, 218, 185), # ffdab9 |
| 296 |
peru=Color(205, 133, 63), # cd853f |
| 297 |
pink=Color(255, 192, 203), # ffc0cb |
| 298 |
plum=Color(221, 160, 221), # dda0dd |
| 299 |
powderblue=Color(176, 224, 230), # b0e0e6 |
| 300 |
purple=Color(128, 0, 128), # 800080 |
| 301 |
red=Color(255, 0, 0), # ff0000 |
| 302 |
rosybrown=Color(188, 143, 143), # bc8f8f |
| 303 |
royalblue=Color(65, 105, 225), # 4169e1 |
| 304 |
saddlebrown=Color(139, 69, 19), # 8b4513 |
| 305 |
salmon=Color(250, 128, 114), # fa8072 |
| 306 |
sandybrown=Color(244, 164, 96), # f4a460 |
| 307 |
seagreen=Color(46, 139, 87), # 2e8b57 |
| 308 |
seashell=Color(255, 245, 238), # fff5ee |
| 309 |
sienna=Color(160, 82, 45), # a0522d |
| 310 |
silver=Color(192, 192, 192), # c0c0c0 |
| 311 |
skyblue=Color(135, 206, 235), # 87ceeb |
| 312 |
slateblue=Color(106, 90, 205), # 6a5acd |
| 313 |
slategray=Color(112, 128, 144), # 708090 |
| 314 |
slategrey=Color(112, 128, 144), # 708090 |
| 315 |
snow=Color(255, 250, 250), # fffafa |
| 316 |
springgreen=Color(0, 255, 127), # 00ff7f |
| 317 |
steelblue=Color(70, 130, 180), # 4682b4 |
| 318 |
tan=Color(210, 180, 140), # d2b48c |
| 319 |
teal=Color(0, 128, 128), # 008080 |
| 320 |
thistle=Color(216, 191, 216), # d8bfd8 |
| 321 |
tomato=Color(255, 99, 71), # ff6347 |
| 322 |
turquoise=Color(64, 224, 208), # 40e0d0 |
| 323 |
violet=Color(238, 130, 238), # ee82ee |
| 324 |
wheat=Color(245, 222, 179), # f5deb3 |
| 325 |
white=Color(255, 255, 255), # ffffff |
| 326 |
whitesmoke=Color(245, 245, 245), # f5f5f5 |
| 327 |
yellow=Color(255, 255, 0), # ffff00 |
| 328 |
yellowgreen=Color(154, 205, 50), # 9acd32 |
| 329 |
) |