-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_wheel.py
86 lines (66 loc) · 2.68 KB
/
color_wheel.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import logging
logger = logging.getLogger()
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
from colormath.color_objects import HSVColor, sRGBColor, LabColor
import numpy as np
from itertools import product, izip
from config import (BLACK_MAX_BRIGHTNESS, WHITE_MIN_BRIGHTNESS, GRAY_MAX_SATURATION, WHITE_MAX_SATURATION, HUE_TABLE,
BRIGHTNESS_IMPORTANCE_LEVEL, SATURATION_TABLE, BRIGHTNESS_TABLE)
from utils import find_nearest_idx
__author__ = 'vvvkamper'
color_wheel = product(enumerate(HUE_TABLE.values()), enumerate(izip(SATURATION_TABLE, BRIGHTNESS_TABLE)))
color_wheel = [(HSVColor(h, s / 100.0, v / 100.0), ih, iy) for (ih, h), (iy, (s, v)) in color_wheel]
extended_colors = [(HSVColor(212, 0.11, 0.91), 13, 0)]
colors = color_wheel + extended_colors
algorithms = {
'delta_e_cie2000': delta_e_cie2000
}
def find_color_code_by_color_distance(r, g, b, algorithm='delta_e_cie2000'):
"""
:param r:
:param g:
:param b:
:param algorithm:
:return: tuple(sRGBColor, color_code)
"""
color = sRGBColor(r, g, b)
lab_color = convert_color(color, LabColor)
distances = [algorithms[algorithm](lab_color, convert_color(x[0], LabColor)) for x in color_wheel]
np_distances = np.array(distances)
idx = np.argmin(np_distances)
ans_color = color_wheel[idx][0]
return convert_color(ans_color, sRGBColor).get_value_tuple(), (color_wheel[idx][1], color_wheel[idx][2])
def check_gray(s, b):
if b < BLACK_MAX_BRIGHTNESS:
return False
elif b > WHITE_MIN_BRIGHTNESS:
return False
elif s > GRAY_MAX_SATURATION:
return False
elif (GRAY_MAX_SATURATION * s + (WHITE_MIN_BRIGHTNESS - BLACK_MAX_BRIGHTNESS) * b > (
WHITE_MIN_BRIGHTNESS - BLACK_MAX_BRIGHTNESS) * GRAY_MAX_SATURATION):
return False
else:
return True
def check_black(s, b):
return b <= BLACK_MAX_BRIGHTNESS
def check_white(s, b):
return b >= WHITE_MIN_BRIGHTNESS and s <= WHITE_MAX_SATURATION
def find_color_code_naive(h, s, b):
if check_gray(s, b):
return 'gray'
if check_black(s, b):
return 'black'
if check_white(s, b):
return 'white'
first = find_nearest_idx(HUE_TABLE.values(), h)
if b > BRIGHTNESS_IMPORTANCE_LEVEL:
logger.info('Brightness is important')
second = find_nearest_idx(SATURATION_TABLE, s)
else:
logger.info('Saturation is important')
second = find_nearest_idx(BRIGHTNESS_TABLE, b)
color = HSVColor(HUE_TABLE[first], SATURATION_TABLE[second]/100.0, BRIGHTNESS_TABLE[second]/100.0)
color = convert_color(color, sRGBColor)
return color.get_value_tuple(), (first, second)