diff --git a/scripts/utils/theme/color_replacement_generator.py b/scripts/utils/theme/color_replacement_generator.py index 904467b..0fd7183 100644 --- a/scripts/utils/theme/color_replacement_generator.py +++ b/scripts/utils/theme/color_replacement_generator.py @@ -23,8 +23,7 @@ class ColorReplacementGenerator: lightness = int(color_def["l"]) / 100 saturation = int(color_def["s"]) / 100 - if theme_color.saturation is not None: - saturation *= theme_color.saturation / 100 + saturation = self._adjust_saturation(saturation, theme_color) alpha = color_def["a"] red, green, blue = self.color_converter.hsl_to_rgb( @@ -33,6 +32,15 @@ class ColorReplacementGenerator: return f"rgba({red}, {green}, {blue}, {alpha})" + @staticmethod + def _adjust_saturation(base_saturation: float, theme_color: InstallationColor) -> float: + """Adjust saturation based on the theme color""" + if theme_color.saturation is None: + return base_saturation + + adjusted = base_saturation * (theme_color.saturation / 100) + return min(adjusted, 1.0) + def _get_color_definition(self, element: str, mode: str) -> dict: """Get color definition for element, handling defaults if needed""" replacer = self.colors.replacers[element] diff --git a/tests/utils/theme/assets/colors.json b/tests/utils/theme/assets/colors.json index 42fbe85..bd8d79a 100644 --- a/tests/utils/theme/assets/colors.json +++ b/tests/utils/theme/assets/colors.json @@ -45,6 +45,11 @@ "0,0": { "light": "rgba(171, 171, 171, 1)", "dark": "rgba(66, 66, 66, 1)" + }, + "0,999": { + "_": "hsl should be 100 but not over 100", + "light": "rgba(255, 87, 87, 1)", + "dark": "rgba(133, 0, 0, 1)" } }, "ACCENT-COLOR": { @@ -55,6 +60,10 @@ "0,0": { "light": "rgba(171, 171, 171, 1)", "dark": "rgba(66, 66, 66, 1)" + }, + "0,999": { + "light": "rgba(255, 87, 87, 1)", + "dark": "rgba(133, 0, 0, 1)" } }, "ACCENT_HOVER": { @@ -65,6 +74,10 @@ "0,0": { "light": "rgba(153, 153, 153, 0.8)", "dark": "rgba(56, 56, 56, 0.4)" + }, + "0,999": { + "light": "rgba(255, 51, 51, 0.8)", + "dark": "rgba(112, 0, 0, 0.4)" } } } diff --git a/tests/utils/theme/test_color_replacement_generator.py b/tests/utils/theme/test_color_replacement_generator.py index 96c0764..d3cbe88 100644 --- a/tests/utils/theme/test_color_replacement_generator.py +++ b/tests/utils/theme/test_color_replacement_generator.py @@ -61,4 +61,19 @@ class ColorReplacementGeneratorTestCase(unittest.TestCase): (rgba for element, rgba in actual if element == expected_element), None ) assert actual_rgba is not None - assert expected_rgba == actual_rgba \ No newline at end of file + assert expected_rgba == actual_rgba + + def test_convert_with_saturation_higher_than_100_should_round_to_max_value_on_overflow(self): + theme_color = InstallationColor(hue=0, saturation=999, modes=[]) + mode: InstallationMode = "dark" + expected_output = self._get_expected_output(theme_color, mode) + + actual_output = self.generator.convert(mode, theme_color) + + self.assertEqual(len(expected_output), len(actual_output)) + for expected_name, expected_value in expected_output: + actual_value = next( + (value for name, value in actual_output if name == expected_name), None + ) + self.assertIsNotNone(actual_value) + self.assertEqual(expected_value, actual_value) \ No newline at end of file