Files
Shrlalgo.RvKits/WPFluent/Controls/ColorPicker/Hsb.cs

74 lines
2.0 KiB
C#
Raw Normal View History

2025-04-24 20:56:44 +08:00
namespace WPFluent.Controls
{
public struct Hsb
{
public Hsb(double Hue, double Saturation, double Brightness, double opacity = 1)
{
this.Hue = Hue;
this.Saturation = Saturation;
this.Brightness = Brightness;
Opacity = opacity;
}
/// <summary>
/// 亮度0~1
/// </summary>
public double Brightness { get; set; }
/// <summary>
/// 色相0~359
/// </summary>
public double Hue { get; set; }
/// <summary>
/// 不透明度0~1
/// </summary>
public double Opacity { get; set; }
/// <summary>
/// 饱和度0~1
/// </summary>
public double Saturation { get; set; }
public static Hsb FromHSB(double hue, double saturation, double brightness, double opacity = 1)
{
return new Hsb(hue, saturation, brightness, opacity);
}
public static Hsb FromColor(Color color)
{
var hsb = new Hsb();
var max = Math.Max(color.R, Math.Max(color.G, color.B));
var min = Math.Min(color.R, Math.Min(color.G, color.B));
var delta = max - min;
hsb.Brightness = max / 255.0;
hsb.Saturation = max == 0 ? 0 : delta / max;
if (delta == 0)
{
hsb.Hue = 0;
}
else if (max == color.R)
{
hsb.Hue = (color.G - color.B) / delta;
}
else if (max == color.G)
{
hsb.Hue = 2 + (color.B - color.R) / delta;
}
else
{
hsb.Hue = 4 + (color.R - color.G) / delta;
}
hsb.Hue *= 60;
if (hsb.Hue < 0)
hsb.Hue += 360;
return hsb;
}
//public override string ToString()
//{
// return base.ToString();
//}
}
}