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

Skip to content

Commit 22a7b95

Browse files
committed
Merge pull request #6382 from anntzer/rgba-support
ENH/API: New color conversion machinery
2 parents c94c282 + 7c2ec4c commit 22a7b95

32 files changed

+385
-409
lines changed

doc/conf.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,19 @@ def getapi(*args):
334334
sys.modules['sip'] = mocksip
335335
sys.modules['PyQt4'] = mockpyqt4
336336

337-
################# numpydoc config ####################
337+
# numpydoc config
338+
338339
numpydoc_show_class_members = False
340+
341+
# Skip deprecated members
342+
343+
def skip_deprecated(app, what, name, obj, skip, options):
344+
if skip:
345+
return skip
346+
skipped = {"matplotlib.colors": ["ColorConverter", "hex2color", "rgb2hex"]}
347+
skip_list = skipped.get(getattr(obj, "__module__", None))
348+
if skip_list is not None:
349+
return getattr(obj, "__name__", None) in skip_list
350+
351+
def setup(app):
352+
app.connect('autodoc-skip-member', skip_deprecated)

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

doc/users/whats_new/rgba-support.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Improved color conversion API and RGBA support
2+
----------------------------------------------
3+
4+
The :module:`~matplotlib.colors` gained a new color conversion API with
5+
full support for the alpha channel. The main public functions are
6+
:func:`~matplotlib.colors.is_color_like`, :func:`matplotlib.colors.to_rgba`,
7+
:func:`matplotlib.colors.to_rgba_array` and :func:`~matplotlib.colors.to_hex`.
8+
RGBA quadruplets are encoded in hex format as `#rrggbbaa`.
9+
10+
A side benefit is that the Qt options editor now allows setting the alpha
11+
channel of the artists as well.

