-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpalette.py
187 lines (142 loc) · 5.23 KB
/
palette.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env python3
"""Palette and Colors"""
import colorsys
import random
import seaborn as sns
def hex2hls(hex_color):
""" "Convert"""
hex_color = hex_color.lstrip("#")
rgb_color = tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
normalized_rgb = (
rgb_color[0] / 255.0,
rgb_color[1] / 255.0,
rgb_color[2] / 255.0,
)
hls_color = colorsys.rgb_to_hls(
normalized_rgb[0], normalized_rgb[1], normalized_rgb[2]
)
return hls_color
def hls2hex(hls_color):
""" "Convert"""
rgb_color = colorsys.hls_to_rgb(hls_color[0], hls_color[1], hls_color[2])
scaled_rgb = tuple(int(c * 255) for c in rgb_color)
return rgb2hex(scaled_rgb)
def rgb2hex(rgb_color):
""" "Convert"""
scaled_rgb = rgb_color
if isinstance(rgb_color[0], float):
scaled_rgb = tuple(int(c * 255) for c in rgb_color)
hex_color = f"#{scaled_rgb[0]:02X}{scaled_rgb[1]:02X}{scaled_rgb[2]:02X}"
return hex_color
def get_triadic_colors(hex_color):
""" "Convert"""
hls_color = hex2hls(hex_color)
triadic_colors = []
for offset in [120.0, 240.0]:
triadic_colors.append(
((hls_color[0] + offset / 360) % 1.0, hls_color[1], hls_color[2])
)
return [hls2hex(hls_color) for hls_color in triadic_colors]
def generate_random_colors(
n_colors=1, hue=None, saturation=None, lightness=None
):
"""Generate random colors. By default, dark colors."""
dark_colors = []
for _i in range(n_colors):
# Vary the hue from 0 to 1
# Set the saturation to a constant value (0.5 for moderate saturation)
# Vary the lightness from 0.2 to 0.5 for dark colors
hue = hue or random.random()
saturation = saturation or 0.5
lightness = lightness or (0.2 + hue * 0.3)
hex_color = hls2hex((hue, lightness, saturation))
dark_colors.append(hex_color)
return dark_colors
def generate_random_dark_red(n_colors=1):
"""Generate random dark red colors"""
hue = random.randint(300, 360) / 360
saturation = random.randint(75, 100) / 100
lightness = random.randint(15, 40) / 100
return generate_random_colors(n_colors, hue, saturation, lightness)
def generate_random_dark_orange(n_colors=1):
"""Generate random dark orange colors"""
hue = random.randint(20, 60) / 360
saturation = random.randint(60, 100) / 100
lightness = random.randint(20, 40) / 100
return generate_random_colors(n_colors, hue, saturation, lightness)
def generate_random_dark_purple(n_colors=1):
"""Generate random dark orange colors"""
hue = random.randint(270, 330) / 360
saturation = random.randint(60, 100) / 100
lightness = random.randint(20, 40) / 100
return generate_random_colors(n_colors, hue, saturation, lightness)
def generate_random_dark_black(n_colors=1):
"""Generate random dark black/gray/blue colors"""
hue = random.randint(0, 360) / 360
saturation = 1
lightness = random.randint(0, 10) / 100
return generate_random_colors(n_colors, hue, saturation, lightness)
def generate_random_dark_blue(n_colors=1):
"""Generate random dark black/gray/blue colors"""
hue = random.randint(180, 240) / 360
saturation = random.randint(75, 100) / 100
lightness = random.randint(0, 15) / 100
return generate_random_colors(n_colors, hue, saturation, lightness)
def generate_random_dark_green(n_colors=1):
"""Generate random dark green colors"""
hue = random.randint(90, 150) / 360
saturation = random.randint(60, 100) / 100
lightness = random.randint(10, 20) / 100
return generate_random_colors(n_colors, hue, saturation, lightness)
def generate_palette(n_palette=1):
"""Convert"""
n_colors = 24
groups = 6
counts = n_colors / groups
all_palettes = []
for _i in range(n_palette):
palette = []
dark_red = generate_random_dark_red()
dark_purple = generate_random_dark_purple()
dark_orange = generate_random_dark_orange()
dark_green = generate_random_dark_green()
dark_blue = generate_random_dark_blue()
dark_black = generate_random_dark_black()
for dark_color in [
c
for dark in (
dark_red,
dark_purple,
dark_orange,
dark_green,
dark_blue,
dark_black,
)
for c in dark
]:
_colors = sns.color_palette(
f"light:{dark_color}", n_colors=n_colors
)
_colors_hex = _colors.as_hex()
_picked_colors_index = [
c + (counts - 1) for c in range(n_colors) if c % counts == 0
]
_picked_colors = [
_colors_hex[int(index)] for index in _picked_colors_index
]
palette.extend(_picked_colors)
all_palettes.append(palette)
return all_palettes
def create_theme_palette():
"""Create palette for theme.
Returned 9 colors:
light red, red, dark red; light green, green, dark green, light gray,
gray, dark gray.
"""
return generate_palette()[0]
def main():
"""Test"""
all_palettes = generate_palette(1)
print(all_palettes)
if __name__ == "__main__":
main()