Created
June 22, 2017 21:12
-
-
Save ryananguiano/0b424ce3b6ee70889b20712d719596c1 to your computer and use it in GitHub Desktop.
color variant fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fix for http://chase-seibert.github.io/blog/2011/07/29/python-calculate-lighterdarker-rgb-colors.html | |
def color_variant(hex_color, brightness_offset=1): | |
""" takes a color like #87c95f and produces a lighter or darker variant """ | |
if len(hex_color) != 7: | |
raise Exception("Passed %s into color_variant(), needs to be in #87c95f format." % hex_color) | |
rgb_hex = [hex_color[x:x+2] for x in [1, 3, 5]] | |
new_rgb_int = [int(hex_value, 16) + brightness_offset for hex_value in rgb_hex] | |
new_rgb_int = [min([255, max([0, i])]) for i in new_rgb_int] # make sure new values are between 0 and 255 | |
# hex() produces "0x88", we want just "88" | |
return "#" + "".join([hex(i)[2:] if len(hex(i)[2:]) == 2 else '0' + hex(i)[2:] for i in new_rgb_int]) | |
# OLD: | |
# return "#" + "".join([hex(i)[2:] for i in new_rgb_int]) | |
""" | |
OLD: color_variant('#ff8022', -20) == '#eb6ce' | |
NEW: color_variant('#ff8022', -20) == '#eb6c0e' | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment