Skip to content

Commit bc8ba2d

Browse files
timtreisclaude
andcommitted
Fix type annotation, docstring, and add tests for literal color support
- Change `color: str | None` to `color: ColorLike | None` in render_labels signature and _validate_label_render_params to match render_shapes/render_points - Update docstring to document that color-like values (named colors, hex strings, RGB(A) tuples) are accepted as literal colors - Add tests for hex string and RGBA tuple color inputs Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2417ef4 commit bc8ba2d

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

src/spatialdata_plot/pl/basic.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ def render_images(
631631
def render_labels(
632632
self,
633633
element: str | None = None,
634-
color: str | None = None,
634+
color: ColorLike | None = None,
635635
*,
636636
groups: list[str] | str | None = None,
637637
contour_px: int | None = 3,
@@ -662,11 +662,13 @@ def render_labels(
662662
element : str | None
663663
The name of the labels element to render. If `None`, all label
664664
elements in the `SpatialData` object will be used and all parameters will be broadcasted if possible.
665-
color : str | None
666-
Can either be string representing a color-like or key in :attr:`sdata.table.obs` or in the index of
667-
:attr:`sdata.table.var`. The latter can be used to color by categorical or continuous variables. If the
668-
color column is found in multiple locations, please provide the table_name to be used for the element if you
669-
would like a specific table to be used. By default one table will automatically be choosen.
665+
color : ColorLike | None
666+
Can either be color-like (name of a color as string, e.g. "red", hex representation, e.g. "#000000" or
667+
"#000000ff", or an RGB(A) array as a tuple or list containing 3-4 floats within [0, 1]. If an alpha value
668+
is indicated, the value of `fill_alpha` takes precedence if given) or a string representing a key in
669+
:attr:`sdata.table.obs` or in the index of :attr:`sdata.table.var`. The latter can be used to color by
670+
categorical or continuous variables. If the color column is found in multiple locations, please provide the
671+
table_name to be used for the element if you would like a specific table to be used.
670672
groups : list[str] | str | None
671673
When using `color` and the key represents discrete labels, `groups` can be used to show only a subset of
672674
them. Other values are set to NA. The list can contain multiple discrete labels to be visualized.

src/spatialdata_plot/pl/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2414,7 +2414,7 @@ def _validate_label_render_params(
24142414
sdata: sd.SpatialData,
24152415
element: str | None,
24162416
cmap: list[Colormap | str] | Colormap | str | None,
2417-
color: str | None,
2417+
color: ColorLike | None,
24182418
fill_alpha: float | int,
24192419
contour_px: int | None,
24202420
groups: list[str] | str | None,
34.6 KB
Loading
34.6 KB
Loading

tests/pl/test_render_labels.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def test_plot_can_stack_render_labels(self, sdata_blobs: SpatialData):
8787
def test_plot_can_color_by_color_name(self, sdata_blobs: SpatialData):
8888
sdata_blobs.pl.render_labels("blobs_labels", color="red").pl.show()
8989

90+
def test_plot_can_color_by_hex(self, sdata_blobs: SpatialData):
91+
sdata_blobs.pl.render_labels("blobs_labels", color="#ff0000").pl.show()
92+
93+
def test_plot_can_color_by_rgba_tuple(self, sdata_blobs: SpatialData):
94+
sdata_blobs.pl.render_labels("blobs_labels", color=(1.0, 0.0, 0.0, 0.5)).pl.show()
95+
9096
def test_plot_can_color_labels_by_continuous_variable(self, sdata_blobs: SpatialData):
9197
sdata_blobs.pl.render_labels("blobs_labels", color="channel_0_sum").pl.show()
9298

0 commit comments

Comments
 (0)