Revision: 8628
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=8628&view=rev
Author:   efiring
Date:     2010-08-14 21:21:58 +0000 (Sat, 14 Aug 2010)

Log Message:
-----------
fix bugs: patch alpha handling, bar color kwarg interpretation
This changeset is somewhat intrusive, with side-effects of moving
most patch color handling out of the draw method, and of changing
rgb2hex to allow rgba. This simplifies backend_svg slightly.

Modified Paths:
--------------
    branches/v1_0_maint/CHANGELOG
    branches/v1_0_maint/lib/matplotlib/axes.py
    branches/v1_0_maint/lib/matplotlib/backends/backend_svg.py
    branches/v1_0_maint/lib/matplotlib/colors.py
    branches/v1_0_maint/lib/matplotlib/patches.py

Modified: branches/v1_0_maint/CHANGELOG
===================================================================
--- branches/v1_0_maint/CHANGELOG       2010-08-13 18:23:33 UTC (rev 8627)
+++ branches/v1_0_maint/CHANGELOG       2010-08-14 21:21:58 UTC (rev 8628)
@@ -1,3 +1,5 @@
+2010-08-14 Fix bug in patch alpha handling, and in bar color kwarg - EF
+
 2010-07-20 Return Qt4's default cursor when leaving the canvas - DSD
 
 2010-07-06 Tagging for mpl 1.0 at r8502

Modified: branches/v1_0_maint/lib/matplotlib/axes.py
===================================================================
--- branches/v1_0_maint/lib/matplotlib/axes.py  2010-08-13 18:23:33 UTC (rev 
8627)
+++ branches/v1_0_maint/lib/matplotlib/axes.py  2010-08-14 21:21:58 UTC (rev 
8628)
@@ -4579,6 +4579,8 @@
             color = [None] * nbars
         else:
             color = list(mcolors.colorConverter.to_rgba_array(color))
+            if len(color) == 0:     # until to_rgba_array is changed
+                color = [[0,0,0,0]]
             if len(color) < nbars:
                 color *= nbars
 
@@ -4586,6 +4588,8 @@
             edgecolor = [None] * nbars
         else:
             edgecolor = list(mcolors.colorConverter.to_rgba_array(edgecolor))
+            if len(edgecolor) == 0:     # until to_rgba_array is changed
+                edgecolor = [[0,0,0,0]]
             if len(edgecolor) < nbars:
                 edgecolor *= nbars
 

Modified: branches/v1_0_maint/lib/matplotlib/backends/backend_svg.py
===================================================================
--- branches/v1_0_maint/lib/matplotlib/backends/backend_svg.py  2010-08-13 
18:23:33 UTC (rev 8627)
+++ branches/v1_0_maint/lib/matplotlib/backends/backend_svg.py  2010-08-14 
21:21:58 UTC (rev 8628)
@@ -118,7 +118,7 @@
                 '<rect x="0" y="0" width="%d" height="%d" fill="%s"/>' %
                 (HATCH_SIZE+1, HATCH_SIZE+1, fill))
             path = '<path d="%s" fill="%s" stroke="%s" stroke-width="1.0"/>' % 
(
-                path_data, rgb2hex(gc.get_rgb()[:3]), 
rgb2hex(gc.get_rgb()[:3]))
+                path_data, rgb2hex(gc.get_rgb()), rgb2hex(gc.get_rgb()))
             self._svgwriter.write(path)
             self._svgwriter.write('\n  </pattern>\n</defs>')
             self._hatchd[dictkey] = id
@@ -135,7 +135,7 @@
             if rgbFace is None:
                 fill = 'none'
             else:
-                fill = rgb2hex(rgbFace[:3])
+                fill = rgb2hex(rgbFace)
 
         offset, seq = gc.get_dashes()
         if seq is None:
