File size: 784 Bytes
ec7ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import colorsys
from typing import NamedTuple

import numpy as np


class HSVUpdate(NamedTuple):
    h: int = 0
    s: int = 0
    v: int = 0

    def apply(self, color: np.ndarray):
        hsv = colorsys.rgb_to_hsv(*color / 255)
        tmp = np.add(hsv, self).clip(0, 1)
        rgb = colorsys.hsv_to_rgb(*tmp)

        return np.array(rgb) * 255


def get_hsv_value(cluster: np.ndarray):
    return colorsys.rgb_to_hsv(*cluster / 255)[2]


def add_hsv_saturation(cluster: np.ndarray, delta: float):
    h, s, v = colorsys.rgb_to_hsv(*cluster / 255)
    s = max(0, min(1, s + delta))

    return np.array(colorsys.hsv_to_rgb(h, s, v)) * 255


def array_to_hex(values: np.ndarray):
    values = np.round(values).astype(int)
    return "#" + ("{:02X}" * len(values)).format(*values)