diff --git a/bec_widgets/utils/colors.py b/bec_widgets/utils/colors.py index 488c8886..31c4eb4f 100644 --- a/bec_widgets/utils/colors.py +++ b/bec_widgets/utils/colors.py @@ -250,7 +250,7 @@ class Colors: if not 0 <= value <= 255: raise PydanticCustomError( "unsupported color", - f"The color values must be between 0 and 255. Provide color {color}.", + f"The color values must be between 0 and 255 in RGBA format (R,G,B,A)", {"wrong_value": color}, ) return color diff --git a/tests/unit_tests/test_color_validation.py b/tests/unit_tests/test_color_validation.py new file mode 100644 index 00000000..4766b8ac --- /dev/null +++ b/tests/unit_tests/test_color_validation.py @@ -0,0 +1,60 @@ +import pytest +from pydantic import ValidationError + +from bec_widgets.utils import Colors +from bec_widgets.widgets.figure.plots.waveform.waveform_curve import CurveConfig + + +def test_color_validation_CSS(): + # Test valid color + color = Colors.validate_color("teal") + assert color == "teal" + + # Test invalid color + with pytest.raises(ValidationError) as excinfo: + CurveConfig(color="invalid_color") + + errors = excinfo.value.errors() + assert len(errors) == 1 + assert errors[0]["type"] == ("unsupported color") + assert "The color must be a valid HEX string or CSS Color." in str(excinfo.value) + + +def test_color_validation_hex(): + # Test valid color + color = Colors.validate_color("#ff0000") + assert color == "#ff0000" + + # Test invalid color + with pytest.raises(ValidationError) as excinfo: + CurveConfig(color="#ff00000") + + errors = excinfo.value.errors() + assert len(errors) == 1 + assert errors[0]["type"] == ("unsupported color") + assert "The color must be a valid HEX string or CSS Color." in str(excinfo.value) + + +def test_color_validation_RGBA(): + # Test valid color + color = Colors.validate_color((255, 0, 0, 255)) + assert color == (255, 0, 0, 255) + + # Test invalid color + with pytest.raises(ValidationError) as excinfo: + CurveConfig(color=(255, 0, 0)) + + errors = excinfo.value.errors() + assert len(errors) == 1 + assert errors[0]["type"] == ("unsupported color") + assert "The color must be a tuple of 4 elements (R, G, B, A)." in str(excinfo.value) + + with pytest.raises(ValidationError) as excinfo: + CurveConfig(color=(255, 0, 0, 355)) + + errors = excinfo.value.errors() + assert len(errors) == 1 + assert errors[0]["type"] == ("unsupported color") + assert "The color values must be between 0 and 255 in RGBA format (R,G,B,A)" in str( + excinfo.value + )