mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add tests for negative number hsl handling, normalise degrees to range 0-1
This commit is contained in:
committed by
Will McGugan
parent
adcce9d58f
commit
9c7d7b703e
@@ -287,7 +287,7 @@ class Color(NamedTuple):
|
||||
if color_match is None:
|
||||
error_message = f"failed to parse {color_text!r} as a color"
|
||||
suggested_color = None
|
||||
if not color_text.startswith(('#', 'rgb', 'hsl')):
|
||||
if not color_text.startswith(("#", "rgb", "hsl")):
|
||||
# Seems like we tried to use a color name: let's try to find one that is close enough:
|
||||
suggested_color = get_suggestion(color_text, COLOR_NAME_TO_RGB.keys())
|
||||
if suggested_color:
|
||||
@@ -336,13 +336,13 @@ class Color(NamedTuple):
|
||||
)
|
||||
elif hsl is not None:
|
||||
h, s, l = hsl.split(",")
|
||||
h = float(h) % 360
|
||||
h = float(h) % 360 / 360
|
||||
s = percentage_string_to_float(s)
|
||||
l = percentage_string_to_float(l)
|
||||
color = Color.from_hls(h, l, s)
|
||||
elif hsla is not None:
|
||||
h, s, l, a = hsla.split(",")
|
||||
h = clamp(float(h), 0, 360) / 360
|
||||
h = float(h) % 360 / 360
|
||||
s = percentage_string_to_float(s)
|
||||
l = percentage_string_to_float(l)
|
||||
a = clamp(float(a), 0.0, 1.0)
|
||||
|
||||
@@ -336,6 +336,11 @@ NULL_SCALAR = ScalarOffset(Scalar.from_number(0), Scalar.from_number(0))
|
||||
|
||||
|
||||
def percentage_string_to_float(string: str) -> float:
|
||||
"""Convert a string percentage e.g. '20%' to a float e.g. 20.0.
|
||||
|
||||
Args:
|
||||
string (str): The percentage string to convert.
|
||||
"""
|
||||
string = string.strip()
|
||||
if string.endswith("%"):
|
||||
percentage = string[:-1]
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import pytest
|
||||
|
||||
from rich.color import Color as RichColor
|
||||
from rich.text import Text
|
||||
|
||||
@@ -136,6 +135,15 @@ def test_color_parse_clamp(input, output):
|
||||
assert Color.parse(input) == output
|
||||
|
||||
|
||||
def test_color_parse_hsl_negative_degrees():
|
||||
assert Color.parse("hsl(-90, 50%, 50%)") == Color.parse("hsl(270, 50%, 50%)")
|
||||
|
||||
|
||||
def test_color_parse_hsla_negative_degrees():
|
||||
assert Color.parse("hsla(-45, 50%, 50%, 0.2)") == Color.parse(
|
||||
"hsla(315, 50%, 50%, 0.2)")
|
||||
|
||||
|
||||
def test_color_parse_color():
|
||||
# as a convenience, if Color.parse is passed a color object, it will return it
|
||||
color = Color(20, 30, 40, 0.5)
|
||||
|
||||
Reference in New Issue
Block a user