Convert *c* to an RGBA color. Parameters ---------- c : :mpltype:`color` or ``np.ma.masked`` alpha : float, optional If *alpha* is given, force the alpha value of the returned RGBA tuple to *alpha*. If None, the alpha value from *c* is used. If *c* doe
(c, alpha=None)
| 304 | |
| 305 | |
| 306 | def to_rgba(c, alpha=None): |
| 307 | """ |
| 308 | Convert *c* to an RGBA color. |
| 309 | |
| 310 | Parameters |
| 311 | ---------- |
| 312 | c : :mpltype:`color` or ``np.ma.masked`` |
| 313 | |
| 314 | alpha : float, optional |
| 315 | If *alpha* is given, force the alpha value of the returned RGBA tuple |
| 316 | to *alpha*. |
| 317 | |
| 318 | If None, the alpha value from *c* is used. If *c* does not have an |
| 319 | alpha channel, then alpha defaults to 1. |
| 320 | |
| 321 | *alpha* is ignored for the color value ``"none"`` (case-insensitive), |
| 322 | which always maps to ``(0, 0, 0, 0)``. |
| 323 | |
| 324 | Returns |
| 325 | ------- |
| 326 | tuple |
| 327 | Tuple of floats ``(r, g, b, a)``, where each channel (red, green, blue, |
| 328 | alpha) can assume values between 0 and 1. |
| 329 | """ |
| 330 | if isinstance(c, tuple) and len(c) == 2: |
| 331 | if alpha is None: |
| 332 | c, alpha = c |
| 333 | else: |
| 334 | c = c[0] |
| 335 | # Special-case nth color syntax because it should not be cached. |
| 336 | if _is_nth_color(c): |
| 337 | prop_cycler = mpl.rcParams['axes.prop_cycle'] |
| 338 | colors = prop_cycler.by_key().get('color', ['k']) |
| 339 | c = colors[int(c[1:]) % len(colors)] |
| 340 | try: |
| 341 | rgba = _colors_full_map.cache[c, alpha] |
| 342 | except (KeyError, TypeError): # Not in cache, or unhashable. |
| 343 | rgba = None |
| 344 | if rgba is None: # Suppress exception chaining of cache lookup failure. |
| 345 | rgba = _to_rgba_no_colorcycle(c, alpha) |
| 346 | try: |
| 347 | _colors_full_map.cache[c, alpha] = rgba |
| 348 | except TypeError: |
| 349 | pass |
| 350 | return rgba |
| 351 | |
| 352 | |
| 353 | def _to_rgba_no_colorcycle(c, alpha=None): |
no test coverage detected
searching dependent graphs…