Skip to content

Shade color #2745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2834c09
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 Jan 19, 2014
c926ba5
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 Jan 19, 2014
d4233b8
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 May 16, 2015
eca57fe
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 May 16, 2015
3216669
using plt
Jul 11, 2015
90f63f8
pylab to plt
Jul 11, 2015
1ee2986
pylab to plt and np
Jul 11, 2015
0fa4b94
pylab to plt and np
Jul 11, 2015
1b72df1
pylab to plt and np
Jul 11, 2015
a0665f7
pylab to plt and np
Jul 11, 2015
8f2b84c
DOC: slightly update demo
tacaswell Jul 17, 2015
887e20d
DOC: whats_new for axes.labelpad
tacaswell Jul 17, 2015
188d7a1
DOC: protect against non-gui backends in demo
tacaswell Jul 17, 2015
b328cbb
pep8
Jul 17, 2015
5a5f458
pep8
Jul 17, 2015
b26a521
pep8
Jul 17, 2015
3720671
pep8
Jul 17, 2015
a7fa5d9
pep8
Jul 17, 2015
38215ef
pep8
Jul 17, 2015
0f726dc
Implement draw_idle
mdehoon Jul 17, 2015
0bbc091
Merge pull request #4731 from mdehoon/implementDrawIdle
tacaswell Jul 17, 2015
5cadac1
Merge pull request #4724 from tacaswell/doc_update_demo
jenshnielsen Jul 17, 2015
6e2ee31
Merge pull request #4739 from jkseppan/pdf-paint
efiring Jul 18, 2015
0a7af4e
Merge pull request #4726 from tacaswell/doc_axeslabelpad_doc
efiring Jul 18, 2015
a967158
Merge pull request #4654 from domspad/MEP12-on-annotation-demo2.py
jenshnielsen Jul 19, 2015
8dd9c29
Merge pull request #4663 from domspad/MEP12-on-axes_props.py
jenshnielsen Jul 19, 2015
d1d0122
Merge pull request #4657 from domspad/MEP12-on-anscombe.py
jenshnielsen Jul 19, 2015
b8d7e25
Merge pull request #4660 from domspad/MEP12-on-arrow_demo.py
jenshnielsen Jul 19, 2015
f37e6ef
Merge pull request #4664 from domspad/MEP12-on-axis_equal_demo.py
jenshnielsen Jul 19, 2015
708468b
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 Jan 19, 2014
1c70b15
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 Jan 19, 2014
badd1f8
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 May 16, 2015
49b9b25
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 May 16, 2015
913a3b4
merged changes
dvreed77 Jul 19, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
NF - added new feature to colors called shade_color to lighten or dar…
…ken a given color by a given percentage, added modifications noted in PR #2745 which included fixing PEP8 errors and adding a test.
  • Loading branch information
dvreed77 committed May 16, 2015
commit d4233b87715338c77139e4328ba45c7020d7d53d
25 changes: 14 additions & 11 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
"""
from __future__ import print_function, division
import re
from colorsys import rgb_to_hls, hls_to_rgb

import numpy as np
from numpy import ma
import matplotlib.cbook as cbook
from colorsys import rgb_to_hls as rgb2hls, hls_to_rgb as hls2rgb


parts = np.__version__.split('.')
NP_MAJOR, NP_MINOR = map(int, parts[:2])
Expand Down Expand Up @@ -1485,11 +1487,15 @@ def from_levels_and_colors(levels, colors, extend='neither'):
norm = BoundaryNorm(levels, ncolors=n_data_colors)
return cmap, norm


def shade_color(color, percent):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple of PEP8 issues here - top level objects in a module should have 2 newlines between them, the docstring's leading line should be slightly more descriptive, no need for a newline after the docstring, spaces after commas needed, excessive new lines in the function itself.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you let me know what your mean by "top level objects". Thanks for the comments!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just anything that is at the top level of the module, such as this function. Don't worry about post-PEP8-ing other code (that should be done in another PR, but anything that is being added or amended we are trying to bring into line with our coding standards).

As I say, it sounds really picky, but it is good for the health of the mpl codebase so thank you for persevering 😄

"""Shade Color
"""
A color helper utility to either darken or lighten given color.

This color utility function allows the user to easily darken or lighten a color for
plotting purposes.
plotting purposes. This function first converts the given color to RGB using
ColorConverter and then to HSL. The saturation is modified according to the given
percentage and converted back to RGB.

Parameters
----------
Expand All @@ -1505,17 +1511,14 @@ def shade_color(color, percent):

"""

cc = ColorConverter()

rgb = cc.to_rgb(color)
rgb = colorConverter.to_rgb(color)

h,l,s = rgb2hls(*rgb)
h, l, s = rgb_to_hls(*rgb)

l *= 1 + float(percent)/100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using np.clip for the clipping part.


l = min(1, l)
l = max(0, l)
l = np.clip(l, 0, 1)

r,g,b = hls2rgb(h,l,s)
r, g, b = hls_to_rgb(h, l, s)

return r,g,b
return r, g, b
29 changes: 27 additions & 2 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import print_function
from nose.tools import assert_raises
import numpy as np
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal

from numpy.testing.utils import assert_array_equal, assert_array_almost_equal, assert_equal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this more than 80 chars wide?


import matplotlib.colors as mcolors
import matplotlib.cm as cm
Expand Down Expand Up @@ -175,6 +174,32 @@ def test_cmap_and_norm_from_levels_and_colors2():
assert_raises(ValueError, mcolors.from_levels_and_colors, levels, colors)


def _shade_test_helper(color, shade, expected):
sc = mcolors.shade_color(color, shade)
assert_equal(sc, expected)


def test_color_shading():
test_colors = (
'white',
'red',
'black',
[0, .5, .9],
'slategrey',
)
test_shade = (0, .5, 1, -.5, -1)
known_shaded_result = (
(1.0, 1.0, 1.0),
(1.0, 0.0049999999999998934, 0.0049999999999998934),
(0.0, 0.0, 0.0),
(0.0, 0.49749999999999983, 0.89549999999999996),
(0.43433441408059281, 0.49694117647058816, 0.55954793886058363)
)
for color, shade, expected in zip(test_colors, test_shade, known_shaded_result):
_shade_test_helper(color, shade, expected)



if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)