codekingpro/portable-devtools
114k
1from math import sqrt2from functools import lru_cache3from typing import Sequence, Tuple, TYPE_CHECKING4 5from .color_triplet import ColorTriplet6 7if TYPE_CHECKING:8 from rich.table import Table9 10 11class Palette:12 """A palette of available colors."""13 14 def __init__(self, colors: Sequence[Tuple[int, int, int]]):15 self._colors = colors16 17 def __getitem__(self, number: int) -> ColorTriplet:18 return ColorTriplet(*self._colors[number])19 20 def __rich__(self) -> "Table":21 from rich.color import Color22 from rich.style import Style23 from rich.text import Text24 from rich.table import Table25 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 table42 43 # This is somewhat inefficient and needs caching44 @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.47 48 Args:49 color (Tuple[int, int, int]): RGB components in range 0 > 255.50 51 Returns:52 int: Index of closes matching color.53 """54 red1, green1, blue1 = color55 _sqrt = sqrt56 get_color = self._colors.__getitem__57 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) // 262 red = red1 - red263 green = green1 - green264 blue = blue1 - blue265 return _sqrt(66 (((512 + red_mean) * red * red) >> 8)67 + 4 * green * green68 + (((767 - red_mean) * blue * blue) >> 8)69 )70 71 min_index = min(range(len(self._colors)), key=get_color_distance)72 return min_index73 74 75if __name__ == "__main__": # pragma: no cover76 import colorsys77 from typing import Iterable78 from rich.color import Color79 from rich.console import Console, ConsoleOptions80 from rich.segment import Segment81 from rich.style import Style82 83 class ColorBox:84 def __rich_console__(85 self, console: Console, options: ConsoleOptions86 ) -> Iterable[Segment]:87 height = console.size.height - 388 for y in range(0, height):89 for x in range(options.max_width):90 h = x / options.max_width91 l = y / (height + 1)92 r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0)93 r2, g2, b2 = colorsys.hls_to_rgb(h, l + (1 / height / 2), 1.0)94 bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255)95 color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255)96 yield Segment("▄", Style(color=color, bgcolor=bgcolor))97 yield Segment.line()98 99 console = Console()100 console.print(ColorBox())101 