-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
90 lines (78 loc) · 2.73 KB
/
run.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
87
88
89
90
import statistics
import colorsys
def hex_to_rgb(hex_color):
hex_color = hex_color.lstrip('#')
return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
def get_grayscale_level(hex_color):
r, g, b = hex_to_rgb(hex_color)
grayscale_level = 1 - statistics.stdev([r, g, b]) / 127.5
return grayscale_level
def get_brightness_description(hex_color):
if get_grayscale_level(hex_color) > 0.5:
return "muted"
else:
return "bright"
def get_color_description(hex_color):
r, g, b = hex_to_rgb(hex_color)
r, g, b = [x/255.0 for x in (r, g, b)]
h, l, s = colorsys.rgb_to_hls(r, g, b)
descriptions = []
if l < 0.33:
descriptions.append("dark")
elif l < 0.66:
descriptions.append("medium")
else:
descriptions.append("light")
if get_brightness_description(hex_color) == "muted":
descriptions.append("muted")
else:
descriptions.append("bright")
h *= 360 # convert hue to degrees for easier interpretation
# Color-specific undertone rules
if 0 <= h < 30: # reds
color_name = "red"
if b > 0.25 * (r + b):
descriptions.append("with cool undertones")
else:
descriptions.append("with warm undertones")
elif 30 <= h < 90: # yellows and oranges
color_name = "yellow"
if b > 0:
descriptions.append("with cool undertones")
else:
descriptions.append("with warm undertones")
elif 90 <= h < 150: # greens
color_name = "green"
if r > 0.33 * (g + b):
descriptions.append("with warm undertones")
else:
descriptions.append("with cool undertones")
elif 150 <= h < 210: # cyans
color_name = "cyan"
if r > 0.2 * (g + b):
descriptions.append("with warm undertones")
else:
descriptions.append("with cool undertones")
elif 210 <= h < 270: # blues
color_name = "blue"
if r > 0.2 * (g + b):
descriptions.append("with warm undertones")
else:
descriptions.append("with cool undertones")
elif 270 <= h < 330: # purples
color_name = "purple"
if r < b:
descriptions.append("with cool undertones")
else:
descriptions.append("with warm undertones")
else: # pinks and reds
color_name = "pink"
if b > 0.33 * (r + b):
descriptions.append("with cool undertones")
else:
descriptions.append("with warm undertones")
# add the color name to the description
descriptions.insert(2, color_name)
return " ".join(descriptions)
hex_color = input("Enter a hex color: ")
print("Color Description:", get_color_description(hex_color))