Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/rich/palette.py: 35%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from functools import lru_cache
2from math import sqrt
3from typing import TYPE_CHECKING, Sequence
5from .color_triplet import ColorTriplet
7if TYPE_CHECKING:
8 from rich.table import Table
11class Palette:
12 """A palette of available colors."""
14 def __init__(self, colors: Sequence[tuple[int, int, int]]):
15 self._colors = colors
17 def __getitem__(self, number: int) -> ColorTriplet:
18 return ColorTriplet(*self._colors[number])
20 def __rich__(self) -> "Table":
21 from rich.color import Color
22 from rich.style import Style
23 from rich.table import Table
24 from rich.text import Text
26 table = Table(
27 "index",
28 "RGB",
29 "Color",
30 title="Palette",
31 caption=f"{len(self._colors)} colors",
32 highlight=True,
33 caption_justify="right",
34 )
35 for index, color in enumerate(self._colors):
36 table.add_row(
37 str(index),
38 repr(color),
39 Text(" " * 16, style=Style(bgcolor=Color.from_rgb(*color))),
40 )
41 return table
43 # This is somewhat inefficient and needs caching
44 @lru_cache(maxsize=1024)
45 def match(self, color: tuple[int, int, int]) -> int:
46 """Find a color from a palette that most closely matches a given color.
48 Args:
49 color (tuple[int, int, int]): RGB components in range 0 > 255.
51 Returns:
52 int: Index of closes matching color.
53 """
54 red1, green1, blue1 = color
55 _sqrt = sqrt
56 get_color = self._colors.__getitem__
58 def get_color_distance(index: int) -> float:
59 """Get the distance to a color."""
60 red2, green2, blue2 = get_color(index)
61 red_mean = (red1 + red2) // 2
62 red = red1 - red2
63 green = green1 - green2
64 blue = blue1 - blue2
65 return _sqrt(
66 (((512 + red_mean) * red * red) >> 8)
67 + 4 * green * green
68 + (((767 - red_mean) * blue * blue) >> 8)
69 )
71 min_index = min(range(len(self._colors)), key=get_color_distance)
72 return min_index
75if __name__ == "__main__": # pragma: no cover
76 import colorsys
77 from typing import Iterable
79 from rich.color import Color
80 from rich.console import Console, ConsoleOptions
81 from rich.segment import Segment
82 from rich.style import Style
84 class ColorBox:
85 def __rich_console__(
86 self, console: Console, options: ConsoleOptions
87 ) -> Iterable[Segment]:
88 height = console.size.height - 3
89 for y in range(0, height):
90 for x in range(options.max_width):
91 h = x / options.max_width
92 l = y / (height + 1)
93 r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)
94 r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2), 1.0)
95 bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)
96 color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)
97 yield Segment("▄", Style(color=color, bgcolor=bgcolor))
98 yield Segment.line()
100 console = Console()
101 console.print(ColorBox())