@@ -126,7 +126,7 @@ def is_color_like(c):
126
126
def to_rgba (c , alpha = None ):
127
127
"""Convert `c` to an RGBA color.
128
128
129
- `alpha` provides a default alpha value.
129
+ If `alpha` is not `None`, it forces the alpha value.
130
130
"""
131
131
# Special-case nth color syntax because it should not be cached.
132
132
if _is_nth_color (c ):
@@ -152,7 +152,7 @@ def to_rgba(c, alpha=None):
152
152
def _to_rgba_no_colorcycle (c , alpha = None ):
153
153
"""Convert `c` to an RGBA color, with no support for color-cycle syntax.
154
154
155
- `alpha` provides a default alpha value.
155
+ If `alpha` is not `None`, it forces the alpha value.
156
156
"""
157
157
orig_c = c
158
158
if isinstance (c , six .string_types ) and c .lower () == "none" :
@@ -187,28 +187,27 @@ def _to_rgba_no_colorcycle(c, alpha=None):
187
187
pass
188
188
raise ValueError ("Invalid RGBA argument: {!r}" .format (orig_c ))
189
189
# tuple color.
190
+ # Python 2.7 / numpy 1.6 apparently require this to return builtin floats,
191
+ # not numpy floats.
190
192
try :
191
- c = np . array ( c , float )
193
+ c = tuple ( map ( float , c ) )
192
194
except TypeError :
193
195
raise ValueError ("Invalid RGBA argument: {!r}" .format (orig_c ))
194
- if c .ndim != 1 :
195
- raise ValueError ("Invalid RGBA argument: {!r}" .format (orig_c ))
196
- if len (c ) == 3 :
197
- c = np .append (c , alpha if alpha is not None else 1. )
198
- if len (c ) == 4 :
199
- if np .any ((c < 0 ) | (c > 1 )):
200
- raise ValueError ("RGBA values should be within 0-1 range" )
201
- if alpha is not None :
202
- c [- 1 ] = alpha
203
- return tuple (c )
204
- else :
196
+ if len (c ) not in [3 , 4 ]:
205
197
raise ValueError ("RGBA sequence should have length 3 or 4" )
198
+ if len (c ) == 3 and alpha is None :
199
+ alpha = 1
200
+ if alpha is not None :
201
+ c = c [:3 ] + (alpha ,)
202
+ if any (elem < 0 or elem > 1 for elem in c ):
203
+ raise ValueError ("RGBA values should be within 0-1 range" )
204
+ return c
206
205
207
206
208
207
def to_rgba_array (c , alpha = None ):
209
208
"""Convert `c` to a (n, 4) array of RGBA colors.
210
209
211
- `alpha` provides a default alpha value. If `c` is "none"
210
+ If `alpha` is not `None`, it forces the alpha value. If `c` is "none"
212
211
(case-insensitive) or an empty list, an empty array is returned.
213
212
"""
214
213
# Single value?
@@ -243,7 +242,7 @@ def to_rgba_array(c, alpha=None):
243
242
def to_hex (c , alpha = None ):
244
243
"""Convert `c` to a hex color.
245
244
246
- `alpha` provides a default alpha value.
245
+ If `alpha` is not `None`, it forces the alpha value.
247
246
"""
248
247
return "#" + "" .join (format (int (np .round (val * 255 )), "02x" )
249
248
for val in to_rgba (c , alpha = alpha ))
0 commit comments