Skip to content

Commit 77d2ca9

Browse files
committed
Add support for CSS' named colors
1 parent 3e7cc74 commit 77d2ca9

File tree

4 files changed

+158
-4
lines changed

4 files changed

+158
-4
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,154 @@
261261
(228, 228, 228),
262262
(238, 238, 238),
263263
]
264+
265+
# Taken from the webcolors library
266+
CSS_COLORS = {
267+
"aliceblue": "#f0f8ff",
268+
"antiquewhite": "#faebd7",
269+
"aqua": "#00ffff",
270+
"aquamarine": "#7fffd4",
271+
"azure": "#f0ffff",
272+
"beige": "#f5f5dc",
273+
"bisque": "#ffe4c4",
274+
"black": "#000000",
275+
"blanchedalmond": "#ffebcd",
276+
"blue": "#0000ff",
277+
"blueviolet": "#8a2be2",
278+
"brown": "#a52a2a",
279+
"burlywood": "#deb887",
280+
"cadetblue": "#5f9ea0",
281+
"chartreuse": "#7fff00",
282+
"chocolate": "#d2691e",
283+
"coral": "#ff7f50",
284+
"cornflowerblue": "#6495ed",
285+
"cornsilk": "#fff8dc",
286+
"crimson": "#dc143c",
287+
"cyan": "#00ffff",
288+
"darkblue": "#00008b",
289+
"darkcyan": "#008b8b",
290+
"darkgoldenrod": "#b8860b",
291+
"darkgray": "#a9a9a9",
292+
"darkgrey": "#a9a9a9",
293+
"darkgreen": "#006400",
294+
"darkkhaki": "#bdb76b",
295+
"darkmagenta": "#8b008b",
296+
"darkolivegreen": "#556b2f",
297+
"darkorange": "#ff8c00",
298+
"darkorchid": "#9932cc",
299+
"darkred": "#8b0000",
300+
"darksalmon": "#e9967a",
301+
"darkseagreen": "#8fbc8f",
302+
"darkslateblue": "#483d8b",
303+
"darkslategray": "#2f4f4f",
304+
"darkslategrey": "#2f4f4f",
305+
"darkturquoise": "#00ced1",
306+
"darkviolet": "#9400d3",
307+
"deeppink": "#ff1493",
308+
"deepskyblue": "#00bfff",
309+
"dimgray": "#696969",
310+
"dimgrey": "#696969",
311+
"dodgerblue": "#1e90ff",
312+
"firebrick": "#b22222",
313+
"floralwhite": "#fffaf0",
314+
"forestgreen": "#228b22",
315+
"fuchsia": "#ff00ff",
316+
"gainsboro": "#dcdcdc",
317+
"ghostwhite": "#f8f8ff",
318+
"gold": "#ffd700",
319+
"goldenrod": "#daa520",
320+
"gray": "#808080",
321+
"grey": "#808080",
322+
"green": "#008000",
323+
"greenyellow": "#adff2f",
324+
"honeydew": "#f0fff0",
325+
"hotpink": "#ff69b4",
326+
"indianred": "#cd5c5c",
327+
"indigo": "#4b0082",
328+
"ivory": "#fffff0",
329+
"khaki": "#f0e68c",
330+
"lavender": "#e6e6fa",
331+
"lavenderblush": "#fff0f5",
332+
"lawngreen": "#7cfc00",
333+
"lemonchiffon": "#fffacd",
334+
"lightblue": "#add8e6",
335+
"lightcoral": "#f08080",
336+
"lightcyan": "#e0ffff",
337+
"lightgoldenrodyellow": "#fafad2",
338+
"lightgray": "#d3d3d3",
339+
"lightgrey": "#d3d3d3",
340+
"lightgreen": "#90ee90",
341+
"lightpink": "#ffb6c1",
342+
"lightsalmon": "#ffa07a",
343+
"lightseagreen": "#20b2aa",
344+
"lightskyblue": "#87cefa",
345+
"lightslategray": "#778899",
346+
"lightslategrey": "#778899",
347+
"lightsteelblue": "#b0c4de",
348+
"lightyellow": "#ffffe0",
349+
"lime": "#00ff00",
350+
"limegreen": "#32cd32",
351+
"linen": "#faf0e6",
352+
"magenta": "#ff00ff",
353+
"maroon": "#800000",
354+
"mediumaquamarine": "#66cdaa",
355+
"mediumblue": "#0000cd",
356+
"mediumorchid": "#ba55d3",
357+
"mediumpurple": "#9370db",
358+
"mediumseagreen": "#3cb371",
359+
"mediumslateblue": "#7b68ee",
360+
"mediumspringgreen": "#00fa9a",
361+
"mediumturquoise": "#48d1cc",
362+
"mediumvioletred": "#c71585",
363+
"midnightblue": "#191970",
364+
"mintcream": "#f5fffa",
365+
"mistyrose": "#ffe4e1",
366+
"moccasin": "#ffe4b5",
367+
"navajowhite": "#ffdead",
368+
"navy": "#000080",
369+
"oldlace": "#fdf5e6",
370+
"olive": "#808000",
371+
"olivedrab": "#6b8e23",
372+
"orange": "#ffa500",
373+
"orangered": "#ff4500",
374+
"orchid": "#da70d6",
375+
"palegoldenrod": "#eee8aa",
376+
"palegreen": "#98fb98",
377+
"paleturquoise": "#afeeee",
378+
"palevioletred": "#db7093",
379+
"papayawhip": "#ffefd5",
380+
"peachpuff": "#ffdab9",
381+
"peru": "#cd853f",
382+
"pink": "#ffc0cb",
383+
"plum": "#dda0dd",
384+
"powderblue": "#b0e0e6",
385+
"purple": "#800080",
386+
"red": "#ff0000",
387+
"rosybrown": "#bc8f8f",
388+
"royalblue": "#4169e1",
389+
"saddlebrown": "#8b4513",
390+
"salmon": "#fa8072",
391+
"sandybrown": "#f4a460",
392+
"seagreen": "#2e8b57",
393+
"seashell": "#fff5ee",
394+
"sienna": "#a0522d",
395+
"silver": "#c0c0c0",
396+
"skyblue": "#87ceeb",
397+
"slateblue": "#6a5acd",
398+
"slategray": "#708090",
399+
"slategrey": "#708090",
400+
"snow": "#fffafa",
401+
"springgreen": "#00ff7f",
402+
"steelblue": "#4682b4",
403+
"tan": "#d2b48c",
404+
"teal": "#008080",
405+
"thistle": "#d8bfd8",
406+
"tomato": "#ff6347",
407+
"turquoise": "#40e0d0",
408+
"violet": "#ee82ee",
409+
"wheat": "#f5deb3",
410+
"white": "#ffffff",
411+
"whitesmoke": "#f5f5f5",
412+
"yellow": "#ffff00",
413+
"yellowgreen": "#9acd32",
414+
}

