Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
34 changes: 16 additions & 18 deletions src/ngio/ome_zarr_meta/ngio_specs/_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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}.")

Expand All @@ -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,
Expand Down