examples/api/collections_demo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
'''
1818

1919
import matplotlib.pyplot as plt
20-
from matplotlib import collections, transforms
21-
from matplotlib.colors import colorConverter
20+
from matplotlib import collections, colors, transforms
2221
import numpy as np
2322

2423
nverts = 50
@@ -38,7 +37,7 @@
3837
xyo = list(zip(xo, yo))
3938

4039
# Make a list of colors cycling through the default series.
41-
colors = [colorConverter.to_rgba(c)
40+
colors = [colors.to_rgba(c)
4241
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
4342

4443
fig, axes = plt.subplots(2, 2)

examples/color/named_colors.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,19 @@
1111

1212
import numpy as np
1313
import matplotlib.pyplot as plt
14-
from matplotlib import colors
14+
from matplotlib import colors as mcolors
1515

1616

17-
colors_ = list(six.iteritems(colors.cnames))
18-
19-
# Add the single letter colors.
20-
for name, rgb in six.iteritems(colors.ColorConverter.colors):
21-
hex_ = colors.rgb2hex(rgb)
22-
colors_.append((name, hex_))
23-
24-
# Transform to hex color values.
25-
hex_ = [color[1] for color in colors_]
26-
# Get the rgb equivalent.
27-
rgb = [colors.hex2color(color) for color in hex_]
28-
# Get the hsv equivalent.
29-
hsv = [colors.rgb_to_hsv(color) for color in rgb]
30-
31-
# Split the hsv values to sort.
32-
hue = [color[0] for color in hsv]
33-
sat = [color[1] for color in hsv]
34-
val = [color[2] for color in hsv]
35-
36-
# Get the color names by themselves.
37-
names = [color[0] for color in colors_]
17+
colors = dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS)
3818

3919
# Sort by hue, saturation, value and name.
40-
ind = np.lexsort((names, val, sat, hue))
41-
sorted_colors = [colors_[i] for i in ind]
20+
by_hsv = sorted((tuple(mcolors.rgb_to_hsv(mcolors.to_rgba(color)[:3])), name)
21+
for name, color in colors.items())
22+
23+
# Get the sorted color names.
24+
sorted_names = [name for hsv, name in by_hsv]
4225

43-
n = len(sorted_colors)
26+
n = len(sorted_names)
4427
ncols = 4
4528
nrows = int(np.ceil(1. * n / ncols))
4629

@@ -53,7 +36,7 @@
5336
# col width
5437
w = X / ncols
5538

56-
for i, (name, color) in enumerate(sorted_colors):
39+
for i, name in enumerate(sorted_names):
5740
col = i % ncols
5841
row = int(i / ncols)
5942
y = Y - (row * h) - h
@@ -68,8 +51,10 @@
6851

6952
# Add extra black line a little bit thicker to make
7053
# clear colors more visible.
71-
ax.hlines(y, xi_line, xf_line, color='black', linewidth=(h * 0.7))
72-
ax.hlines(y + h * 0.1, xi_line, xf_line, color=color, linewidth=(h * 0.6))
54+
ax.hlines(
55+
y, xi_line, xf_line, color='black', linewidth=(h * 0.7))
56+
ax.hlines(
57+
y + h * 0.1, xi_line, xf_line, color=colors[name], linewidth=(h * 0.6))
7358

7459
ax.set_xlim(0, X)
7560
ax.set_ylim(0, Y)

examples/event_handling/lasso_demo.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@
77
usable as is). There will be some refinement of the API.
88
"""
99
from matplotlib.widgets import Lasso
10-
from matplotlib.colors import colorConverter
1110
from matplotlib.collections import RegularPolyCollection
12-
from matplotlib import path
11+
from matplotlib import colors as mcolors, path
1312

1413
import matplotlib.pyplot as plt
1514
from numpy import nonzero
1615
from numpy.random import rand
1716

1817

1918
class Datum(object):
20-
colorin = colorConverter.to_rgba('red')
21-
colorout = colorConverter.to_rgba('blue')
19+
colorin = mcolors.to_rgba("red")
20+
colorout = mcolors.to_rgba("blue")
2221

2322
def __init__(self, x, y, include=False):
2423
self.x = x

examples/mplot3d/polys3d_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
from mpl_toolkits.mplot3d import Axes3D
77
from matplotlib.collections import PolyCollection
8-
from matplotlib.colors import colorConverter
98
import matplotlib.pyplot as plt
9+
from matplotlib import colors as mcolors
1010
import numpy as np
1111

1212

1313
def cc(arg):
1414
'''
1515
Shorthand to convert 'named' colors to rgba format at 60% opacity.
1616
'''
17-
return colorConverter.to_rgba(arg, alpha=0.6)
17+
return mcolors.to_rgba(arg, alpha=0.6)
1818

1919

2020
def polygon_under_graph(xlist, ylist):

examples/pylab_examples/colours.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
Some simple functions to generate colours.
44
"""
55
import numpy as np
6-
from matplotlib.colors import colorConverter
6+
from matplotlib import colors as mcolors
77

88

99
def pastel(colour, weight=2.4):
1010
""" Convert colour into a nice pastel shade"""
11-
rgb = np.asarray(colorConverter.to_rgb(colour))
11+
rgb = np.asarray(mcolors.to_rgba(colour)[:3])
1212
# scale colour
1313
maxc = max(rgb)
1414
if maxc < 1.0 and maxc > 0:

examples/pylab_examples/demo_ribbon_box.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class RibbonBox(object):
1818
nx = original_image.shape[1]
1919

2020
def __init__(self, color):
21-
rgb = matplotlib.colors.colorConverter.to_rgb(color)
21+
rgb = matplotlib.colors.to_rgba(color)[:3]
2222

2323
im = np.empty(self.original_image.shape,
2424
self.original_image.dtype)

examples/pylab_examples/line_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import matplotlib.pyplot as plt
22
from matplotlib.collections import LineCollection
3-
from matplotlib.colors import colorConverter
3+
from matplotlib import colors as mcolors
44

55
import numpy as np
66

@@ -30,7 +30,7 @@
3030
# where onoffseq is an even length tuple of on and off ink in points.
3131
# If linestyle is omitted, 'solid' is used
3232
# See matplotlib.collections.LineCollection for more information
33-
colors = [colorConverter.to_rgba(c)
33+
colors = [mcolors.to_rgba(c)
3434
for c in plt.rcParams['axes.prop_cycle'].by_key()['color']]
3535

3636
line_segments = LineCollection(segs, linewidths=(0.5, 1, 1.5, 2),

0 commit comments

Comments
 (0)