pytermgui/colors.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import TYPE_CHECKING, Generator, Literal, Type
2020

2121
from .ansi_interface import reset as reset_style
22-
from .color_table import COLOR_TABLE
22+
from .color_info import COLOR_TABLE, CSS_COLORS
2323
from .exceptions import ColorSyntaxError
2424
from .fancy_repr import FancyYield
2525
from .input import getch
@@ -74,7 +74,10 @@
7474
16: "bright-white",
7575
}
7676

77-
NAMED_COLORS = {**{color: str(index) for index, color in XTERM_NAMED_COLORS.items()}}
77+
NAMED_COLORS = {
78+
**CSS_COLORS,
79+
**{color: str(index) for index, color in XTERM_NAMED_COLORS.items()},
80+
}
7881

7982
_COLOR_CACHE: dict[str, Color] = {}
8083
_COLOR_MATCH_CACHE: dict[tuple[float, float, float], Color] = {}

pytermgui/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def _apply_colors(colors: list[str] | list[int], item: str) -> str:
242242
def macro_rainbow(item: str) -> str:
243243
"""Creates rainbow-colored text."""
244244

245-
colors = ["red", "208", "yellow", "green", "brightblue", "blue", "93"]
245+
colors = ["red", "208", "yellow", "green", "bright-blue", "blue", "93"]
246246

247247
return _apply_colors(colors, item)
248248

pytermgui/regex.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RE_PIXEL_SIZE = re.compile(r"\x1b\[4;([\d]+);([\d]+)t")
1212

1313
RE_256 = re.compile(r"^([\d]{1,3})$")
14-
RE_HEX = re.compile(r"(?:#)?([0-9a-fA-F]{6})")
14+
RE_HEX = re.compile(r"#?([0-9a-fA-F]{6})")
1515
RE_RGB = re.compile(r"(\d{1,3};\d{1,3};\d{1,3})")
1616

1717
__all__ = [

0 commit comments

Comments
 (0)