SaturationBar.cs 1.1 KB

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