Convert *c* to a (n, 4) array of RGBA colors. Parameters ---------- c : :mpltype:`color` or list of :mpltype:`color` or RGB(A) array If *c* is a masked array, an `~numpy.ndarray` is returned with a (0, 0, 0, 0) row for each masked value or row in *c*. alpha : f
(c, alpha=None)
| 436 | |
| 437 | |
| 438 | def to_rgba_array(c, alpha=None): |
| 439 | """ |
| 440 | Convert *c* to a (n, 4) array of RGBA colors. |
| 441 | |
| 442 | Parameters |
| 443 | ---------- |
| 444 | c : :mpltype:`color` or list of :mpltype:`color` or RGB(A) array |
| 445 | If *c* is a masked array, an `~numpy.ndarray` is returned with a |
| 446 | (0, 0, 0, 0) row for each masked value or row in *c*. |
| 447 | |
| 448 | alpha : float or sequence of floats, optional |
| 449 | If *alpha* is given, force the alpha value of the returned RGBA tuple |
| 450 | to *alpha*. |
| 451 | |
| 452 | If None, the alpha value from *c* is used. If *c* does not have an |
| 453 | alpha channel, then alpha defaults to 1. |
| 454 | |
| 455 | *alpha* is ignored for the color value ``"none"`` (case-insensitive), |
| 456 | which always maps to ``(0, 0, 0, 0)``. |
| 457 | |
| 458 | If *alpha* is a sequence and *c* is a single color, *c* will be |
| 459 | repeated to match the length of *alpha*. |
| 460 | |
| 461 | Returns |
| 462 | ------- |
| 463 | array |
| 464 | (n, 4) array of RGBA colors, where each channel (red, green, blue, |
| 465 | alpha) can assume values between 0 and 1. |
| 466 | """ |
| 467 | if isinstance(c, tuple) and len(c) == 2 and isinstance(c[1], Real): |
| 468 | if alpha is None: |
| 469 | c, alpha = c |
| 470 | else: |
| 471 | c = c[0] |
| 472 | # Special-case inputs that are already arrays, for performance. (If the |
| 473 | # array has the wrong kind or shape, raise the error during one-at-a-time |
| 474 | # conversion.) |
| 475 | if np.iterable(alpha): |
| 476 | alpha = np.asarray(alpha).ravel() |
| 477 | if (isinstance(c, np.ndarray) and c.dtype.kind in "if" |
| 478 | and c.ndim == 2 and c.shape[1] in [3, 4]): |
| 479 | mask = c.mask.any(axis=1) if np.ma.is_masked(c) else None |
| 480 | c = np.ma.getdata(c) |
| 481 | if np.iterable(alpha): |
| 482 | if c.shape[0] == 1 and alpha.shape[0] > 1: |
| 483 | c = np.tile(c, (alpha.shape[0], 1)) |
| 484 | elif c.shape[0] != alpha.shape[0]: |
| 485 | raise ValueError("The number of colors must match the number" |
| 486 | " of alpha values if there are more than one" |
| 487 | " of each.") |
| 488 | if c.shape[1] == 3: |
| 489 | result = np.column_stack([c, np.zeros(len(c))]) |
| 490 | result[:, -1] = alpha if alpha is not None else 1. |
| 491 | elif c.shape[1] == 4: |
| 492 | result = c.copy() |
| 493 | if alpha is not None: |
| 494 | result[:, -1] = alpha |
| 495 | if mask is not None: |
searching dependent graphs…