ColorModelStrategy.cs 4.0 KB

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