Skip to content

Instantly share code, notes, and snippets.

@technic
Forked from ihincks/lighten_color.py
Created January 30, 2019 16:32
Show Gist options
  • Save technic/8bf3932ad7539b762a05da11c0093ed5 to your computer and use it in GitHub Desktop.
Save technic/8bf3932ad7539b762a05da11c0093ed5 to your computer and use it in GitHub Desktop.
Function to lighten any color in matplotlib
def lighten_color(color, amount=0.5):
"""
Lightens the given color by multiplying (1-luminosity) by the given amount.
Input can be matplotlib color string, hex string, or RGB tuple.
Examples:
>> lighten_color('g', 0.3)
>> lighten_color('#F034A3', 0.6)
>> lighten_color((.3,.55,.1), 0.5)
"""
import matplotlib.colors as mc
import colorsys
try:
c = mc.cnames[color]
except:
c = color
c = np.array(colorsys.rgb_to_hls(*mc.to_rgb(c)))
return colorsys.hls_to_rgb(c[0],1-amount * (1-c[1]),c[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment