colors.py 1.6 KB

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