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

Skip to content

Commit 05a6294

Browse files
author
Joel B. Mohler
committed
optimize rgba color parsing by pre-parsing before draw
1 parent 047c60a commit 05a6294

File tree

6 files changed

+34
-25
lines changed

6 files changed

+34
-25
lines changed

lib/matplotlib/backend_bases.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ def __init__(self):
773773

774774
def copy_properties(self, gc):
775775
'Copy properties from gc to self'
776+
print(gc._alpha)
776777
self._alpha = gc._alpha
777778
self._forced_alpha = gc._forced_alpha
778779
self._antialiased = gc._antialiased
@@ -784,6 +785,8 @@ def copy_properties(self, gc):
784785
self._linestyle = gc._linestyle
785786
self._linewidth = gc._linewidth
786787
self._rgb = gc._rgb
788+
if not isinstance(gc._orig_color, tuple):
789+
raise Thing()
787790
self._orig_color = gc._orig_color
788791
self._hatch = gc._hatch
789792
self._url = gc._url
@@ -918,7 +921,7 @@ def set_alpha(self, alpha):
918921
else:
919922
self._alpha = 1.0
920923
self._forced_alpha = False
921-
self.set_foreground(self._orig_color)
924+
self.set_foreground(self._orig_color, isRGBA=True)
922925

923926
def set_antialiased(self, b):
924927
"""
@@ -980,20 +983,22 @@ def set_foreground(self, fg, isRGBA=False):
980983
981984
If you know fg is rgba, set ``isRGBA=True`` for efficiency.
982985
"""
983-
self._orig_color = fg
984-
if self._forced_alpha:
986+
if self._forced_alpha and isRGBA:
987+
self._rgb = tuple(fg[:3] + (self._alpha,))
988+
elif self._forced_alpha:
985989
self._rgb = colors.colorConverter.to_rgba(fg, self._alpha)
986990
elif isRGBA:
987991
self._rgb = fg
988992
else:
989993
self._rgb = colors.colorConverter.to_rgba(fg)
994+
self._orig_color = self._rgb
990995

991996
def set_graylevel(self, frac):
992997
"""
993998
Set the foreground color to be a gray level with *frac*
994999
"""
995-
self._orig_color = frac
9961000
self._rgb = (frac, frac, frac, self._alpha)
1001+
self._orig_color = self._rgb
9971002

9981003
def set_joinstyle(self, js):
9991004
"""

lib/matplotlib/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def draw(self, renderer):
280280
self._linestyles == [(None, None)] and
281281
len(self._antialiaseds) == 1 and len(self._urls) == 1 and
282282
self.get_hatch() is None):
283-
gc.set_foreground(tuple(edgecolors[0]))
283+
gc.set_foreground(tuple(edgecolors[0]), isRGBA=True)
284284
gc.set_linewidth(self._linewidths[0])
285285
gc.set_linestyle(self._linestyles[0])
286286
gc.set_antialiased(self._antialiaseds[0])

lib/matplotlib/colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,11 @@ def to_rgb(self, arg):
330330
# for the user to figure out as-is.
331331
return color
332332

333+
def override_alpha(self, rgba, alpha):
334+
if alpha == None:
335+
return rgba
336+
return tuple(rgba[:3] + (alpha,))
337+
333338
def to_rgba(self, arg, alpha=None):
334339
"""
335340
Returns an *RGBA* tuple of four floats from 0-1.

lib/matplotlib/lines.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ def draw(self, renderer):
715715
gc.set_linewidth(0)
716716
gc.set_foreground(rgbaFace, isRGBA=True)
717717
else:
718-
gc.set_foreground(edgecolor)
718+
gc.set_foreground(edgecolor, isRGBA=True)
719719
gc.set_linewidth(self._markeredgewidth)
720720

