~exitb/retrofit

retrofit/palette.py -rw-r--r-- 994 bytes
5b189c4cexitb Fix reported version 1 year, 10 months ago
                                                                                
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
46
47
from math import sqrt

TARGET_PALETTE = [
    (0, 0, 0),
    (170, 0, 0),
    (0, 170, 0),
    (170, 85, 0),
    (0, 0, 170),
    (170, 0, 170),
    (0, 170, 170),
    (170, 170, 170),
    (85, 85, 85),
    (255, 85, 85),
    (85, 255, 85),
    (255, 255, 85),
    (85, 85, 255),
    (255, 85, 255),
    (85,  255, 255),
    (255, 255, 255),
]

def cube_palette():
    for red in range(6):
        for green in range(6):
            for blue in range(6):
                yield (red * 51, green * 51, blue * 51)



CUBE_PALETTE = list(cube_palette())

def difference(first, second):
    red = (first[0] - second[0]) ** 2
    green = (first[1] - second[1]) ** 2
    blue = (first[2] - second[2]) ** 2

    return sqrt(red + green + blue)

def closest(index):
    def keyfunc(color):
        return difference(color[1], TARGET_PALETTE[index])

    return min(enumerate(CUBE_PALETTE), key=keyfunc)[0] + 16

if __name__ == "__main__":
    for color in range(16):
        print(color, closest(color))