Skip to content

Commit

Permalink
Avoid floating point error with maximum husl sat/lum (#2000)
Browse files Browse the repository at this point in the history
* homogenization to use _color_to_rgb and normalization of husl_to_rgb output in [0,1]

* added better test to husl_to_rgb input and output

* Use numpy function and improve tests

Co-authored-by: Ivan Gonzalez <scratchmex@gmail.com>
(cherry picked from commit b47d843)
  • Loading branch information
mwaskom committed Mar 17, 2020
1 parent 7641b66 commit aff322a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion seaborn/palettes.py
Expand Up @@ -382,7 +382,7 @@ def husl_palette(n_colors=6, h=.01, s=.9, l=.65): # noqa
hues *= 359
s *= 99
l *= 99 # noqa
palette = [husl.husl_to_rgb(h_i, s, l) for h_i in hues]
palette = [_color_to_rgb((h_i, s, l), input='husl') for h_i in hues]
return _ColorPalette(palette)


Expand Down Expand Up @@ -469,8 +469,10 @@ def _color_to_rgb(color, input):
color = colorsys.hls_to_rgb(*color)
elif input == "husl":
color = husl.husl_to_rgb(*color)
color = tuple(np.clip(color, 0, 1))
elif input == "xkcd":
color = xkcd_rgb[color]

return color


Expand Down
10 changes: 8 additions & 2 deletions seaborn/tests/test_palettes.py
Expand Up @@ -194,8 +194,14 @@ def test_rgb_from_husl(self):

color = 120, 50, 40
rgb_got = palettes._color_to_rgb(color, "husl")
rgb_want = husl.husl_to_rgb(*color)
nt.assert_equal(rgb_got, rgb_want)
rgb_want = tuple(husl.husl_to_rgb(*color))
assert rgb_got == rgb_want

for h in range(0, 360):
color = h, 100, 100
rgb = palettes._color_to_rgb(color, "husl")
assert min(rgb) >= 0
assert max(rgb) <= 1

def test_rgb_from_xkcd(self):

Expand Down

0 comments on commit aff322a

Please sign in to comment.