Skip to content

Commit d48de0d

Browse files
author
Sonja Stockhaus
committed
Color dataclass update
1 parent 1254107 commit d48de0d

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

src/spatialdata_plot/pl/utils.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,7 @@ def _type_check_params(param_dict: dict[str, Any], element_type: str) -> dict[st
16701670
if _is_color_like(color):
16711671
logger.info("Value for parameter 'color' appears to be a color, using it as such.")
16721672
param_dict["col_for_color"] = None
1673+
# TODO: create Color object?
16731674
else:
16741675
param_dict["col_for_color"] = color
16751676
param_dict["color"] = None
@@ -2602,13 +2603,25 @@ def __post__init__(self) -> None:
26022603
if self.alpha is None:
26032604
self.alpha = self.color[7:]
26042605
self.color = self.color[:7]
2605-
# named color
26062606
else:
2607+
try:
2608+
float(self.color)
2609+
except ValueError:
2610+
# we're not dealing with what matplotlib considers greyscale
2611+
pass
2612+
else:
2613+
raise TypeError(
2614+
f"Invalid type `{type(self.color)}` for a color, expecting str | None | tuple[float, ...] | "
2615+
"list[float]. Note that unlike in matplotlib, giving a string of a number within [0, 1] as a "
2616+
"greyscale value is not supported here!"
2617+
)
26072618
# matplotlib raises ValueError in case of invalid color name
26082619
self.color = colors.to_hex(self.color, keep_alpha=False)
26092620
elif isinstance(self.color, list[float] | tuple[float, ...]):
26102621
if len(self.color) < 3:
2611-
raise ValueError(f"Color `{self.color}` can't be interpreted as RGB(A) array, needs at least 3 values.")
2622+
raise ValueError(f"Color `{self.color}` can't be interpreted as RGB(A) array, needs 3 or 4 values!")
2623+
if len(self.color) > 4:
2624+
raise ValueError(f"Color `{self.color}` can't be interpreted as RGB(A) array, needs 3 or 4 values!")
26122625
# get first 3-4 values
26132626
r, g, b = self.color[0], self.color[1], self.color[2]
26142627
a = 1.0 if len(self.color) == 3 else self.color[3]
@@ -2617,12 +2630,6 @@ def __post__init__(self) -> None:
26172630
self.color = colors.rgb2hex((r, g, b, a), keep_alpha=False)
26182631
if self.alpha is None:
26192632
self.alpha = colors.rgb2hex((r, g, b, a), keep_alpha=True)[7:]
2620-
elif isinstance(self.color, float | int):
2621-
raise TypeError(
2622-
f"Invalid type `{type(self.color)}` for a color, expecting str | None | tuple[float, ...] | "
2623-
"list[float]. Note that unlike in matplotlib, giving a float within [0, 1] as a greyscale value is not "
2624-
"supported here!"
2625-
)
26262633
else:
26272634
raise TypeError(
26282635
f"Invalid type `{type(self.color)}` for color, expecting str | None | tuple[float, ...] | list[float]."

0 commit comments

Comments
 (0)