mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Testing HSL and testing spaces in rgb(..), hsl(..) css declarations
This commit is contained in:
@@ -127,9 +127,7 @@ class Color(NamedTuple):
|
|||||||
Returns:
|
Returns:
|
||||||
Color: A new color.
|
Color: A new color.
|
||||||
"""
|
"""
|
||||||
print("A")
|
|
||||||
r, g, b = hls_to_rgb(h, l, s)
|
r, g, b = hls_to_rgb(h, l, s)
|
||||||
print("B")
|
|
||||||
return cls(int(r * 255 + 0.5), int(g * 255 + 0.5), int(b * 255 + 0.5))
|
return cls(int(r * 255 + 0.5), int(g * 255 + 0.5), int(b * 255 + 0.5))
|
||||||
|
|
||||||
def __rich__(self) -> Text:
|
def __rich__(self) -> Text:
|
||||||
@@ -342,17 +340,17 @@ class Color(NamedTuple):
|
|||||||
clamp(float_a, 0.0, 1.0),
|
clamp(float_a, 0.0, 1.0),
|
||||||
)
|
)
|
||||||
elif hsl is not None:
|
elif hsl is not None:
|
||||||
h, s, l = [value.strip() for value in hsl.split(",")]
|
h, s, l = hsl.split(",")
|
||||||
h = clamp(int(h), 0, 360) / 360
|
h = clamp(float(h), 0, 360) / 360
|
||||||
s = percentage_string_to_float(s)
|
s = percentage_string_to_float(s)
|
||||||
l = percentage_string_to_float(l)
|
l = percentage_string_to_float(l)
|
||||||
color = Color.from_hls(h, l, s)
|
color = Color.from_hls(h, l, s)
|
||||||
elif hsla is not None:
|
elif hsla is not None:
|
||||||
h, s, l, a = [value.strip() for value in hsl.split(",")]
|
h, s, l, a = hsla.split(",")
|
||||||
h = clamp(h, 0, 360)
|
h = clamp(float(h), 0, 360) / 360
|
||||||
s = percentage_string_to_float(s)
|
s = percentage_string_to_float(s)
|
||||||
l = percentage_string_to_float(l)
|
l = percentage_string_to_float(l)
|
||||||
a = clamp(a, 0.0, 1.0)
|
a = clamp(float(a), 0.0, 1.0)
|
||||||
color = Color.from_hls(h, l, s).with_alpha(a)
|
color = Color.from_hls(h, l, s).with_alpha(a)
|
||||||
else:
|
else:
|
||||||
raise AssertionError("Can't get here if RE_COLOR matches")
|
raise AssertionError("Can't get here if RE_COLOR matches")
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from textual.css.tokenizer import Expect, Tokenizer, Token
|
|||||||
|
|
||||||
PERCENT = r"-?\d+\.?\d*%"
|
PERCENT = r"-?\d+\.?\d*%"
|
||||||
DECIMAL = r"-?\d+\.?\d*"
|
DECIMAL = r"-?\d+\.?\d*"
|
||||||
COMMA = r",\s*"
|
COMMA = r"\s*,\s*"
|
||||||
OPEN_BRACE = r"\(\s*"
|
OPEN_BRACE = r"\(\s*"
|
||||||
CLOSE_BRACE = r"\s*\)"
|
CLOSE_BRACE = r"\s*\)"
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ COMMENT_START = r"\/\*"
|
|||||||
SCALAR = rf"{DECIMAL}(?:fr|%|w|h|vw|vh)"
|
SCALAR = rf"{DECIMAL}(?:fr|%|w|h|vw|vh)"
|
||||||
DURATION = r"\d+\.?\d*(?:ms|s)"
|
DURATION = r"\d+\.?\d*(?:ms|s)"
|
||||||
NUMBER = r"\-?\d+\.?\d*"
|
NUMBER = r"\-?\d+\.?\d*"
|
||||||
COLOR = f"{HEX_COLOR}|{RGB_COLOR}|{HSL_COLOR}"
|
COLOR = rf"{HEX_COLOR}|{RGB_COLOR}|{HSL_COLOR}"
|
||||||
KEY_VALUE = r"[a-zA-Z_-][a-zA-Z0-9_-]*=[0-9a-zA-Z_\-\/]+"
|
KEY_VALUE = r"[a-zA-Z_-][a-zA-Z0-9_-]*=[0-9a-zA-Z_\-\/]+"
|
||||||
TOKEN = "[a-zA-Z][a-zA-Z0-9_-]*"
|
TOKEN = "[a-zA-Z][a-zA-Z0-9_-]*"
|
||||||
STRING = r"\".*?\""
|
STRING = r"\".*?\""
|
||||||
|
|||||||
@@ -904,6 +904,32 @@ class TestParseText:
|
|||||||
assert styles.background == Color.parse("red")
|
assert styles.background == Color.parse("red")
|
||||||
|
|
||||||
|
|
||||||
|
class TestParseColor:
|
||||||
|
"""More in-depth tests around parsing of CSS colors"""
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("value,result", [
|
||||||
|
("rgb(1,255,50)", Color(1, 255, 50)),
|
||||||
|
("rgb( 1, 255,50 )", Color(1, 255, 50)),
|
||||||
|
("rgba( 1, 255,50,0.3 )", Color(1, 255, 50, 0.3)),
|
||||||
|
("rgba( 1, 255,50, 1.3 )", Color(1, 255, 50, 1.0)),
|
||||||
|
("hsl( 180, 50%, 50% )", Color(64, 191, 191)),
|
||||||
|
("hsl(180,50%,50%)", Color(64, 191, 191)),
|
||||||
|
("hsla(180,50%,50%,0.25)", Color(64, 191, 191, 0.25)),
|
||||||
|
("hsla( 180, 50% ,50%,0.25 )", Color(64, 191, 191, 0.25)),
|
||||||
|
("hsla( 180, 50% , 50% , 1.5 )", Color(64, 191, 191)),
|
||||||
|
])
|
||||||
|
def test_rgb_and_hsl(self, value, result):
|
||||||
|
css = f""".box {{
|
||||||
|
color: {value};
|
||||||
|
}}
|
||||||
|
"""
|
||||||
|
stylesheet = Stylesheet()
|
||||||
|
stylesheet.add_source(css)
|
||||||
|
|
||||||
|
styles = stylesheet.rules[0].styles
|
||||||
|
assert styles.color == result
|
||||||
|
|
||||||
|
|
||||||
class TestParseOffset:
|
class TestParseOffset:
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"offset_x, parsed_x, offset_y, parsed_y",
|
"offset_x, parsed_x, offset_y, parsed_y",
|
||||||
|
|||||||
Reference in New Issue
Block a user