Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2834c09

Browse files
committed
NF - added new feature to colors called shade_color to lighten or darken a given color by a given percentage
1 parent 5e8d808 commit 2834c09

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

lib/matplotlib/colors.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import numpy as np
5252
from numpy import ma
5353
import matplotlib.cbook as cbook
54+
from colorsys import rgb_to_hls as rgb2hls, hls_to_rgb as hls2rgb
5455

5556
parts = np.__version__.split('.')
5657
NP_MAJOR, NP_MINOR = map(int, parts[:2])
@@ -1483,3 +1484,38 @@ def from_levels_and_colors(levels, colors, extend='neither'):
14831484

14841485
norm = BoundaryNorm(levels, ncolors=n_data_colors)
14851486
return cmap, norm
1487+
1488+
def shade_color(color, percent):
1489+
"""Shade Color
1490+
1491+
This color utility function allows the user to easily darken or lighten a color for
1492+
plotting purposes.
1493+
1494+
Parameters
1495+
----------
1496+
color : string, list, hexvalue
1497+
Any acceptable Matplotlib color value, such as 'red', 'slategrey', '#FFEE11', (1,0,0)
1498+
1499+
percent : the amount by which to brighten or darken the color.
1500+
1501+
Returns
1502+
-------
1503+
color : tuple of floats
1504+
tuple representing converted rgb values
1505+
1506+
"""
1507+
1508+
cc = CC()
1509+
1510+
rgb = cc.to_rgb(color)
1511+
1512+
h,l,s = rgb2hls(*rgb)
1513+
1514+
l *= 1 + float(percent)/100
1515+
1516+
l = min(1, l)
1517+
l = max(0, l)
1518+
1519+
r,g,b = hls2rgb(h,l,s)
1520+
1521+
return r,g,b

0 commit comments

Comments
 (0)