|
@@ -1,7 +1,12 @@
|
|
"""The colors used in Reflex are a wrapper around https://www.radix-ui.com/colors."""
|
|
"""The colors used in Reflex are a wrapper around https://www.radix-ui.com/colors."""
|
|
|
|
|
|
|
|
+from __future__ import annotations
|
|
|
|
+
|
|
from dataclasses import dataclass
|
|
from dataclasses import dataclass
|
|
-from typing import Literal
|
|
|
|
|
|
+from typing import TYPE_CHECKING, Literal, get_args
|
|
|
|
+
|
|
|
|
+if TYPE_CHECKING:
|
|
|
|
+ from reflex.vars import Var
|
|
|
|
|
|
ColorType = Literal[
|
|
ColorType = Literal[
|
|
"gray",
|
|
"gray",
|
|
@@ -40,10 +45,16 @@ ColorType = Literal[
|
|
"white",
|
|
"white",
|
|
]
|
|
]
|
|
|
|
|
|
|
|
+COLORS = frozenset(get_args(ColorType))
|
|
|
|
+
|
|
ShadeType = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
ShadeType = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
|
|
+MIN_SHADE_VALUE = 1
|
|
|
|
+MAX_SHADE_VALUE = 12
|
|
|
|
|
|
|
|
|
|
-def format_color(color: ColorType, shade: ShadeType, alpha: bool) -> str:
|
|
|
|
|
|
+def format_color(
|
|
|
|
+ color: ColorType | Var[str], shade: ShadeType | Var[int], alpha: bool | Var[bool]
|
|
|
|
+) -> str:
|
|
"""Format a color as a CSS color string.
|
|
"""Format a color as a CSS color string.
|
|
|
|
|
|
Args:
|
|
Args:
|
|
@@ -54,7 +65,13 @@ def format_color(color: ColorType, shade: ShadeType, alpha: bool) -> str:
|
|
Returns:
|
|
Returns:
|
|
The formatted color.
|
|
The formatted color.
|
|
"""
|
|
"""
|
|
- return f"var(--{color}-{'a' if alpha else ''}{shade})"
|
|
|
|
|
|
+ if isinstance(alpha, bool):
|
|
|
|
+ return f"var(--{color}-{'a' if alpha else ''}{shade})"
|
|
|
|
+
|
|
|
|
+ from reflex.components.core import cond
|
|
|
|
+
|
|
|
|
+ alpha_var = cond(alpha, "a", "")
|
|
|
|
+ return f"var(--{color}-{alpha_var}{shade})"
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
@dataclass
|
|
@@ -62,13 +79,13 @@ class Color:
|
|
"""A color in the Reflex color palette."""
|
|
"""A color in the Reflex color palette."""
|
|
|
|
|
|
# The color palette to use
|
|
# The color palette to use
|
|
- color: ColorType
|
|
|
|
|
|
+ color: ColorType | Var[str]
|
|
|
|
|
|
# The shade of the color to use
|
|
# The shade of the color to use
|
|
- shade: ShadeType = 7
|
|
|
|
|
|
+ shade: ShadeType | Var[int] = 7
|
|
|
|
|
|
# Whether to use the alpha variant of the color
|
|
# Whether to use the alpha variant of the color
|
|
- alpha: bool = False
|
|
|
|
|
|
+ alpha: bool | Var[bool] = False
|
|
|
|
|
|
def __format__(self, format_spec: str) -> str:
|
|
def __format__(self, format_spec: str) -> str:
|
|
"""Format the color as a CSS color string.
|
|
"""Format the color as a CSS color string.
|