color_defaults.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from __future__ import annotations
  2. from flexmeasures.data.models.user import Account
  3. def get_color_settings(account: Account | None) -> dict:
  4. """
  5. This function returns the primary and secondary color settings for the UI.
  6. It also provides variations of the primary and secondary colors, such as border color, hover color, and transparent color.
  7. """
  8. primary_color: str = "#1a3443"
  9. secondary_color: str = "#f1a122"
  10. if account:
  11. primary_color = str(
  12. account.primary_color
  13. or (
  14. account.consultancy_account
  15. and account.consultancy_account.primary_color
  16. )
  17. or primary_color
  18. )
  19. secondary_color = str(
  20. account.secondary_color
  21. or (
  22. account.consultancy_account
  23. and account.consultancy_account.secondary_color
  24. )
  25. or secondary_color
  26. )
  27. # Compute variations
  28. primary_border_color = darken_color(primary_color, 7)
  29. primary_hover_color = darken_color(primary_color, 4)
  30. primary_transparent = rgba_color(primary_color, 0.2)
  31. secondary_hover_color = lighten_color(secondary_color, 4)
  32. secondary_transparent = rgba_color(secondary_color, 0.2)
  33. return {
  34. "primary_color": primary_color,
  35. "primary_border_color": primary_border_color,
  36. "primary_hover_color": primary_hover_color,
  37. "primary_transparent": primary_transparent,
  38. "secondary_color": secondary_color,
  39. "secondary_hover_color": secondary_hover_color,
  40. "secondary_transparent": secondary_transparent,
  41. }
  42. def darken_color(hex_color: str, percentage: int) -> str:
  43. hex_color = hex_color.lstrip("#")
  44. r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
  45. r = int(r * (1 - percentage / 100))
  46. g = int(g * (1 - percentage / 100))
  47. b = int(b * (1 - percentage / 100))
  48. return f"#{r:02x}{g:02x}{b:02x}"
  49. def lighten_color(hex_color: str, percentage: int) -> str:
  50. hex_color = hex_color.lstrip("#")
  51. r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
  52. r = int(r + (255 - r) * percentage / 100)
  53. g = int(g + (255 - g) * percentage / 100)
  54. b = int(b + (255 - b) * percentage / 100)
  55. return f"#{r:02x}{g:02x}{b:02x}"
  56. def rgba_color(hex_color: str, alpha: float) -> str:
  57. hex_color = hex_color.lstrip("#")
  58. r, g, b = int(hex_color[0:2], 16), int(hex_color[2:4], 16), int(hex_color[4:6], 16)
  59. return f"rgba({r}, {g}, {b}, {alpha})"