From f696aeb5c23ef8ed527fca1682c02079f135e64d Mon Sep 17 00:00:00 2001 From: lorenzo Date: Wed, 5 Nov 2025 14:52:23 +0100 Subject: [PATCH 1/2] minor channel improvements --- .../ome_zarr_meta/ngio_specs/_channels.py | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/ngio/ome_zarr_meta/ngio_specs/_channels.py b/src/ngio/ome_zarr_meta/ngio_specs/_channels.py index 5360d588..b14cfd88 100644 --- a/src/ngio/ome_zarr_meta/ngio_specs/_channels.py +++ b/src/ngio/ome_zarr_meta/ngio_specs/_channels.py @@ -102,6 +102,20 @@ def valid_hex_color(v: str) -> bool: return True +def into_valid_hex_color(v: str) -> str: + """Convert a string into a valid hexadecimal color. + + If the string is already a valid hexadecimal color, return it. + Otherwise, return a hexadecimal color based on the hash of the string. + """ + # strip leading '#' if present + v = v.lstrip("#") + if valid_hex_color(v): + return v + + return NgioColors.semi_random_pick(v.lower()).value + + class ChannelVisualisation(BaseModel): """Channel visualisation model. @@ -135,13 +149,10 @@ def validate_color(cls, value: str | NgioColors) -> str: """ if value is None: return NgioColors.semi_random_pick().value - if isinstance(value, str) and valid_hex_color(value): - return value + if isinstance(value, str): + return into_valid_hex_color(value) elif isinstance(value, NgioColors): return value.value - elif isinstance(value, str): - value_lower = value.lower() - return NgioColors.semi_random_pick(value_lower).value else: raise NgioValueError(f"Invalid color {value}.") @@ -159,19 +170,6 @@ def check_start_end(cls, data): data["end"] = start + 1 return data - @model_validator(mode="after") - def check_model(self) -> "ChannelVisualisation": - """Check that the start and end values are within the min and max values.""" - if self.start < self.min or self.start > self.max: - raise NgioValidationError( - f"Start value {self.start} is out of range [{self.min}, {self.max}]" - ) - if self.end < self.min or self.end > self.max: - raise NgioValidationError( - f"End value {self.end} is out of range [{self.min}, {self.max}]" - ) - return self - @classmethod def default_init( cls, From 1458d3405f5eb2001b727ba4ef53d4f174edcdea Mon Sep 17 00:00:00 2001 From: lorenzo Date: Wed, 5 Nov 2025 14:54:57 +0100 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cef81dfc..992dd7ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## [v0.4.4] + +### Bug Fixes + +- Fix bug in channel visualization when using hex colors with leading '#'. +- Remove strict range check in channel window. + ## [v0.4.3] ### Bug Fixes