ColorModelStrategy.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ColorHelper;
  2. using ColorConverter = ColorHelper.ColorConverter;
  3. namespace Terminal.Gui.Views;
  4. internal class ColorModelStrategy
  5. {
  6. public IEnumerable<ColorBar> CreateBars (ColorModel model) =>
  7. model switch
  8. {
  9. ColorModel.RGB => CreateRgbBars (),
  10. ColorModel.HSV => CreateHsvBars (),
  11. ColorModel.HSL => CreateHslBars (),
  12. _ => throw new ArgumentOutOfRangeException (nameof (model), model, null)
  13. };
  14. public Color GetColorFromBars (IList<IColorBar> bars, ColorModel model) =>
  15. model switch
  16. {
  17. ColorModel.RGB => ToColor (new ((byte)bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value)),
  18. ColorModel.HSV => ToColor (ColorConverter.HsvToRgb (new (bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value))),
  19. ColorModel.HSL => ToColor (ColorConverter.HslToRgb (new (bars [0].Value, (byte)bars [1].Value, (byte)bars [2].Value))),
  20. _ => throw new ArgumentOutOfRangeException (nameof (model), model, null)
  21. };
  22. public void SetBarsToColor (IList<IColorBar> bars, Color newValue, ColorModel model)
  23. {
  24. if (bars.Count == 0)
  25. {
  26. return;
  27. }
  28. switch (model)
  29. {
  30. case ColorModel.RGB:
  31. bars [0].SetValueWithoutRaisingEvent (newValue.R);
  32. bars [1].SetValueWithoutRaisingEvent (newValue.G);
  33. bars [2].SetValueWithoutRaisingEvent (newValue.B);
  34. break;
  35. case ColorModel.HSV:
  36. HSV newHsv = ColorConverter.RgbToHsv (new (newValue.R, newValue.G, newValue.B));
  37. bars [0].SetValueWithoutRaisingEvent (newHsv.H);
  38. bars [1].SetValueWithoutRaisingEvent (newHsv.S);
  39. bars [2].SetValueWithoutRaisingEvent (newHsv.V);
  40. break;
  41. case ColorModel.HSL:
  42. HSL newHsl = ColorConverter.RgbToHsl (new (newValue.R, newValue.G, newValue.B));
  43. bars [0].SetValueWithoutRaisingEvent (newHsl.H);
  44. bars [1].SetValueWithoutRaisingEvent (newHsl.S);
  45. bars [2].SetValueWithoutRaisingEvent (newHsl.L);
  46. break;
  47. default:
  48. throw new ArgumentOutOfRangeException (nameof (model), model, null);
  49. }
  50. }
  51. private static IEnumerable<ColorBar> CreateHslBars ()
  52. {
  53. HueBar h = new ()
  54. {
  55. Text = "H:"
  56. };
  57. yield return h;
  58. SaturationBar s = new ()
  59. {
  60. Text = "S:"
  61. };
  62. LightnessBar l = new ()
  63. {
  64. Text = "L:"
  65. };
  66. s.HBar = h;
  67. s.LBar = l;
  68. l.HBar = h;
  69. l.SBar = s;
  70. yield return s;
  71. yield return l;
  72. }
  73. private static IEnumerable<ColorBar> CreateHsvBars ()
  74. {
  75. HueBar h = new ()
  76. {
  77. Text = "H:"
  78. };
  79. yield return h;
  80. SaturationBar s = new ()
  81. {
  82. Text = "S:"
  83. };
  84. ValueBar v = new ()
  85. {
  86. Text = "V:"
  87. };
  88. s.HBar = h;
  89. s.VBar = v;
  90. v.HBar = h;
  91. v.SBar = s;
  92. yield return s;
  93. yield return v;
  94. }
  95. private static IEnumerable<ColorBar> CreateRgbBars ()
  96. {
  97. RBar r = new ()
  98. {
  99. Text = "R:"
  100. };
  101. GBar g = new ()
  102. {
  103. Text = "G:"
  104. };
  105. BBar b = new ()
  106. {
  107. Text = "B:"
  108. };
  109. r.GBar = g;
  110. r.BBar = b;
  111. g.RBar = r;
  112. g.BBar = b;
  113. b.RBar = r;
  114. b.GBar = g;
  115. yield return r;
  116. yield return g;
  117. yield return b;
  118. }
  119. private static Color ToColor (RGB rgb) { return new (rgb.R, rgb.G, rgb.B); }
  120. }