Ensure opacity values are in range [0.0, 1.0]

This commit is contained in:
Arjan Molenaar 2021-02-07 12:26:10 +01:00
parent d43308dba6
commit 844272f37a
No known key found for this signature in database
GPG Key ID: BF977B918996CB13
3 changed files with 32 additions and 2 deletions

View File

@ -133,6 +133,8 @@ family, size, weight.
``background-color: hsl(130, 95%, 10%);``
``color`` Color used for lines
``text-color`` Color for text
``opacity`` Color opacity factor (``0.0`` - ``1.0``),
applied to all colors
======================= =======================================
```

View File

@ -108,10 +108,18 @@ def parse_color(prop, value):
"line-width",
"vertical-spacing",
"border-radius",
"opacity",
)
def parse_positive_number(prop, value) -> Optional[Number]:
if isinstance(value, number) and value > 0:
if isinstance(value, number) and value >= 0:
return value
return None
@declarations.register(
"opacity",
)
def parse_factor(prop, value) -> Optional[Number]:
if isinstance(value, number) and 0 <= value <= 1:
return value
return None

View File

@ -222,6 +222,26 @@ def test_line_style(css_value, result):
assert props.get("line-style") == result
@pytest.mark.parametrize(
"css_value,result",
[
[0.5, 0.5],
[0, 0.0],
[1, 1.0],
[-0.1, None],
[1.1, None],
["wrong", None],
],
)
def test_opacity(css_value, result):
css = f"mytype {{ opacity: {css_value} }}"
compiled_style_sheet = CompiledStyleSheet(css)
props = compiled_style_sheet.match(Node("mytype"))
assert props.get("opacity") == result
def test_broken_line_style():
# diagram css is missing the closing bracket
css = "diagram { line-style: sloppy * { }"