Skip to content

Commit 7c2ec4c

Browse files
committed
Default to #rrggbb, not #rrggbbaa.
1 parent d492ba4 commit 7c2ec4c

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

doc/users/colors.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ it can be provided as:
99

1010
* ``(r, g, b)`` tuples
1111
* ``(r, g, b, a)`` tuples
12-
* hex string, ex ``#OFOFOF``
12+
* hex string, ex ``#0F0F0F``, or ``#0F0F0F0F`` (with alpha channel)
1313
* float value between [0, 1] for gray level
1414
* One of ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}``
1515
* valid CSS4/X11 color names

lib/matplotlib/colors.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
The module also provides functions for checking whether an object can be
1919
interpreted as a color (:func:`is_color_like`), for converting such an object
2020
to an RGBA tuple (:func:`to_rgba`) or to an HTML-like hex string in the
21-
`#rrggbbaa` format (:func:`to_hex`), and a sequence of colors to an `(n, 4)`
21+
`#rrggbb` format (:func:`to_hex`), and a sequence of colors to an `(n, 4)`
2222
RGBA array (:func:`to_rgba_array`). Caching is used for efficiency.
2323
2424
Commands which take color arguments can use several formats to specify
@@ -240,13 +240,17 @@ def to_rgb(c):
240240
return to_rgba(c)[:3]
241241

242242

243-
def to_hex(c, alpha=None):
243+
def to_hex(c, keep_alpha=False):
244244
"""Convert `c` to a hex color.
245245
246-
If `alpha` is not `None`, it forces the alpha value.
246+
Uses the #rrggbb format if `keep_alpha` is False (the default), `#rrggbbaa`
247+
otherwise.
247248
"""
249+
c = to_rgba(c)
250+
if not keep_alpha:
251+
c = c[:3]
248252
return "#" + "".join(format(int(np.round(val * 255)), "02x")
249-
for val in to_rgba(c, alpha=alpha))
253+
for val in c)
250254

251255

252256
### Backwards-compatible color-conversion API
@@ -258,7 +262,7 @@ def to_hex(c, alpha=None):
258262

259263
def rgb2hex(c):
260264
'Given an rgb or rgba sequence of 0-1 floats, return the hex string'
261-
return to_hex(c)[:7] # Drop alpha.
265+
return to_hex(c)
262266

263267

264268
def hex2color(c):

lib/matplotlib/tests/test_colors.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ def angled_plane(azimuth, elevation, angle, x, y):
555555

556556

557557
def test_xkcd():
558-
assert mcolors.to_hex("blue") == "#0000ffff"
559-
assert mcolors.to_hex("xkcd:blue") == "#0343dfff"
558+
assert mcolors.to_hex("blue") == "#0000ff"
559+
assert mcolors.to_hex("xkcd:blue") == "#0343df"
560560

561561

562562
def _sph2cart(theta, phi):
@@ -607,8 +607,8 @@ def test_colormap_reversing():
607607
def test_cn():
608608
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
609609
['blue', 'r'])
610-
assert mcolors.to_hex("C0") == '#0000ffff'
611-
assert mcolors.to_hex("C1") == '#ff0000ff'
610+
assert mcolors.to_hex("C0") == '#0000ff'
611+
assert mcolors.to_hex("C1") == '#ff0000'
612612

613613
matplotlib.rcParams['axes.prop_cycle'] = cycler('color',
614614
['xkcd:blue', 'r'])
@@ -622,10 +622,11 @@ def test_conversions():
622622
# alpha is properly set.
623623
assert_equal(mcolors.to_rgba((1, 1, 1), .5), (1, 1, 1, .5))
624624
# builtin round differs between py2 and py3.
625-
assert_equal(mcolors.to_hex((.7, .7, .7)), "#b2b2b2ff")
625+
assert_equal(mcolors.to_hex((.7, .7, .7)), "#b2b2b2")
626626
# hex roundtrip.
627627
hex_color = "#1234abcd"
628-
assert_equal(mcolors.to_hex(mcolors.to_rgba(hex_color)), hex_color)
628+
assert_equal(mcolors.to_hex(mcolors.to_rgba(hex_color), keep_alpha=True),
629+
hex_color)
629630

630631

631632
if __name__ == '__main__':

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy