ColorModelStrategy.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. switch (model)
  42. {
  43. case ColorModel.RGB:
  44. bars [0].SetValueWithoutRaisingEvent (newValue.R);
  45. bars [1].SetValueWithoutRaisingEvent (newValue.G);
  46. bars [2].SetValueWithoutRaisingEvent (newValue.B);
  47. break;
  48. case ColorModel.HSV:
  49. HSV newHsv = ColorConverter.RgbToHsv (new (newValue.R, newValue.G, newValue.B));
  50. bars [0].SetValueWithoutRaisingEvent (newHsv.H);
  51. bars [1].SetValueWithoutRaisingEvent (newHsv.S);
  52. bars [2].SetValueWithoutRaisingEvent (newHsv.V);
  53. break;
  54. case ColorModel.HSL:
  55. HSL newHsl = ColorConverter.RgbToHsl (new (newValue.R, newValue.G, newValue.B));
  56. bars [0].SetValueWithoutRaisingEvent (newHsl.H);
  57. bars [1].SetValueWithoutRaisingEvent (newHsl.S);
  58. bars [2].SetValueWithoutRaisingEvent (newHsl.L);
  59. break;
  60. default:
  61. throw new ArgumentOutOfRangeException (nameof (model), model, null);
  62. }
  63. }
  64. private IEnumerable<ColorBar> CreateHslBars ()
  65. {
  66. var h = new HueBar
  67. {
  68. Text = "H:"
  69. };
  70. yield return h;
  71. var s = new SaturationBar
  72. {
  73. Text = "S:"
  74. };
  75. var l = new LightnessBar
  76. {
  77. Text = "L:"
  78. };
  79. s.HBar = h;
  80. s.LBar = l;
  81. l.HBar = h;
  82. l.SBar = s;
  83. yield return s;
  84. yield return l;
  85. }
  86. private IEnumerable<ColorBar> CreateHsvBars ()
  87. {
  88. var h = new HueBar
  89. {
  90. Text = "H:"
  91. };
  92. yield return h;
  93. var s = new SaturationBar
  94. {
  95. Text = "S:"
  96. };
  97. var v = new ValueBar
  98. {
  99. Text = "V:"
  100. };
  101. s.HBar = h;
  102. s.VBar = v;
  103. v.HBar = h;
  104. v.SBar = s;
  105. yield return s;
  106. yield return v;
  107. }
  108. private IEnumerable<ColorBar> CreateRgbBars ()
  109. {
  110. var r = new RBar
  111. {
  112. Text = "R:"
  113. };
  114. var g = new GBar
  115. {
  116. Text = "G:"
  117. };
  118. var b = new BBar
  119. {
  120. Text = "B:"
  121. };
  122. r.GBar = g;
  123. r.BBar = b;
  124. g.RBar = r;
  125. g.BBar = b;
  126. b.RBar = r;
  127. b.GBar = g;
  128. yield return r;
  129. yield return g;
  130. yield return b;
  131. }
  132. private Color ToColor (RGB rgb) { return new (rgb.R, rgb.G, rgb.B); }
  133. }