ColorModelStrategy.cs 4.0 KB

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