Created
April 12, 2018 11:32
-
-
Save danwalmsley/223cf316ffb300fb99cf230e975d3517 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static float Hue2Rgb(float p, float q, float t) | |
{ | |
if (t < 0) t += 1; | |
if (t > 1) t -= 1; | |
if (t < 1.0 / 6.0) return p + (q - p) * 6.0f * t; | |
if (t < 1.0 / 2.0) return q; | |
if (t < 2.0 / 3.0) return p + (q - p) * (2.0f / 3.0f - t) * 6.0f; | |
return p; | |
} | |
public static BGRAColor FromHsv(HsvColor color) | |
{ | |
float r = 0; | |
float g = 0; | |
float b = 0; | |
var i = (float)Math.Floor(color.Hue * 6f); | |
var f = color.Hue * 6f - i; | |
var p = color.Value * (1f - color.Saturation); | |
var q = color.Value * (1f - f * color.Saturation); | |
var t = color.Value * (1f - (1f - f) * color.Saturation); | |
switch (i % 6) | |
{ | |
case 0: r = color.Value; g = t; b = p; break; | |
case 1: r = q; g = color.Value; b = p; break; | |
case 2: r = p; g = color.Value; b = t; break; | |
case 3: r = p; g = q; b = color.Value; break; | |
case 4: r = t; g = p; b = color.Value; break; | |
case 5: r = color.Value; g = p; b = q; break; | |
} | |
return new BGRAColor(Trim(r * 255), Trim(g * 255), Trim(b * 255), 255); | |
} | |
public static BGRAColor FromHsl(HslColor color) | |
{ | |
float r, g, b; | |
if (color.Saturation == 0) | |
{ | |
r = g = b = color.Lightness; // achromatic | |
} | |
else | |
{ | |
var q = color.Lightness < 0.5 ? color.Lightness * (1 + color.Saturation) : color.Lightness + color.Saturation - color.Lightness * color.Saturation; | |
var p = 2f * color.Lightness - q; | |
r = Hue2Rgb(p, q, color.Hue + 1.0f / 3.0f); | |
g = Hue2Rgb(p, q, color.Hue); | |
b = Hue2Rgb(p, q, color.Hue - 1.0f / 3.0f); | |
} | |
return new BGRAColor(Trim(r * 255), Trim(g * 255), Trim(b * 255), 255); | |
} | |
public static HslColor RgbToHsl (this IColor color) | |
{ | |
var r = color.R / 255.0f; | |
var g = color.G / 255.0f; | |
var b = color.B / 255.0f; | |
var max = Math.Max(r, Math.Max(g, b)); | |
var min = Math.Min(r, Math.Min(g, b)); | |
float h,s,l; | |
h = s = l = (max + min) / 2; | |
if (max == min) | |
{ | |
h = s = 0; // achromatic | |
} | |
else | |
{ | |
var d = max - min; | |
s = l > 0.5 ? d / (2 - max - min) : d / (max + min); | |
if(max == r) | |
{ | |
h = (g - b) / d + (g < b ? 6 : 0); | |
} | |
else if(max == g) | |
{ | |
h = (b - r) / d + 2; | |
} | |
else if(max == b) | |
{ | |
h = (r - g) / d + 4; | |
} | |
h /= 6; | |
} | |
return new HslColor { Hue = h, Saturation = s, Lightness = l }; | |
} | |
public static HsvColor RgbToHsv(this IColor color) | |
{ | |
var r = color.R / 255.0f; | |
var g = color.G / 255.0f; | |
var b = color.B / 255.0f; | |
var max = Math.Max(r, Math.Max(g, b)); | |
var min = Math.Min(r, Math.Min(g, b)); | |
float h, s, v; | |
h = s = v = max; | |
var d = max - min; | |
s = max == 0 ? 0 : d / max; | |
if (max == min) | |
{ | |
h = 0; // achromatic | |
} | |
else | |
{ | |
if(max == r) | |
{ | |
h = (g - b) / d + (g < b ? 6 : 0); | |
} | |
else if (max == g) | |
{ | |
h = (b - r) / d + 2; | |
} | |
else if (max == b) | |
{ | |
h = (r - g) / d + 4; | |
} | |
h /= 6; | |
} | |
return new HsvColor { Hue = h, Saturation = s, Value = v }; | |
} | |
static float RGBChannelFromHue(float m1, float m2, float h) | |
{ | |
h = (h + 1f) % 1f; | |
if (h < 0) h += 1; | |
if (h * 6 < 1) return m1 + (m2 - m1) * 6 * h; | |
else if (h * 2 < 1) return m2; | |
else if (h * 3 < 2) return m1 + (m2 - m1) * 6 * (2f / 3f - h); | |
else return m1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment