Fixed saturation overflow in --sat tweak. Fixes #59

This commit is contained in:
Vladyslav Hroshev
2025-04-24 09:05:03 +03:00
parent 419a8d36ba
commit 407aaf9c52
3 changed files with 39 additions and 3 deletions

View File

@@ -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]

View File

@@ -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)"
}
}
}

View File

@@ -62,3 +62,18 @@ class ColorReplacementGeneratorTestCase(unittest.TestCase):
)
assert actual_rgba is not None
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)