1
1
import operator
2
- import numpy
2
+ import numpy , colorsys
3
+ from material_color_utilities_python .utils .theme_utils import *
4
+
3
5
def hex2rgb (hex ):
4
6
hex = hex .lstrip ('#' )
5
7
rgb = tuple (int (hex [i :i + 2 ], 16 ) for i in (0 , 2 , 4 ))
@@ -180,44 +182,93 @@ def blend2contrast(lighter_color, darker_color, blend_color, min_contrast, blend
180
182
else :
181
183
new = blendColors (lighter_color ,blend_color , blend_ratio )
182
184
contrast = contrast_ratio (darker_color , new )
183
- #time.sleep(1)
184
185
# print(f"new: {new} vs {darker_color} blend: {blend_ratio} contrast: {contrast}")
185
186
return new
186
187
else :
187
188
return blendColors (lighter_color , blend_color , .12 )
188
189
190
+ def scale_lightness (hex_color , amount ):
191
+ r , g , b = hex2rgb (hex_color )
192
+ # convert rgb to hls
193
+ h , s , v = colorsys .rgb_to_hsv (r , g , b )
194
+ # manipulate value and convert back to rgb
195
+ r , g , b = colorsys .hsv_to_rgb (h , s , amount )
196
+ o_hex = rgb2hex (int (r ), int (g ), int (b ))
197
+ # print(f"scale_lightness color: {hex_color} * amount: {amount} = {o_hex}")
198
+ return o_hex
199
+
200
+ def lighteen_color (hex_color , min , blend ):
201
+ current_luminance = color_luminance (hex_color )[1 ]
202
+ # print(f"original luminance: {current_luminance}")
203
+ if current_luminance < min :
204
+ new_lightness = 255.0 * (1.0 - current_luminance )
205
+ # print(f increase lightness to {new_lightness}")
206
+ new_color = scale_lightness (hex_color , new_lightness )
207
+ else :
208
+ new_color = hex_color
209
+ o = blendColors (new_color ,blend ,0.2 )
210
+ # print(f"result after blend: {o}")
211
+ return o
189
212
190
213
# Tests
191
214
if __name__ == '__main__' :
192
215
# Test color blend
193
- print ("Test color blend" )
216
+ print ("> Test color blend #ff0000 , #00ff00 " )
194
217
print (blendColors ('#ff0000' , "#00ff00" , .01 ))
195
218
print (blendColors ('#ff0000' , "#00ff00" , .25 ))
196
219
print (blendColors ('#ff0000' , "#00ff00" , .5 ))
197
220
print (blendColors ('#ff0000' , "#00ff00" , .75 ))
198
221
print (blendColors ('#ff0000' , "#00ff00" , .99 ))
199
222
200
- print ("Test color hex2alpha" )
201
- print (hex2alpha ('#ff0000' ,128 ))
223
+ print ("> Test color hex2alpha '#ff0000',50" )
224
+ print (hex2alpha ('#ff0000' ,50 ))
225
+
202
226
color1hex = '#082523'
203
227
color1rgb = hex2rgb (color1hex )
204
228
color1rgb_alpha = rgb2alpha (color1rgb ,200 )
205
- print ("Test color rgb2alpha" )
229
+
230
+ print ("> Test color rgb2alpha" )
206
231
print (color1rgb_alpha )
207
- print ("Test color hex2rgba" )
232
+ print ("> Test color hex2rgba" )
208
233
color1rgba = hex2rgba (color1hex ,200 )
209
234
print (color1rgba )
210
- print ("Test color_luminance" )
235
+ print ("> Test color_luminance" )
211
236
print (color_luminance (color1hex ))
212
237
213
- colors_list = ('#f96767' ,'#ff8400' ,'#ffd500' ,'#00fffb' ,'#c1f7fb' ,'#00eeff' )
214
- print (">Test sort_colors_luminance" )
215
- sort_colors_luminance (colors_list )
238
+ colors_list = ('#f96767' ,'#222250' , '# ff8400' ,'#ffd500' ,'#00fffb' ,'#c1f7fb' ,'#00eeff' )
239
+ print ("> Test sort_colors_luminance '#f96767','#222250','#ff8400','#ffd500','#00fffb','#c1f7fb','#00eeff' " )
240
+ print ( sort_colors_luminance (colors_list ) )
216
241
217
- print ("Test contrast_ratio" )
218
- contrast_ratio ('#475AC6' ,'#1A1A22' )
242
+ print ("> Test contrast_ratio '#475AC6','#1A1A22' " )
243
+ print ( contrast_ratio ('#475AC6' ,'#1A1A22' ) )
219
244
245
+ print ("> Test blend2contrast '#475AC6','#1A1A22','#c1f7fb',4.5 ,0.1, True" )
220
246
print (blend2contrast ('#475AC6' ,'#1A1A22' ,'#c1f7fb' ,4.5 ,0.1 , True ))
221
-
247
+ print ( "> Test blend2contrast '#e1ffb4','#FEFCF5','#060605', 4.5, 0.01, False" )
222
248
print (blend2contrast ('#e1ffb4' ,'#FEFCF5' ,'#060605' , 4.5 , 0.01 , False ))
223
-
249
+
250
+ print ("> Oklab vs cam16 blend '#ff0000', '#0000ff', .5" )
251
+ print (f"oklab: { blendColors ('#ff0000' , '#0000ff' , .5 )} " )
252
+ print (f"cam16: { hexFromArgb (Blend .cam16Ucs (argbFromHex ('#ff0000' ),argbFromHex ('#0000ff' ),0.5 ))} " )
253
+
254
+ print ("> lighteen_color '#b70708',.15,'#ffffff'" )
255
+ print (lighteen_color ('#b70708' ,.15 ,'#ffffff' ))
256
+
257
+ test_colors = [ '#000000' ,
258
+ '#4141a6' ,
259
+ '#1dc136' ,
260
+ '#bbb13c' ,
261
+ '#ed19cd' ,
262
+ '#e40f0f' ,
263
+ '#fe6c0b' ,
264
+ '#fff000' ,
265
+ '#36e5d3' ,
266
+ '#131aed' ,
267
+ '#ff0000' ,
268
+ '#00ff00' ,
269
+ '#0000ff' ,
270
+ '#ffffff'
271
+ ]
272
+
273
+ for color in test_colors :
274
+ print (color_luminance (color ))
0 commit comments