SukiColorTheme.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using Avalonia.Media;
  3. using SukiUI.Enums;
  4. namespace SukiUI.Models;
  5. public record SukiColorTheme
  6. {
  7. public string DisplayName { get; }
  8. public Color Primary { get; }
  9. public IBrush PrimaryBrush => new SolidColorBrush(Primary);
  10. public Color PrimaryDark { get; }
  11. public IBrush PrimaryDarkBrush => new SolidColorBrush(PrimaryDark);
  12. public Color Accent { get; }
  13. public IBrush AccentBrush => new SolidColorBrush(Accent);
  14. public Color AccentDark { get; }
  15. public IBrush AccentDarkBrush => new SolidColorBrush(AccentDark);
  16. // Used in shaders to save calculating them per-frame.
  17. internal Color BackgroundPrimary { get; }
  18. internal Color BackgroundAccent { get; }
  19. internal Color Background { get; }
  20. // dark scale...
  21. private const double dS = 0.5;
  22. public SukiColorTheme(string displayName, Color primary, Color accent)
  23. {
  24. DisplayName = displayName;
  25. Primary = primary;
  26. Accent = accent;
  27. PrimaryDark = new Color(primary.A, (byte)(primary.R * dS), (byte)(primary.G * dS), (byte)(primary.B * dS));
  28. AccentDark = new Color(accent.A, (byte)(accent.R * dS), (byte)(accent.G * dS), (byte)(accent.B * dS));
  29. Background = GetBackgroundColor(Primary);
  30. BackgroundPrimary = new Color(primary.A, (byte)(primary.R / 1), (byte)(primary.G / 1), (byte)(primary.B / 1));
  31. BackgroundAccent = new Color(accent.A, (byte)(accent.R / 1), (byte)(accent.G / 1), (byte)(accent.B / 1));
  32. }
  33. public override int GetHashCode()
  34. {
  35. unchecked
  36. {
  37. var hash = 17;
  38. hash *= 31 + Primary.GetHashCode();
  39. hash *= 31 + Accent.GetHashCode();
  40. hash *= 31 + DisplayName.GetHashCode();
  41. return hash;
  42. }
  43. }
  44. public override string ToString()
  45. {
  46. return DisplayName;
  47. }
  48. private static Color GetBackgroundColor(Color input)
  49. {
  50. int r = input.R;
  51. int g = input.G;
  52. int b = input.B;
  53. var minValue = Math.Min(Math.Min(r, g), b);
  54. var maxValue = Math.Max(Math.Max(r, g), b);
  55. r = (r == minValue) ? 47 : ((r == maxValue) ? 47 : 27);
  56. g = (g == minValue) ? 47 : ((g == maxValue) ? 47 : 27);
  57. b = (b == minValue) ? 47 : ((b == maxValue) ? 47 : 27);
  58. return new Color(255, (byte)r, (byte)g, (byte)b);
  59. }
  60. }
  61. internal record DefaultSukiColorTheme : SukiColorTheme
  62. {
  63. internal SukiColor ThemeColor { get; }
  64. internal DefaultSukiColorTheme(SukiColor themeColor, Color primary, Color accent)
  65. : base(themeColor.ToString(), primary, accent)
  66. {
  67. ThemeColor = themeColor;
  68. }
  69. }