3from colorsys
import rgb_to_hls
4from enum
import IntEnum
5from functools
import lru_cache
6from typing
import TYPE_CHECKING, NamedTuple, Optional, Tuple
8from ._palettes
import EIGHT_BIT_PALETTE, STANDARD_PALETTE, WINDOWS_PALETTE
9from .color_triplet
import ColorTriplet
10from .repr
import Result, rich_repr
11from .terminal_theme
import DEFAULT_TERMINAL_THEME
14 from .terminal_theme
import TerminalTheme
15 from .text
import Text
22 """One of the 3 color system supported by terminals."""
30 return f
"ColorSystem.{self.name}"
37 """Type of color stored in Color class."""
46 return f
"ColorType.{self.name}"
84 "light_sea_green": 37,
93 "medium_spring_green": 49,
104 "medium_purple4": 60,
108 "dark_sea_green4": 71,
109 "pale_turquoise4": 66,
112 "cornflower_blue": 69,
120 "medium_turquoise": 80,
125 "dark_slate_gray2": 87,
131 "medium_purple3": 98,
137 "light_slate_grey": 103,
138 "light_slate_gray": 103,
139 "medium_purple": 104,
140 "light_slate_blue": 105,
141 "dark_olive_green3": 149,
142 "dark_sea_green": 108,
143 "light_sky_blue3": 110,
145 "dark_sea_green3": 150,
146 "dark_slate_gray3": 116,
151 "dark_slate_gray1": 123,
153 "medium_violet_red": 126,
158 "medium_orchid3": 133,
159 "medium_orchid": 134,
160 "medium_purple2": 140,
161 "dark_goldenrod": 136,
162 "light_salmon3": 173,
166 "medium_purple1": 141,
169 "navajo_white3": 144,
172 "light_steel_blue3": 146,
173 "light_steel_blue": 147,
175 "dark_sea_green2": 157,
177 "light_sky_blue1": 153,
179 "dark_olive_green2": 155,
180 "dark_sea_green1": 193,
181 "pale_turquoise1": 159,
186 "medium_orchid1": 207,
192 "light_goldenrod3": 179,
198 "light_goldenrod2": 222,
199 "light_yellow3": 187,
202 "light_steel_blue1": 189,
204 "dark_olive_green1": 192,
217 "pale_violet_red1": 211,
222 "light_salmon1": 216,
227 "navajo_white1": 223,
231 "light_goldenrod1": 227,
289 """The color could not be parsed."""
295color\(([0-9]{1,3})\)$|
304 """Terminal color definition."""
307 """The name of the color (typically the input to Color.parse)."""
309 """The type of the color."""
310 number: Optional[int] =
None
311 """The color number, if a standard color, or None."""
312 triplet: Optional[ColorTriplet] =
None
313 """A triplet of color components, if an RGB color."""
316 """Displays the actual color if Rich printed."""
317 from .style
import Style
318 from .text
import Text
321 f
"<color {self.name!r} ({self.type.name.lower()})",
322 (
"⬤", Style(color=self)),
334 """Get the native color system for this color."""
341 """Check if the color is ultimately defined by the system."""
346 """Check if the color is a default color."""
350 self, theme: Optional[
"TerminalTheme"] =
None, foreground: bool =
True
352 """Get an equivalent color triplet for this color.
355 theme (TerminalTheme, optional): Optional terminal theme, or None to use default. Defaults to None.
356 foreground (bool, optional): True for a foreground color, or False for background. Defaults to True.
359 ColorTriplet: A color triplet containing RGB components.
363 theme = DEFAULT_TERMINAL_THEME
382 """Create a Color number from it's 8-bit ansi number.
385 number (int): A number between 0-255 inclusive.
388 Color: A new Color instance.
391 name=f
"color({number})",
398 """Create a truecolor RGB color from a triplet of values.
401 triplet (ColorTriplet): A color triplet containing red, green and blue components.
404 Color: A new color object.
409 def from_rgb(cls, red: float, green: float, blue: float) ->
"Color":
410 """Create a truecolor from three color components in the range(0->255).
413 red (float): Red component in range 0-255.
414 green (float): Green component in range 0-255.
415 blue (float): Blue component in range 0-255.
418 Color: A new color object.
424 """Get a Color instance representing the default color.
427 Color: Default color.
432 @lru_cache(maxsize=1024)
433 def parse(cls, color: str) ->
"Color":
434 """Parse a color definition."""
435 original_color = color
438 if color ==
"default":
442 if color_number
is not None:
450 if color_match
is None:
456 int(color_24[0:2], 16), int(color_24[2:4], 16), int(color_24[4:6], 16)
461 number = int(color_8)
472 if len(components) != 3:
474 f
"expected three components in {original_color!r}"
476 red, green, blue = components
478 if not all(component <= 255
for component
in triplet):
480 f
"color components must be <= 255 in {original_color!r}"
484 @lru_cache(maxsize=1024)
486 """Get the ANSI escape codes for this color."""
489 return (
"39" if foreground
else "49",)
493 assert number
is not None
494 fore, back = (30, 40)
if number < 8
else (82, 92)
495 return (str(fore + number
if foreground
else back + number),)
499 assert number
is not None
500 fore, back = (30, 40)
if number < 8
else (82, 92)
501 return (str(fore + number
if foreground
else back + number),)
505 return (
"38" if foreground
else "48",
"5", str(self.
numbernumber))
510 return (
"38" if foreground
else "48",
"2", str(red), str(green), str(blue))
512 @lru_cache(maxsize=1024)
514 """Downgrade a color system to a system with fewer colors."""
524 gray =
round(l * 25.0)
530 color_number = 231 + gray
534 six_red = red / 95
if red < 95
else 1 + (red - 95) / 40
535 six_green = green / 95
if green < 95
else 1 + (green - 95) / 40
536 six_blue = blue / 95
if blue < 95
else 1 + (blue - 95) / 40
572 """Parse six hex characters in to RGB triplet."""
573 assert len(hex_color) == 6,
"must be 6 characters"
575 int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
581 color1: ColorTriplet, color2: ColorTriplet, cross_fade: float = 0.5
583 """Blend one RGB color in to another."""
587 int(r1 + (r2 - r1) * cross_fade),
588 int(g1 + (g2 - g1) * cross_fade),
589 int(b1 + (b2 - b1) * cross_fade),
594if __name__ ==
"__main__":
596 from .console
import Console
597 from .table
import Table
598 from .text
import Text
602 table =
Table(show_footer=
False, show_edge=
True)
610 for color_number, name
in colors:
613 color_cell =
Text(
" " * 10, style=f
"on {name}")
614 if color_number < 16:
617 color = EIGHT_BIT_PALETTE[color_number]
"Color" from_rgb(cls, float red, float green, float blue)
bool is_system_defined(self)
Tuple[str,...] get_ansi_codes(self, bool foreground=True)
"Color" from_ansi(cls, int number)
"Color" from_triplet(cls, "ColorTriplet" triplet)
ColorTriplet get_truecolor(self, Optional["TerminalTheme"] theme=None, bool foreground=True)
"Color" downgrade(self, ColorSystem system)
Result __rich_repr__(self)
ColorTriplet parse_rgb_hex(str hex_color)