Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jul 14, 2024
1 parent 9c3498b commit dd8e005
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/textual/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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]


Expand Down
7 changes: 2 additions & 5 deletions tests/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit dd8e005

Please sign in to comment.