-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpalette.py
232 lines (185 loc) · 7.78 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
"""Palette and Colors."""
import colorsys
import random
from utils import get_tmux_option
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 HSL color to HEX code.
Parameter:
hls_color - tuple containing hue, lightness, and saturation color codes
such as (0.5277777777777778, 0.04, 1).
"""
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_scheme_colors(hex_color, n_colors=7):
""" "Convert."""
assert hex_color is not None, "Invalid argument: hex_color is None."
assert (
n_colors is not None and isinstance(n_colors, int) and n_colors > 0
), f"Invalid argument: n_colors = {n_colors}"
hls_color = hex2hls(hex_color)
triadic_colors = []
for offset in range(0, 360, 360 // n_colors):
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][0:n_colors]
def padding(num, target_length):
"""
Padding left for number to make it's string format length reaches the target length.
This is mainly used to construct valid hex color number in R,G,B
position. Example, if the given num is a hex number 0xf and the
target length is 2, then the padding result is 0f.
"""
str_num = str(num)
if str_num.startswith("0x"):
str_num = str_num[2:]
if len(str_num) < target_length:
str_num = (
f"{''.join(['0' for i in range(target_length - len(str_num))])}{str_num}"
)
return str_num
def lighter(base_color, n_color):
"""Given base color, return 'n' color hex codes from base color to lightest
color."""
color_rgb = tuple(int(base_color[1:][i : i + 2], 16) for i in (0, 2, 4))
color_rgb_ligher = tuple(
[c for c in range(color, 255, (255 - color) // n_color)][0:n_color]
for color in color_rgb
)
lighter_colors = [
f"#{''.join(tuple(padding(hex(color_ligher[index]), 2) for color_ligher in color_rgb_ligher))}"
for index in range(0, n_color)
]
return lighter_colors
def random_color(
min_color=0,
max_color=231,
eutmux_base_color_total=7,
eutmux_light_color_total=24,
):
"""
Generate random color hex codes.
Firstly, it will generate random integer from min_color (0-(255 - eutmux_light_color_total - 1)) to max_color (0-(255 - eutmux_light_color_total)).
The max_color should be less than (255 - eutmux_light_color_total) because it needs the room to generate lighter colors.
To generate darker colors, use smaller value for max_color.
To generate ligher colors, use bigger value for min_color.
It's recommended to use default values.
If you want to make change, please make sure what you are doing.
Secondly, it will generate 'eutmux_light_color_total' different hex color codes from base color to the lightest color.
Note that 'eutmux_light_color_total' includes base color also. It means it will generate 'eutmux_light_color_total - 1' lighter colors besides base color.
Parameters:
min_color - minimum color code. default: 0.
max_color - maximum color code. default: 254.
eutmux_base_color_total - how many base colors to generate. default: 7.
eutmux_light_color_total - how many lighter colors to generate. default: 24.
plot: True to plot the generated color. Otherwise, False. default: False.
Retrun:
Generated random base colors and all lighter colors of each base color.
The returned value is a two-dimention list. First dimention length is the value of eutmux_base_color_total. Second dimention length is eutmux_light_color_total.
"""
random_int = random.randint(0, 15**6)
_min = min_color
_max = max_color
random_color_code = "#"
for counter in range(0, 3):
random_int = random.randint(_min, _max)
random_color = padding(hex(random_int), 2)
random_color_code = random_color_code + random_color
base_colors = get_scheme_colors(random_color_code, eutmux_base_color_total)[
0:eutmux_base_color_total
]
random_colors = []
for base_color in base_colors:
lighter_colors = lighter(base_color, eutmux_light_color_total)
random_colors.append(lighter_colors)
return random_colors
class Palette:
"""Generate palette colors."""
def __init__(self):
# random colors are used for sections, components, and pieces
self.eutmux_base_color_total = get_tmux_option("@eutmux_base_color_total", 5)
self.eutmux_light_color_total = get_tmux_option("@eutmux_light_color_total", 6)
def generate_palette_colors(self):
"""
Generate random palette.
6 group base colors: 5 base colors + dark gray color. echo base
color has 6 different colors from dark to light. placeholders
are from light to dark, so need to reverse the order.
"""
random_colors = random_color(
max_color=200,
eutmux_base_color_total=self.eutmux_base_color_total,
eutmux_light_color_total=self.eutmux_light_color_total,
)
# dark colors are used for status-line
dark_colors = random_color(
max_color=30, eutmux_base_color_total=1, eutmux_light_color_total=6
)
random_colors.extend(dark_colors)
for r_colors in random_colors:
r_colors.reverse()
return [color for r_colors in random_colors for color in r_colors]
def generate_palette(self):
"""
Generate palette content.
Palette contains a list of colors. Each color is a pair of color
name and color code.
The format is "C_[base color sequence]_[colormap sequence]".
For example, "C_1_1":"#8f67ff".
Note:
The 'base color sequence' starts from 1 to @eutmux_base_color_total (not
included)
The 'colormap sequence' starts from 0 to @eutmux_light_color_total (not
included)
When "colormap sequence" is 0, then it represents the lightest color.
One continuous colormap is for one base color and consists of a
group of colors from lightest color to the base color.
Return:
A list of palette colors.
"""
palette_color_codes = self.generate_palette_colors()
base_color_sequence = 0
colormap_sequence = 0
palette_colors = []
for index, color in enumerate(palette_color_codes):
colormap_sequence = index % self.eutmux_light_color_total
if colormap_sequence == 0:
base_color_sequence += 1
color_name = f"C_{base_color_sequence}_{colormap_sequence}"
palette_colors.append(f"{color_name}:{color}")
return palette_colors
def generate_palette():
"""Generate palette colors."""
return Palette().generate_palette()
def main():
"""Test."""
palette = Palette()
for counter in range(9):
r_colors = random_color(max_color=100)
print(r_colors)
print(palette.generate_palette())
if __name__ == "__main__":
main()