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

Skip to content

improve docstring and add test fot to_rgb(<float>) #2610

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

Merged
merged 1 commit into from
Nov 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 5 additions & 2 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,13 @@ def to_rgb(self, arg):
1) a letter from the set 'rgbcmykw'
2) a hex color string, like '#00FFFF'
3) a standard name, like 'aqua'
4) a float, like '0.4', indicating gray on a 0-1 scale
4) a string representation of a float, like '0.4',
indicating gray on a 0-1 scale

if *arg* is *RGBA*, the *A* will simply be discarded.
"""
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.

Copy link
Member

Choose a reason for hiding this comment

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

Please condense this comment down to one or two lines, e.g.,

# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.

try:
return self.cache[arg]
except KeyError:
Expand Down Expand Up @@ -304,7 +307,7 @@ def to_rgb(self, arg):
if fl < 0 or fl > 1:
raise ValueError(
'gray (string) must be in range 0-1')
color = tuple([fl] * 3)
color = (fl,)*3
elif cbook.iterable(arg):
if len(arg) > 4 or len(arg) < 3:
raise ValueError(
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ def test_autoscale_masked():
plt.draw()


def test_colors_no_float():
# Gray must be a string to distinguish 3-4 grays from RGB or RGBA.

def gray_from_float_rgb():
return mcolors.colorConverter.to_rgb(0.4)

def gray_from_float_rgba():
return mcolors.colorConverter.to_rgba(0.4)

assert_raises(ValueError, gray_from_float_rgb)
assert_raises(ValueError, gray_from_float_rgba)


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