8000 Merge pull request #15152 from PatrickFeiring/shorthand-hex-colors · matplotlib/matplotlib@b12a3a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit b12a3a8

Browse files
authored
Merge pull request #15152 from PatrickFeiring/shorthand-hex-colors
Support for shorthand hex colors.
2 parents 9086f0a + 790756f commit b12a3a8

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
3-digit and 4-digit hex colors
2+
------------------------------
3+
4+
Colors can now be specified using 3-digit or 4-digit hex colors, shorthand for
5+
the colors obtained by duplicating each character, e.g. ``#123`` is equivalent to
6+
``#112233`` and ``#123a`` is equivalent to ``#112233aa``.

lib/matplotlib/colors.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
interval ``[0, 1]`` (e.g., ``(0.1, 0.2, 0.5)`` or ``(0.1, 0.2, 0.5, 0.3)``);
4141
* a hex RGB or RGBA string (e.g., ``'#0f0f0f'`` or ``'#0f0f0f80'``;
4242
case-insensitive);
43+
* a shorthand hex RGB or RGBA string, equivalent to the hex RGB or RGBA
44+
string obtained by duplicating each character, (e.g., ``'#abc'``, equivalent
45+
to ``'#aabbcc'``, or ``'#abcd'``, equivalent to ``'#aabbccdd'``;
46+
case-insensitive);
4347
* a string representation of a float value in ``[0, 1]`` inclusive for gray
4448
level (e.g., ``'0.5'``);
4549
* one of ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}``, they are the single
@@ -215,20 +219,34 @@ def _to_rgba_no_colorcycle(c, alpha=None):
215219
"%(since)s and will be removed %(removal)s; please "
216220
"use lowercase instead.")
217221
if isinstance(c, str):
218-
# hex color with no alpha.
222+
# hex color in #rrggbb format.
219223
match = re.match(r"\A#[a-fA-F0-9]{6}\Z", c)
220224
if match:
221225
return (tuple(int(n, 16) / 255
222226
for n in [c[1:3], c[3:5], c[5:7]])
223227
+ (alpha if alpha is not None else 1.,))
224-
# hex color with alpha.
228+
# hex color in #rgb format, shorthand for #rrggbb.
229+
match = re.match(r"\A#[a-fA-F0-9]{3}\Z", c)
230+
if match:
231+
return (tuple(int(n, 16) / 255
232+
for n in [c[1]*2, c[2]*2, c[3]*2])
233+
+ (alpha if alpha is not None else 1.,))
234+
# hex color with alpha in #rrggbbaa format.
225235
match = re.match(r"\A#[a-fA-F0-9]{8}\Z", c)
226236
if match:
227237
color = [int(n, 16) / 255
228238
for n in [c[1:3], c[3:5], c[5:7], c[7:9]]]
229239
if alpha is not None:
230240
color[-1] = alpha
231241
return tuple(color)
242+
# hex color with alpha in #rgba format, shorthand for #rrggbbaa.
243+
match = re.match(r"\A#[a-fA-F0-9]{4}\Z", c)
244+
if match:
245+
color = [int(n, 16) / 255
246+
for n in [c[1]*2, c[2]*2, c[3]*2, c[4]*2]]
247+
if alpha is not None:
248+
color[-1] = alpha
249+
return tuple(color)
232250
# string gray.
233251
try:
234252
c = float(c)

lib/matplotlib/tests/test_colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,3 +910,8 @@ def __add__(self, other):
910910
def test_same_color():
911911
assert mcolors.same_color('k', (0, 0, 0))
912912
assert not mcolors.same_color('w', (1, 1, 0))
913+
914+
915+
def test_hex_shorthand_notation():
916+
assert mcolors.same_color("#123", "#112233")
917+
assert mcolors.same_color("#123a", "#112233aa")

tutorials/colors/colors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
interval ``[0, 1]`` (e.g., ``(0.1, 0.2, 0.5)`` or ``(0.1, 0.2, 0.5, 0.3)``);
1010
* a hex RGB or RGBA string (e.g., ``'#0f0f0f'`` or ``'#0f0f0f80'``;
1111
case-insensitive);
12+
* a shorthand hex RGB or RGBA string, equivalent to the hex RGB or RGBA
13+
string obtained by duplicating each character, (e.g., ``'#abc'``, equivalent
14+
to ``'#aabbcc'``, or ``'#abcd'``, equivalent to ``'#aabbccdd'``;
15+
case-insensitive);
1216
* a string representation of a float value in ``[0, 1]`` inclusive for gray
1317
level (e.g., ``'0.5'``);
1418
* one of ``{'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w'}``, they are the single

0 commit comments

Comments
 (0)
0