themes.nim 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # author: Ethosa
  2. import
  3. color,
  4. exceptions,
  5. tables,
  6. macros
  7. type
  8. Colors = Table[string, ColorRef]
  9. ThemeRef* = ref object
  10. name*: string
  11. colors*: Colors
  12. proc newTheme*(name: string, colors: Colors): ThemeRef =
  13. ThemeRef(name: name, colors: colors)
  14. var
  15. themes = @[
  16. newTheme("default", {
  17. "background" : Color("#181527"),
  18. "background_deep" : Color("#131022"),
  19. "accent" : Color("#cb8ea3"),
  20. "accent_dark" : Color("#9a4488"),
  21. "foreground" : Color("#d8d3ec"),
  22. "url_color": Color("#582af2")}.toTable()),
  23. newTheme("light", {
  24. "background": Color("#fbd6b3"),
  25. "background_deep": Color("#cbb693"),
  26. "accent": Color("#fbaefa"),
  27. "accent_dark": Color("#da9de9"),
  28. "foreground": Color("#39414b"),
  29. "url_color": Color("#2a9afc")}.toTable())
  30. ]
  31. {.cast(noSideEffect).}:
  32. var current_theme* = themes[0].deepCopy()
  33. proc addTheme*(theme: ThemeRef) =
  34. themes.add(theme)
  35. proc getTheme*(name: string): ThemeRef =
  36. for theme in themes:
  37. if theme.name == name:
  38. return theme
  39. throwError(ResourceError, "Theme " & name & " isn't available.")
  40. proc `[]`*(theme: ThemeRef, index: string): ColorRef =
  41. ## Returns theme color by name.
  42. for name, clr in theme.colors.pairs():
  43. if name == index:
  44. return clr
  45. proc changeTheme*(name: string) =
  46. ## Changes color theme to another, if available.
  47. var theme = getTheme(name)
  48. current_theme.name = theme.name
  49. for name, clr in theme.colors.pairs():
  50. theme.colors[name].copyColorTo(current_theme.colors[name])
  51. macro `~`*(theme: ThemeRef, field: untyped): untyped =
  52. ## Alternative usage of `[]` proc.
  53. ##
  54. let fname = $field
  55. result = quote do:
  56. `theme`[`fname`]