|
12 | 12 | from contextlib import suppress |
13 | 13 | from copy import deepcopy |
14 | 14 | from dataclasses import field |
15 | | -from typing import TYPE_CHECKING, cast, overload |
| 15 | +from typing import TYPE_CHECKING |
16 | 16 | from warnings import warn |
17 | 17 |
|
| 18 | +import mizani._colors.utils as color_utils |
18 | 19 | import numpy as np |
19 | 20 | import pandas as pd |
20 | 21 | from pandas.core.groupby import DataFrameGroupBy |
|
26 | 27 | from typing import Any, Callable, Literal, TypeVar |
27 | 28 |
|
28 | 29 | import numpy.typing as npt |
29 | | - from matplotlib.typing import ColorType |
30 | 30 | from typing_extensions import TypeGuard |
31 | 31 |
|
32 | 32 | from plotnine.typing import ( |
33 | 33 | AnyArrayLike, |
34 | | - AnySeries, |
35 | 34 | DataLike, |
36 | 35 | FloatArray, |
37 | 36 | FloatArrayLike, |
|
60 | 59 | "centre": (0.5, 0.5), |
61 | 60 | } |
62 | 61 |
|
| 62 | +to_rgba = color_utils.to_rgba |
| 63 | + |
63 | 64 |
|
64 | 65 | def is_scalar(val): |
65 | 66 | """ |
@@ -530,105 +531,6 @@ def remove_missing( |
530 | 531 | return data |
531 | 532 |
|
532 | 533 |
|
533 | | -@overload |
534 | | -def to_rgba(colors: ColorType, alpha: float) -> ColorType: ... |
535 | | - |
536 | | - |
537 | | -@overload |
538 | | -def to_rgba( |
539 | | - colors: Sequence[ColorType], alpha: float |
540 | | -) -> Sequence[ColorType] | ColorType: ... |
541 | | - |
542 | | - |
543 | | -@overload |
544 | | -def to_rgba( |
545 | | - colors: AnySeries, alpha: AnySeries |
546 | | -) -> Sequence[ColorType] | ColorType: ... |
547 | | - |
548 | | - |
549 | | -def to_rgba( |
550 | | - colors: Sequence[ColorType] | AnySeries | ColorType, |
551 | | - alpha: float | Sequence[float] | AnySeries, |
552 | | -) -> Sequence[ColorType] | ColorType: |
553 | | - """ |
554 | | - Convert hex colors to rgba values. |
555 | | -
|
556 | | - Parameters |
557 | | - ---------- |
558 | | - colors : iterable | str |
559 | | - colors to convert |
560 | | - alphas : iterable | float |
561 | | - alpha values |
562 | | -
|
563 | | - Returns |
564 | | - ------- |
565 | | - out : ndarray | tuple |
566 | | - rgba color(s) |
567 | | -
|
568 | | - Notes |
569 | | - ----- |
570 | | - Matplotlib plotting functions only accept scalar |
571 | | - alpha values. Hence no two objects with different |
572 | | - alpha values may be plotted in one call. This would |
573 | | - make plots with continuous alpha values innefficient. |
574 | | - However :), the colors can be rgba hex values or |
575 | | - list-likes and the alpha dimension will be respected. |
576 | | - """ |
577 | | - |
578 | | - def is_iterable(var): |
579 | | - return np.iterable(var) and not isinstance(var, str) |
580 | | - |
581 | | - def has_alpha(c): |
582 | | - return (isinstance(c, tuple) and len(c) == 4) or ( |
583 | | - isinstance(c, str) and len(c) == 9 and c[0] == "#" |
584 | | - ) |
585 | | - |
586 | | - def no_color(c): |
587 | | - return c is None or c.lower() in ("none", "") |
588 | | - |
589 | | - def to_rgba_hex(c: ColorType, a: float) -> str: |
590 | | - """ |
591 | | - Convert rgb color to rgba hex value |
592 | | -
|
593 | | - If color c has an alpha channel, then alpha value |
594 | | - a is ignored |
595 | | - """ |
596 | | - from matplotlib.colors import colorConverter, to_hex |
597 | | - |
598 | | - if c in ("None", "none"): |
599 | | - return c |
600 | | - |
601 | | - _has_alpha = has_alpha(c) |
602 | | - c = to_hex(c, keep_alpha=_has_alpha) |
603 | | - |
604 | | - if not _has_alpha: |
605 | | - arr = colorConverter.to_rgba(c, a) |
606 | | - return to_hex(arr, keep_alpha=True) |
607 | | - |
608 | | - return c |
609 | | - |
610 | | - if is_iterable(colors): |
611 | | - colors = cast("Sequence[ColorType]", colors) |
612 | | - |
613 | | - if all(no_color(c) for c in colors): |
614 | | - return "none" |
615 | | - |
616 | | - if isinstance(alpha, (Sequence, pd.Series)): |
617 | | - return [to_rgba_hex(c, a) for c, a in zip(colors, alpha)] |
618 | | - else: |
619 | | - return [to_rgba_hex(c, alpha) for c in colors] |
620 | | - else: |
621 | | - colors = cast("ColorType", colors) |
622 | | - |
623 | | - if no_color(colors): |
624 | | - return colors |
625 | | - |
626 | | - if isinstance(alpha, (Sequence, pd.Series)): |
627 | | - return [to_rgba_hex(colors, a) for a in alpha] |
628 | | - else: |
629 | | - return to_rgba_hex(colors, alpha) |
630 | | - |
631 | | - |
632 | 534 | def groupby_apply( |
633 | 535 | df: pd.DataFrame, |
634 | 536 | cols: str | list[str], |
|
0 commit comments