INNER CODE UNIT · Python
_rgb_dist
KennethJAllen/proper-pixel-art · proper_pixel_art/colors.py:29
def _rgb_dist(a: RGB, b: RGB) -> int:
"""Naive color distance"""
dr, dg, db = a[0] - b[0], a[1] - b[1], a[2] - b[2]
return dr**2 + dg**2 + db**2
def top_opaque_colors(
img: Image.Image,
alpha_threshold: int,
limit: int = _DEFAULTS.top_colors_limit,
thumbnail_size: tuple[int, int] = _DEFAULTS.thumbnail_size,
) -> list[RGB]:
"""Return the most common opaque colors (RGB) up to limit."""
rgba = img.convert("RGBA").copy()
rgba.thumbnail(thumbnail_size) # speed and de-noise tiny details
counts = Counter()
for r, g, b, a in rgba.get_flattened_data():
if a >= alpha_threshold: