6 "indianred": "#cd5c5c",
7 "lightcoral": "#f08080",
9 "darksalmon": "#e9967a",
10 "lightsalmon": "#ffa07a",
13 "firebrick": "#b22222",
16 "lightpink": "#ffb6c1",
18 "deeppink": "#ff1493",
19 "mediumvioletred": "#c71585",
20 "palevioletred": "#db7093",
21 "lightsalmon": "#ffa07a",
24 "orangered": "#ff4500",
25 "darkorange": "#ff8c00",
29 "lightyellow": "#ffffe0",
30 "lemonchiffon": "#fffacd",
31 "lightgoldenrodyellow": "#fafad2",
32 "papayawhip": "#ffefd5",
33 "moccasin": "#ffe4b5",
34 "peachpuff": "#ffdab9",
35 "palegoldenrod": "#eee8aa",
37 "darkkhaki": "#bdb76b",
38 "lavender": "#e6e6fa",
45 "mediumorchid": "#ba55d3",
46 "mediumpurple": "#9370db",
47 "blueviolet": "#8a2be2",
48 "darkviolet": "#9400d3",
49 "darkorchid": "#9932cc",
50 "darkmagenta": "#8b008b",
53 "darkslateblue": "#483d8b",
54 "slateblue": "#6a5acd",
55 "mediumslateblue": "#7b68ee",
56 "greenyellow": "#adff2f",
57 "chartreuse": "#7fff00",
58 "lawngreen": "#7cfc00",
60 "limegreen": "#32cd32",
61 "palegreen": "#98fb98",
62 "lightgreen": "#90ee90",
63 "mediumspringgreen": "#00fa9a",
64 "springgreen": "#00ff7f",
65 "mediumseagreen": "#3cb371",
66 "seagreen": "#2e8b57",
67 "forestgreen": "#228b22",
69 "darkgreen": "#006400",
70 "yellowgreen": "#9acd32",
71 "olivedrab": "#6b8e23",
73 "darkolivegreen": "#556b2f",
74 "mediumaquamarine": "#66cdaa",
75 "darkseagreen": "#8fbc8f",
76 "lightseagreen": "#20b2aa",
77 "darkcyan": "#008b8b",
81 "lightcyan": "#e0ffff",
82 "paleturquoise": "#afeeee",
83 "aquamarine": "#7fffd4",
84 "turquoise": "#40e0d0",
85 "mediumturquoise": "#48d1cc",
86 "darkturquoise": "#00ced1",
87 "cadetblue": "#5f9ea0",
88 "steelblue": "#4682b4",
89 "lightsteelblue": "#b0c4de",
90 "powderblue": "#b0e0e6",
91 "lightblue": "#add8e6",
93 "lightskyblue": "#87cefa",
94 "deepskyblue": "#00bfff",
95 "dodgerblue": "#1e90ff",
96 "cornflowerblue": "#6495ed",
97 "royalblue": "#4169e1",
99 "mediumblue": "#0000cd",
100 "darkblue": "#00008b",
102 "midnightblue": "#191970",
103 "cornsilk": "#fff8dc",
104 "blanchedalmond": "#ffebcd",
106 "navajowhite": "#ffdead",
108 "burlywood": "#deb887",
110 "rosybrown": "#bc8f8f",
111 "sandybrown": "#f4a460",
112 "goldenrod": "#daa520",
113 "darkgoldenrod": "#b8860b",
115 "chocolate": "#d2691e",
116 "saddlebrown": "#8b4513",
122 "honeydew": "#f0fff0",
123 "mintcream": "#f5fffa",
125 "aliceblue": "#f0f8ff",
126 "ghostwhite": "#f8f8ff",
127 "whitesmoke": "#f5f5f5",
128 "seashell": "#fff5ee",
130 "oldlace": "#fdf5e6",
131 "floralwhite": "#fffaf0",
133 "antiquewhite": "#faebd7",
135 "lavenderblush": "#fff0f5",
136 "mistyrose": "#ffe4e1",
137 "gainsboro": "#dcdcdc",
138 "lightgrey": "#d3d3d3",
140 "darkgray": "#a9a9a9",
142 "dimgray": "#696969",
143 "lightslategray": "#778899",
144 "slategray": "#708090",
145 "darkslategray": "#2f4f4f",
151 "#000000", #{ 0, 0, 0 },//0 - black
152 "#000080", #{ 0, 0, 128 },//1 - navy
153 "#008000", #{ 0, 128, 0 },//2 - green
154 "#008080", #{ 0, 128, 128 },//3 - teal
155 "#800000", #{ 128, 0, 0 },//4 - maroon
156 "#800080", #{ 128, 0, 128 },//5 - purple
157 "#808000", #{ 128, 128, 0 },//6 - olive
158 "#C0C0C0", #{ 192, 192, 192 },//7 - silver
159 "#808080", #{ 128, 128, 128 },//8 - gray
160 "#0000FF", #{ 0, 0, 255 },//9 - blue
161 "#00FF00", #{ 0, 255, 0 },//a - lime
162 "#00FFFF", #{ 0, 255, 255 },//b - cyan
163 "#FF0000", #{ 255, 0, 0 },//c - red
164 "#FF00FF", #{ 255, 0, 255 },//d - magenta
165 "#FFFF00", #{ 255, 255, 0 },//e - yellow
166 "#FFFFFF", #{ 255, 255, 255 } //f - white
196 X = (0.412453 * r + 0.357580 * g + 0.180423 * b) / 0.950456
197 Y = (0.212671 * r + 0.715160 * g + 0.072169 * b)
198 Z = (0.019334 * r + 0.119193 * g + 0.950227 * b) / 1.088754
200 #[X * 0.950456] [0.412453 0.357580 0.180423] [R]
201 #[Y ] = [0.212671 0.715160 0.072169] * [G]
202 #[Z * 1.088754] [0.019334 0.119193 0.950227] [B]
204 T = 0.008856 #threshold
207 fX = math.pow(X, 1./3.)
209 fX = 7.787 * X + 16./116.
213 Y3 = math.pow(Y, 1./3.)
217 fY = 7.787 * Y + 16./116.
221 fZ = math.pow(Z, 1./3.)
223 fZ = 7.787 * Z + 16./116.
231 def colorDistance(r1,g1,b1 = None, r2 = None, g2 = None,b2 = None):
232 if type(r1) == tuple and type(g1) == tuple and b1 is None and r2 is None and g2 is None and b2 is None:
233 (l1,a1,b1) = RGB2LAB(*r1)
234 (l2,a2,b2) = RGB2LAB(*g1)
236 (l1,a1,b1) = RGB2LAB(r1,g1,b1)
237 (l2,a2,b2) = RGB2LAB(r2,g2,b2)
240 C1 = math.sqrt(a1*a1 + b1*b1)
241 C2 = math.sqrt(a2*a2 + b2*b2)
245 dH = math.sqrt(max(0, da*da + db*db - dC*dC))
251 s2 = dC/(1. + K1 * C1)
252 s3 = dH/(1. + K2 * C1)
253 return math.sqrt(s1*s1 + s2*s2 + s3*s3)
255 def parseHexColor(col):
256 if len(col) != 4 and len(col) != 7 and not col.startswith("#"):
266 return (int(r,16), int(g,16), int(b,16))
269 if isinstance(col, str):
270 if col.lower() in webcolors:
271 return parseHexColor(webcolors[col.lower()])
273 return parseHexColor(col)
277 def getNearestConsoleColor(col):
278 color = getColor(col)
280 mindist = colorDistance(color, getColor(consoleColors[0]))
281 for i in range(len(consoleColors)):
282 dist = colorDistance(color, getColor(consoleColors[i]))
290 from ctypes import windll, Structure, c_short, c_ushort, byref
294 class COORD(Structure):
299 class SMALL_RECT(Structure):
306 class CONSOLE_SCREEN_BUFFER_INFO(Structure):
309 ("dwCursorPosition", COORD),
310 ("wAttributes", WORD),
311 ("srWindow", SMALL_RECT),
312 ("dwMaximumWindowSize", COORD)]
314 class winConsoleColorizer(object):
315 def __init__(self, stream):
316 self.handle = msvcrt.get_osfhandle(stream.fileno())
317 self.default_attrs = 7#self.get_text_attr()
320 def get_text_attr(self):
321 csbi = CONSOLE_SCREEN_BUFFER_INFO()
322 windll.kernel32.GetConsoleScreenBufferInfo(self.handle, byref(csbi))
323 return csbi.wAttributes
325 def set_text_attr(self, color):
326 windll.kernel32.SetConsoleTextAttribute(self.handle, color)
328 def write(self, *text, **attrs):
331 color = attrs.get("color", None)
333 col = getNearestConsoleColor(color)
335 self.set_text_attr(col)
336 self.stream.write(" ".join([str(t) for t in text]))
339 self.set_text_attr(self.default_attrs)
341 class dummyColorizer(object):
342 def __init__(self, stream):
345 def write(self, *text, **attrs):
347 self.stream.write(" ".join([str(t) for t in text]))
349 class asciiSeqColorizer(object):
350 RESET_SEQ = "\033[0m"
351 #BOLD_SEQ = "\033[1m"
352 ITALIC_SEQ = "\033[3m"
353 UNDERLINE_SEQ = "\033[4m"
354 STRIKEOUT_SEQ = "\033[9m"
355 COLOR_SEQ0 = "\033[00;%dm" #dark
356 COLOR_SEQ1 = "\033[01;%dm" #bold and light
358 def __init__(self, stream):
361 def get_seq(self, code):
363 return self.__class__.COLOR_SEQ1 % (30 + code - 9)
365 return self.__class__.COLOR_SEQ0 % (30 + code)
367 def write(self, *text, **attrs):
370 color = attrs.get("color", None)
372 col = getNearestConsoleColor(color)
373 self.stream.write(self.get_seq(col))
374 self.stream.write(" ".join([str(t) for t in text]))
376 self.stream.write(self.__class__.RESET_SEQ)
379 def getColorizer(stream):
382 return winConsoleColorizer(stream)
384 return asciiSeqColorizer(stream)
386 return dummyColorizer(stream)