@@ -149,7 +149,7 @@
             return 'fill: %s; stroke: %s; stroke-width: %f; ' \
                 'stroke-linejoin: %s; stroke-linecap: %s; %s opacity: %f' % (
                          fill,
-                         rgb2hex(gc.get_rgb()[:3]),
+                         rgb2hex(gc.get_rgb()),
                          linewidth,
                          gc.get_joinstyle(),
                          _capstyle_d[gc.get_capstyle()],
@@ -469,7 +469,7 @@
         glyph_map=self._glyph_map
 
         text2path = self._text2path
-        color = rgb2hex(gc.get_rgb()[:3])
+        color = rgb2hex(gc.get_rgb())
         fontsize = prop.get_size_in_points()
 
         write = self._svgwriter.write
@@ -592,7 +592,7 @@
         y -= font.get_descent() / 64.0
 
         fontsize = prop.get_size_in_points()
-        color = rgb2hex(gc.get_rgb()[:3])
+        color = rgb2hex(gc.get_rgb())
         write = self._svgwriter.write
 
         if rcParams['svg.embed_char_paths']:
@@ -730,7 +730,7 @@
             self.mathtext_parser.parse(s, 72, prop)
         svg_glyphs = svg_elements.svg_glyphs
         svg_rects = svg_elements.svg_rects
-        color = rgb2hex(gc.get_rgb()[:3])
+        color = rgb2hex(gc.get_rgb())
         write = self._svgwriter.write
 
         style = "fill: %s" % color

Modified: branches/v1_0_maint/lib/matplotlib/colors.py
===================================================================
--- branches/v1_0_maint/lib/matplotlib/colors.py        2010-08-13 18:23:33 UTC 
(rev 8627)
+++ branches/v1_0_maint/lib/matplotlib/colors.py        2010-08-14 21:21:58 UTC 
(rev 8628)
@@ -217,8 +217,8 @@
 
 
 def rgb2hex(rgb):
-    'Given a len 3 rgb tuple of 0-1 floats, return the hex string'
-    return '#%02x%02x%02x' % tuple([round(val*255) for val in rgb])
+    'Given an rgb or rgba sequence of 0-1 floats, return the hex string'
+    return '#%02x%02x%02x' % tuple([round(val*255) for val in rgb[:3]])
 
 hexColorPattern = re.compile("\A#[a-fA-F0-9]{6}\Z")
 

Modified: branches/v1_0_maint/lib/matplotlib/patches.py
===================================================================
--- branches/v1_0_maint/lib/matplotlib/patches.py       2010-08-13 18:23:33 UTC 
(rev 8627)
+++ branches/v1_0_maint/lib/matplotlib/patches.py       2010-08-14 21:21:58 UTC 
(rev 8628)
@@ -55,6 +55,7 @@
     def __init__(self,
                  edgecolor=None,
                  facecolor=None,
+                 color=None,
                  linewidth=None,
                  linestyle=None,
                  antialiased = None,
@@ -74,20 +75,22 @@
         if linestyle is None: linestyle = "solid"
         if antialiased is None: antialiased = mpl.rcParams['patch.antialiased']
 
-        if 'color' in kwargs:
+        self._fill = True # needed for set_facecolor call
+        if color is not None:
             if (edgecolor is not None or
                 facecolor is not None):
                 import warnings
                 warnings.warn("Setting the 'color' property will override"
                               "the edgecolor or facecolor properties. ")
-
-        self.set_edgecolor(edgecolor)
-        self.set_facecolor(facecolor)
+            self.set_color(color)
+        else:
+            self.set_edgecolor(edgecolor)
+            self.set_facecolor(facecolor)
         self.set_linewidth(linewidth)
         self.set_linestyle(linestyle)
         self.set_antialiased(antialiased)
         self.set_hatch(hatch)
-        self.fill = fill
+        self.set_fill(fill)
         self._combined_transform = transforms.IdentityTransform()
 
         self.set_path_effects(path_effects)
@@ -98,7 +101,7 @@
         """
         Return a copy of the vertices used in this patch
 
-        If the patch contains Bézier curves, the curves will be
+        If the patch contains Bezier curves, the curves will be
         interpolated by line segments.  To access the curves as
         curves, use :meth:`get_path`.
         """
@@ -223,7 +226,7 @@
         ACCEPTS: mpl color spec, or None for default, or 'none' for no color
         """
         if color is None: color = mpl.rcParams['patch.edgecolor']
-        self._edgecolor = color
+        self._edgecolor = colors.colorConverter.to_rgba(color, self._alpha)
 
     def set_ec(self, color):
         """alias for set_edgecolor"""
@@ -236,7 +239,12 @@
         ACCEPTS: mpl color spec, or None for default, or 'none' for no color
         """
         if color is None: color = mpl.rcParams['patch.facecolor']
-        self._facecolor = color
+        self._original_facecolor = color # save: otherwise changing _fill
+                                         # may lose alpha information
+        self._facecolor = colors.colorConverter.to_rgba(color, self._alpha)
+        if not self._fill:
+            self._facecolor = list(self._facecolor)
+            self._facecolor[3] = 0
 
     def set_fc(self, color):
         """alias for set_facecolor"""
@@ -256,7 +264,22 @@
         self.set_facecolor(c)
         self.set_edgecolor(c)
 
+    def set_alpha(self, alpha):
+        """
+        Set the alpha tranparency of the patch.
 
+        ACCEPTS: float or None
+        """
+        if alpha is not None:
+            try:
+                float(alpha)
+            except TypeError:
+                raise TypeError('alpha must be a float or None')
+        artist.Artist.set_alpha(self, alpha)
+        self.set_facecolor(self._original_facecolor) # using self._fill and 
self._alpha
+        self._edgecolor = colors.colorConverter.to_rgba(
+                                        self._edgecolor[:3], self._alpha)
+
     def set_linewidth(self, w):
         """
         Set the patch linewidth in points
@@ -289,11 +312,12 @@
 
         ACCEPTS: [True | False]
         """
-        self.fill = b
+        self._fill = b
+        self.set_facecolor(self._original_facecolor)
 
     def get_fill(self):
         'return whether fill is set'
-        return self.fill
+        return self._fill
 
     def set_hatch(self, hatch):
         """
@@ -345,29 +369,25 @@
         renderer.open_group('patch', self.get_gid())
         gc = renderer.new_gc()
 
-        if cbook.is_string_like(self._edgecolor) and 
self._edgecolor.lower()=='none':
-            gc.set_linewidth(0)
-        else:
-            gc.set_foreground(self._edgecolor)
-            gc.set_linewidth(self._linewidth)
-            gc.set_linestyle(self._linestyle)
+        gc.set_alpha(self._edgecolor[3])
+        gc.set_foreground(self._edgecolor, isRGB=True)
 
+        lw = self._linewidth
+        if self._edgecolor[3] == 0:
+            lw = 0
+        gc.set_linewidth(lw)
+        gc.set_linestyle(self._linestyle)
+
         gc.set_antialiased(self._antialiased)
         self._set_gc_clip(gc)
         gc.set_capstyle('projecting')
         gc.set_url(self._url)
         gc.set_snap(self.get_snap())
 
-        if (not self.fill or self._facecolor is None or
-            (cbook.is_string_like(self._facecolor) and 
self._facecolor.lower()=='none')):
-            rgbFace = None
-            gc.set_alpha(1.0)
-        else:
-            r, g, b, a = colors.colorConverter.to_rgba(self._facecolor, 
self._alpha)
-            rgbFace = (r, g, b)
-            gc.set_alpha(a)
+        rgbFace = self._facecolor
+        if rgbFace[3] == 0:
+            rgbFace = None # (some?) renderers expect this as no-fill signal
 
-
         if self._hatch:
             gc.set_hatch(self._hatch )
 
@@ -3823,7 +3843,7 @@
                                                 )
 
         #if not fillable:
-        #    self.fill = False
+        #    self._fill = False
 
         return _path, fillable
 
@@ -3831,32 +3851,28 @@
 
     def draw(self, renderer):
         if not self.get_visible(): return
-        #renderer.open_group('patch')
+
+        renderer.open_group('patch', self.get_gid())
         gc = renderer.new_gc()
 
+        gc.set_alpha(self._edgecolor[3])
+        gc.set_foreground(self._edgecolor, isRGB=True)
 
-        if cbook.is_string_like(self._edgecolor) and 
self._edgecolor.lower()=='none':
-            gc.set_linewidth(0)
-        else:
-            gc.set_foreground(self._edgecolor)
-            gc.set_linewidth(self._linewidth)
-            gc.set_linestyle(self._linestyle)
+        lw = self._linewidth
+        if self._edgecolor[3] == 0:
+            lw = 0
+        gc.set_linewidth(lw)
+        gc.set_linestyle(self._linestyle)
 
         gc.set_antialiased(self._antialiased)
         self._set_gc_clip(gc)
         gc.set_capstyle('round')
         gc.set_snap(self.get_snap())
 
-        if (not self.fill or self._facecolor is None or
-            (cbook.is_string_like(self._facecolor) and 
self._facecolor.lower()=='none')):
-            rgbFace = None
-            gc.set_alpha(1.0)
-        else:
-            r, g, b, a = colors.colorConverter.to_rgba(self._facecolor, 
self._alpha)
-            rgbFace = (r, g, b)
-            gc.set_alpha(a)
+        rgbFace = self._facecolor
+        if rgbFace[3] == 0:
+            rgbFace = None # (some?) renderers expect this as no-fill signal
 
-
         if self._hatch:
             gc.set_hatch(self._hatch )
 
@@ -3870,7 +3886,6 @@
 
         affine = transforms.IdentityTransform()
 
-        renderer.open_group('patch', self.get_gid())
 
         if self.get_path_effects():
             for path_effect in self.get_path_effects():


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
This SF.net email is sponsored by 

Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev 
_______________________________________________
Matplotlib-checkins mailing list
Matplotlib-checkins@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to