-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Closed
Shade color #2745
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 c926ba5
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 d4233b8
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 eca57fe
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 3216669
using plt
90f63f8
pylab to plt
1ee2986
pylab to plt and np
0fa4b94
pylab to plt and np
1b72df1
pylab to plt and np
a0665f7
pylab to plt and np
8f2b84c
DOC: slightly update demo
tacaswell 887e20d
DOC: whats_new for axes.labelpad
tacaswell 188d7a1
DOC: protect against non-gui backends in demo
tacaswell b328cbb
pep8
5a5f458
pep8
b26a521
pep8
3720671
pep8
a7fa5d9
pep8
38215ef
pep8
0f726dc
Implement draw_idle
mdehoon 0bbc091
Merge pull request #4731 from mdehoon/implementDrawIdle
tacaswell 5cadac1
Merge pull request #4724 from tacaswell/doc_update_demo
jenshnielsen 6e2ee31
Merge pull request #4739 from jkseppan/pdf-paint
efiring 0a7af4e
Merge pull request #4726 from tacaswell/doc_axeslabelpad_doc
efiring a967158
Merge pull request #4654 from domspad/MEP12-on-annotation-demo2.py
jenshnielsen 8dd9c29
Merge pull request #4663 from domspad/MEP12-on-axes_props.py
jenshnielsen d1d0122
Merge pull request #4657 from domspad/MEP12-on-anscombe.py
jenshnielsen b8d7e25
Merge pull request #4660 from domspad/MEP12-on-arrow_demo.py
jenshnielsen f37e6ef
Merge pull request #4664 from domspad/MEP12-on-axis_equal_demo.py
jenshnielsen 708468b
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 1c70b15
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 badd1f8
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 49b9b25
NF - added new feature to colors called shade_color to lighten or dar…
dvreed77 913a3b4
merged changes
dvreed77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
commit d4233b87715338c77139e4328ba45c7020d7d53d
There are no files selected for viewing
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
|
@@ -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): | ||
"""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 | ||
---------- | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains hidden or 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
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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 😄