Ensure opacity values are in range [0.0, 1.0]
This commit is contained in:
parent
d43308dba6
commit
844272f37a
@ -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
|
||||
======================= =======================================
|
||||
```
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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 * { }"
|
||||
|
Loading…
x
Reference in New Issue
Block a user