SaturationBar.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #nullable enable
  2. using ColorHelper;
  3. using ColorConverter = ColorHelper.ColorConverter;
  4. namespace Terminal.Gui;
  5. internal class SaturationBar : ColorBar
  6. {
  7. public HueBar? HBar { get; set; }
  8. // Should only have either LBar or VBar not both
  9. public LightnessBar? LBar { get; set; }
  10. public ValueBar? VBar { get; set; }
  11. /// <inheritdoc/>
  12. protected override Color GetColor (double fraction)
  13. {
  14. if (LBar != null && HBar != null)
  15. {
  16. var hsl = new HSL (HBar.Value, (byte)(MaxValue * fraction), (byte)LBar.Value);
  17. RGB rgb = ColorConverter.HslToRgb (hsl);
  18. return new (rgb.R, rgb.G, rgb.B);
  19. }
  20. if (VBar != null && HBar != null)
  21. {
  22. var hsv = new HSV (HBar.Value, (byte)(MaxValue * fraction), (byte)VBar.Value);
  23. RGB rgb = ColorConverter.HsvToRgb (hsv);
  24. return new (rgb.R, rgb.G, rgb.B);
  25. }
  26. throw new ($"{nameof (SaturationBar)} requires either Lightness or Value to render");
  27. }
  28. /// <inheritdoc/>
  29. protected override int MaxValue => 100;
  30. }