-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolors.py
257 lines (223 loc) · 5.92 KB
/
colors.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from random import uniform, sample, choice, random
def hsv_to_rgb(h,s,v):
if s > 1 and v > 1:
s = s * 0.01
v = v * 0.01
c = v * s
x = c * (1 - abs((h/60) % 2 - 1))
m = v - c
if 0 <= h < 60:
r,g,b = (c,x,0)
elif 60 <= h < 120:
r,g,b = (x,c,0)
elif 120 <= h < 180:
r,g,b = (0,c,x)
elif 180 <= h < 240:
r,g,b = (0,x,c)
elif 240 <= h < 300:
r,g,b = (x,0,c)
elif 300 <= h < 360:
r,g,b = (c,0,x)
return (round((r+m)*255), round((g+m)*255), round((b+m)*255))
def rgb_to_hsv(r,g,b):
rp = r/255
gp = g/255
bp = b/255
cmax = max(rp,gp,bp)
cmin = min(rp,gp,bp)
delta = cmax-cmin
if delta == 0:
h = 0
elif cmax == rp:
h = 60 * (((gp - bp)/delta) % 6)
elif cmax == gp:
h = 60 * (((bp - rp)/delta) + 2)
elif cmax == bp:
h = 60 * (((rp - gp)/delta) + 4)
if cmax == 0:
s = 0
else:
s = delta/cmax
v = cmax
return (h,s,v)
def hsl_to_rgb(h,s,l):
c = (1 - abs((2 * l) - 1)) * s
x = c * (1 - abs((h/60) % 2 - 1))
m = l - (c/2)
if 0 <= h < 60:
r,g,b = (c,x,0)
elif 60 <= h < 120:
r,g,b = (x,c,0)
elif 120 <= h < 180:
r,g,b = (0,c,x)
elif 180 <= h < 240:
r,g,b = (0,x,c)
elif 240 <= h < 300:
r,g,b = (x,0,c)
elif 300 <= h < 360:
r,g,b = (c,0,x)
return (round((r+m)*255), round((g+m)*255), round((b+m)*255))
def rgb_to_hsl(r,g, b):
rp = r/255
gp = g/255
bp = b/255
cmax = max(rp,gp,bp)
cmin = min(rp,gp,bp)
delta = cmax-cmin
if delta == 0:
h = 0
elif cmax == rp:
h = 60 * (((gp - bp)/delta) % 6)
elif cmax == gp:
h = 60 * (((bp - rp)/delta) + 2)
elif cmax == bp:
h = 60 * (((rp - gp)/delta) + 4)
if delta == 0:
s = 0
else:
s = delta/(1 - abs((2 * l) - 1))
l = (cmax + cmin) / 2
return (h,s,l)
"""
normalize value from
0 1 float range to 0 255 integer range
"""
def normalizeForFloatToWeb(value):
return round(255/(value - 1) + 255)
"""
normalize value from
0 255 integer range to 0 1 float range
"""
def normalizeForWebToFloat(value):
return round(1/(255 * (value - 255)) + 1)
"""
Generate random rgb color
"""
def generateRandomRgb(minR=0, maxR=255, minV=0, maxV=255, minB=0, maxB=255):
return (
choice(range(minR,maxR + 1)),
choice(range(minV,maxV + 1)),
choice(range(minB,maxB + 1))
)
"""
Generate random rgb from hsv components
hue a un range de 0 à 360 (degrés)
saturation et value ont un range de 0 à 1
"""
def generateRandomRgbFromHsv(minH=0, maxH=360, minS=0, maxS=1, minV=0, maxV=1):
return hsv_to_rgb(
uniform(minH, maxH),
uniform(minS, maxS),
uniform(minV, maxV),
)
"""
Generate random rgb from hls components
hue a un range de 0 à 360 (degrés)
light et saturation ont un range de 0 à 1
"""
def generateRandomRgbFromHsl(minH=0, maxH=360, minL=0, maxL=1, minS=0, maxS=1):
return hls_to_rgb(
uniform(minH, maxH),
uniform(minL, maxL),
uniform(minS, maxS),
)
"""
Generate pastel RGB color
"""
def generatePastelRgb():
return (
round(random() * 127) + 127,
round(random() * 127) + 127,
round(random() * 127) + 127,
)
"""
Blender uses (r,g,b,a) floating points colors
All values in the tuple goes from 0 to 1.
"""
def rgbToBlender(rgb):
r,g,b = rgb
return (
r/256,
g/256,
b/256,
1
)
"""
Generate a new color by adding
rgb components and divide them by 2
"""
def addMixRgb(rgb1, rgb2):
r1,g1,b1 = rgb1
r2,g2,b2 = rgb2
return (
(r1 + r2) / 2,
(g1 + g2) / 2,
(b1 + b2) / 2,
)
"""
Generate a new color by subbing
rgb components and multiply them by 2
"""
def subMixRgb(rgb1, rgb2):
r1,g1,b1 = rgb1
r2,g2,b2 = rgb2
return (
(r1 - r2) * 2,
(g1 - g2) * 2,
(b1 - b2) * 2,
)
"""
Generate new color based on golden angle shift
http://en.wikipedia.org/wiki/Golden_angle
http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
"""
def goldenRatioRgb(rgb, golden_ratio=137.508):
hsv = rgb_to_hsv(*rgb)
h,s,v = hsv
h = (h + golden_ratio) % 360 # (hue + golden_ratio) % 360
return hsv_to_rgb(h,s,v)
"""
Generate color theme based on a specific
algorithm
The "method" argument tell what algorithm to use
It takes 3 values:
- add
- sub
- gold (golden ratio)
"""
def generateColorSchemeRgb(rgb, numbers=5, method="gold", **kwargs):
colors = [rgb]
if kwargs.get('shuffle') == True:
for _ in range(0,numbers):
colorMethod = choice([1,2,3])
if colorMethod == 1:
colors.append(addMixRgb(rgb, generateRandomRgb()))
elif colorMethod == 2:
colors.append(subMixRgb(rgb, generateRandomRgb()))
elif colorMethod == 3:
if(kwargs.get('ratio')):
colors.append(goldenRatioRgb(colors[-1], kwargs['ratio']))
else:
colors.append(goldenRatioRgb(colors[-1]))
else:
for _ in range(0,numbers):
if method == "add":
colors.append(addMixRgb(rgb, generateRandomRgb()))
elif method == "sub":
colors.append(subMixRgb(rgb, generateRandomRgb()))
elif method == "gold":
if(kwargs.get('ratio')):
colors.append(goldenRatioRgb(colors[-1], kwargs['ratio']))
else:
colors.append(goldenRatioRgb(colors[-1]))
return colors
"""
Takes a list of rgb colors, and compute the mean
"""
def meanColorFromThemeRgb(theme):
def mean(l):
return sum(l) / len(l)
r = mean([rgb[0] for rgb in theme ])
g = mean([rgb[1] for rgb in theme ])
b = mean([rgb[2] for rgb in theme ])
return (r,g,b,1)