721721
marker = self._marker
@@ -790,7 +790,7 @@ def get_markeredgecolor(self):
790790
if self._marker.get_marker() in ('.', ','):
791791
return self._color
792792
if self._marker.is_filled() and self.get_fillstyle() != 'none':
793-
return 'k' # Bad hard-wired default...
793+
return colorConverter.to_rgba('k') # Bad hard-wired default...
794794
else:
795795
return self._color
796796
else:
@@ -887,7 +887,7 @@ def set_color(self, color):
887887
888888
ACCEPTS: any matplotlib color
889889
"""
890-
self._color = color
890+
self._color = colorConverter.to_rgba(color)
891891

892892
def set_drawstyle(self, drawstyle):
893893
"""
@@ -1001,9 +1001,10 @@ def set_markerfacecolor(self, fc):
10011001
ACCEPTS: any matplotlib color
10021002
"""
10031003
if fc is None:
1004-
fc = 'auto'
1004+
self._markerfacecolor = 'auto'
1005+
return
10051006

1006-
self._markerfacecolor = fc
1007+
self._markerfacecolor = colorConverter.to_rgba(fc)
10071008

10081009
def set_markerfacecoloralt(self, fc):
10091010
"""
@@ -1012,9 +1013,10 @@ def set_markerfacecoloralt(self, fc):
10121013
ACCEPTS: any matplotlib color
10131014
"""
10141015
if fc is None:
1015-
fc = 'auto'
1016+
self._markerfacecoloralt = 'auto'
1017+
return
10161018

1017-
self._markerfacecoloralt = fc
1019+
self._markerfacecoloralt = colorConverter.to_rgba(fc)
10181020

10191021
def set_markersize(self, sz):
10201022
"""
@@ -1149,11 +1151,11 @@ def _get_rgba_face(self, alt=False):
11491151
if is_string_like(facecolor) and facecolor.lower() == 'none':
11501152
rgbaFace = None
11511153
else:
1152-
rgbaFace = colorConverter.to_rgba(facecolor, self._alpha)
1154+
rgbaFace = colorConverter.override_alpha(facecolor, self._alpha)
11531155
return rgbaFace
11541156

11551157
def _get_rgba_ln_color(self, alt=False):
1156-
return colorConverter.to_rgba(self._color, self._alpha)
1158+
return colorConverter.override_alpha(self._color, self._alpha)
11571159

11581160
# some aliases....
11591161
def set_aa(self, val):

lib/matplotlib/text.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from matplotlib.artist import Artist
1919
from matplotlib.cbook import is_string_like, maxdict
2020
from matplotlib import docstring
21+
from matplotlib import colors
2122
from matplotlib.font_manager import FontProperties
2223
from matplotlib.patches import bbox_artist, YAArrow, FancyBboxPatch
2324
from matplotlib.patches import FancyArrowPatch, Rectangle
@@ -551,7 +552,7 @@ def draw(self, renderer):
551552
self._draw_bbox(renderer, posx, posy)
552553

553554
gc = renderer.new_gc()
554-
gc.set_foreground(self.get_color())
555+
gc.set_foreground(self.get_color(), isRGBA=True)
555556
gc.set_alpha(self.get_alpha())
556557
gc.set_url(self._url)
557558
self._set_gc_clip(gc)
@@ -768,12 +769,7 @@ def set_color(self, color):
768769
769770
ACCEPTS: any matplotlib color
770771
"""
771-
# Make sure it is hashable, or get_prop_tup will fail.
772-
try:
773-
hash(color)
774-
except TypeError:
775-
color = tuple(color)
776-
self._color = color
772+
self._color = colors.colorConverter.to_rgba(color)
777773

778774
def set_ha(self, align):
779775
'alias for set_horizontalalignment'

lib/matplotlib/transforms.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,8 +1371,9 @@ def transform_path_non_affine(self, path):
13711371
``transform_path(path)`` is equivalent to
13721372
``transform_path_affine(transform_path_non_affine(values))``.
13731373
"""
1374-
return Path(self.transform_non_affine(path.vertices), path.codes,
1375-
path._interpolation_steps)
1374+
x = self.transform_non_affine(path.vertices)
1375+
return Path._fast_from_codes_and_verts(x, path.codes,
1376+
{'interpolation_steps': path._interpolation_steps})
13761377

13771378
def transform_angles(self, angles, pts, radians=False, pushoff=1e-5):
13781379
"""
@@ -2578,8 +2579,8 @@ def _revalidate(self):
25782579
self._transformed_path = \
25792580
self._transform.transform_path_non_affine(self._path)
25802581
self._transformed_points = \
2581-
Path(self._transform.transform_non_affine(self._path.vertices),
2582-
None, self._path._interpolation_steps)
2582+
Path._fast_from_codes_and_verts(self._transform.transform_non_affine(self._path.vertices),
2583+
None, {'interpolation_steps': self._path._interpolation_steps})
25832584
self._invalid = 0
25842585

25852586
def get_transformed_points_and_affine(self):

0 commit comments

Comments
 (0)