Skip to content

Instantly share code, notes, and snippets.

@macoj
Last active December 21, 2016 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macoj/1ec35755a54c900de69ba73b38c2f128 to your computer and use it in GitHub Desktop.
Save macoj/1ec35755a54c900de69ba73b38c2f128 to your computer and use it in GitHub Desktop.
darker color, brighter color, same hue

Sometimes I want a color just brighter or darker:

def shade_color(hex_color, offset):
    new_color = hex_color
    if len(hex_color) in [6, 7]:
        if hex_color[0] == "#":
            hex_color = hex_color[1:]
        rgb_hex = np.array([float(int(hex_color[x:x+2], 16)) for x in [0, 2, 4]])
        rgb_hex /= 255.0
        hls_color = colorsys.rgb_to_hls(*rgb_hex)
        new_color = colorsys.hls_to_rgb(hls_color[0], min(1, hls_color[1]*offset), hls_color[2])
    return "#" + "".join(['%2s' % (hex(int(i*255))[2:]).rjust(2, '0') for i in new_color])

which you can use as the following:

import matplotlib.pyplot as plt
color = '#62BAAB'
x = np.arange(50)
for offset in np.arange(0.0, 2, 0.02):
    plt.plot(x, [offset]*50, color=Plotter.shade_color(color, offset), linewidth=5)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment