diff --git a/src/textual/color.py b/src/textual/color.py index 35dadb0e67..02e5d07e18 100644 --- a/src/textual/color.py +++ b/src/textual/color.py @@ -598,8 +598,8 @@ def colors(self) -> list[Color]: add_color = colors.append (stop1, color1), (stop2, color2) = self._stops[0:2] for step_position in range(accuracy): - step = step_position / accuracy - while step >= stop2: + step = step_position / (accuracy - 1) + while step > stop2: position += 1 (stop1, color1), (stop2, color2) = self._stops[ position : position + 2 @@ -627,7 +627,8 @@ def get_color(self, position: float) -> Color: Returns: A Textual color. """ - color_index = int(clamp(position, 0, 1) * (self._accuracy - 1)) + accuracy = self._accuracy - 1 + color_index = int(clamp(position * accuracy, 0, accuracy)) return self.colors[color_index] def get_rich_color(self, position: float) -> RichColor: @@ -641,7 +642,8 @@ def get_rich_color(self, position: float) -> RichColor: Returns: A (Rich) color. """ - color_index = int(clamp(position, 0, 1) * (self._accuracy - 1)) + accuracy = self._accuracy - 1 + color_index = int(clamp(position * accuracy, 0, accuracy)) return self.rich_colors[color_index] diff --git a/tests/test_color.py b/tests/test_color.py index bdae7a7cbb..3cda9854a2 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -245,8 +245,9 @@ def test_gradient_errors(): def test_gradient(): gradient = Gradient( (0, Color(255, 0, 0)), - (0.5, Color(0, 0, 255)), + (0.5, "blue"), (1, Color(0, 255, 0)), + accuracy=11, ) assert gradient.get_color(-1) == Color(255, 0, 0) @@ -255,7 +256,3 @@ def test_gradient(): assert gradient.get_color(1.2) == Color(0, 255, 0) assert gradient.get_color(0.5) == Color(0, 0, 255) assert gradient.get_color(0.7) == Color(0, 101, 153) - - gradient._stops.pop() - with pytest.raises(AssertionError): - gradient.get_color(1.0)