chore: HSV hue is normalized between 0 and 1

This commit is contained in:
Syntriax 2025-05-02 18:53:44 +03:00
parent fc3c1ed1f9
commit 16e4077d40
2 changed files with 14 additions and 10 deletions

View File

@ -66,6 +66,7 @@ public readonly struct ColorHSV(float hue, float saturation, float value)
if (hue < 0f)
hue += 360f;
hue /= 360f;
saturation = max.ApproximatelyEquals(0f) ? 0f : delta / max;
value = max;

View File

@ -42,18 +42,21 @@ public readonly struct ColorRGB(byte r, byte g, byte b)
public static implicit operator ColorRGB(ColorRGBA rgba) => new(rgba.R, rgba.G, rgba.B);
public static implicit operator ColorRGB(ColorHSV hsv)
{
float c = hsv.Value * hsv.Saturation;
float x = c * (1 - Math.Abs(hsv.Hue / 60 % 2 - 1));
float m = hsv.Value - c;
float hue = hsv.Hue * 360f;
float chroma = hsv.Value * hsv.Saturation;
float x = chroma * (1f - Math.Abs(hue / 60f % 2f - 1f));
float m = hsv.Value - chroma;
float r1 = 0, g1 = 0, b1 = 0;
float r1 = 0f;
float g1 = 0f;
float b1 = 0f;
if (hsv.Hue >= 0 && hsv.Hue < 60) { r1 = c; g1 = x; b1 = 0; }
else if (hsv.Hue >= 60 && hsv.Hue < 120) { r1 = x; g1 = c; b1 = 0; }
else if (hsv.Hue >= 120 && hsv.Hue < 180) { r1 = 0; g1 = c; b1 = x; }
else if (hsv.Hue >= 180 && hsv.Hue < 240) { r1 = 0; g1 = x; b1 = c; }
else if (hsv.Hue >= 240 && hsv.Hue < 300) { r1 = x; g1 = 0; b1 = c; }
else if (hsv.Hue >= 300 && hsv.Hue < 360) { r1 = c; g1 = 0; b1 = x; }
if (hue < 60) { r1 = chroma; g1 = x; b1 = 0; }
else if (hue < 120) { r1 = x; g1 = chroma; b1 = 0; }
else if (hue < 180) { r1 = 0; g1 = chroma; b1 = x; }
else if (hue < 240) { r1 = 0; g1 = x; b1 = chroma; }
else if (hue < 300) { r1 = x; g1 = 0; b1 = chroma; }
else if (hue <= 360) { r1 = chroma; g1 = 0; b1 = x; }
byte r = (byte)Math.RoundToInt((r1 + m) * 255);
byte g = (byte)Math.RoundToInt((g1 + m) * 255);