forked from Textualize/rich
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.py
45 lines (35 loc) · 1.39 KB
/
palette.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from math import sqrt
from functools import lru_cache
from typing import Sequence, Tuple
from .color_triplet import ColorTriplet
class Palette:
"""A palette of available colors."""
def __init__(self, colors: Sequence[Tuple[int, int, int]]):
self._colors = colors
def __getitem__(self, number: int) -> ColorTriplet:
return ColorTriplet(*self._colors[number])
# This is somewhat inefficient and needs caching
@lru_cache(maxsize=1024)
def match(self, color: Tuple[int, int, int]) -> int:
"""Find a color from a palette that most closely matches a given color.
Args:
color (Tuple[int, int, int]): RGB components in range 0 > 255.
Returns:
int: Index of closes matching color.
"""
red1, green1, blue1 = color
_sqrt = sqrt
def get_color_distance(index: int) -> float:
"""Get the distance to a color."""
red2, green2, blue2 = self._colors[index]
red_mean = int((red1 + red2) / 2)
red = red1 - red2
green = green1 - green2
blue = blue1 - blue2
return _sqrt(
(((512 + red_mean) * red * red) >> 8)
+ 4 * green * green
+ (((767 - red_mean) * blue * blue) >> 8)
)
min_index = min(range(len(self._colors)), key=get_color_distance)
return min_index