colors.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "gray",
  37. "accent",
  38. ]
  39. ShadeType = Literal[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  40. def format_color(color: ColorType, shade: ShadeType, alpha: bool) -> str:
  41. """Format a color as a CSS color string.
  42. Args:
  43. color: The color to use.
  44. shade: The shade of the color to use.
  45. alpha: Whether to use the alpha variant of the color.
  46. Returns:
  47. The formatted color.
  48. """
  49. return f"var(--{color}-{'a' if alpha else ''}{shade})"
  50. @dataclass
  51. class Color:
  52. """A color in the Reflex color palette."""
  53. # The color palette to use
  54. color: ColorType
  55. # The shade of the color to use
  56. shade: ShadeType = 7
  57. # Whether to use the alpha variant of the color
  58. alpha: bool = False
  59. def __format__(self, format_spec: str) -> str:
  60. """Format the color as a CSS color string.
  61. Args:
  62. format_spec: The format specifier to use.
  63. Returns:
  64. The formatted color.
  65. """
  66. return format_color(self.color, self.shade, self.alpha)