3 ms·
I think HSL would probably be a better choice than HSV. Python has HSL support built in, so you can do that pretty easily: from colorsys import rgb_to_hls,
by eurleif 15y ago
I think HSL would probably be a better choice than HSV. Python has HSL support built in, so you can do that pretty easily:
from colorsys import rgb_to_hls, hls_to_rgb
def adjust_color_lightness(r, g, b, factor):
h, l, s = rgb_to_hls(r / 255.0, g / 255.0, b / 255.0)
l = max(min(l * factor, 1.0), 0.0)
r, g, b = hls_to_rgb(h, l, s)
return int(r * 255), int(g * 255), int(b * 255)
def lighten_color(r, g, b, factor=0.1):
return adjust_color_lightness(r, g, b, 1 + factor)
def darken_color(r, g, b, factor=0.1):
return adjust_color_lightness(r, g, b, 1 - factor)
Though, I'm not sure if doing it by multiplying is correct. I don't like that lighten_color(*darken_color(r, g, b)) != (r, g, b)