colors.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """The colors used in Reflex are a wrapper around https://www.radix-ui.com/colors."""
  2. from __future__ import annotations
  3. from dataclasses import dataclass
  4. from typing import TYPE_CHECKING, Literal, get_args
  5. if TYPE_CHECKING:
  6. from reflex.vars import Var
  7. ColorType = Literal[
  8. "gray",
  9. "mauve",
  10. "slate",
  11. "sage",
  12. "olive",
  13. "sand",
  14. "tomato",
  15. "red",
  16. "ruby",
  17. "crimson",
  18. "pink",
  19. "plum",
  20. "purple",
  21. "violet",
  22. "iris",
  23. "indigo",
  24. "blue",
  25. "cyan",
  26. "teal",
  27. "jade",
  28. "green",
  29. "grass",
  30. "brown",
  31. "orange",
  32. "sky",
  33. "mint",
  34. "lime",
  35. "yellow",
  36. "amber",
  37. "gold",
  38. "bronze",
  39. "accent",
  40. "black",
  41. "white",
  42. ]
  43. COLORS = frozenset(get_args(ColorType))
  44. ShadeType = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  45. MIN_SHADE_VALUE = 1
  46. MAX_SHADE_VALUE = 12
  47. def format_color(
  48. color: ColorType | Var[str], shade: ShadeType | Var[int], alpha: bool | Var[bool]
  49. ) -> str:
  50. """Format a color as a CSS color string.
  51. Args:
  52. color: The color to use.
  53. shade: The shade of the color to use.
  54. alpha: Whether to use the alpha variant of the color.
  55. Returns:
  56. The formatted color.
  57. """
  58. if isinstance(alpha, bool):
  59. return f"var(--{color}-{'a' if alpha else ''}{shade})"
  60. from reflex.components.core import cond
  61. alpha_var = cond(alpha, "a", "")
  62. return f"var(--{color}-{alpha_var}{shade})"
  63. @dataclass
  64. class Color:
  65. """A color in the Reflex color palette."""
  66. # The color palette to use
  67. color: ColorType | Var[str]
  68. # The shade of the color to use
  69. shade: ShadeType | Var[int] = 7
  70. # Whether to use the alpha variant of the color
  71. alpha: bool | Var[bool] = False
  72. def __format__(self, format_spec: str) -> str:
  73. """Format the color as a CSS color string.
  74. Args:
  75. format_spec: The format specifier to use.
  76. Returns:
  77. The formatted color.
  78. """
  79. return format_color(self.color, self.shade, self.alpha)