Created
August 17, 2012 01:47
-
-
Save CH3COOH/3375180 to your computer and use it in GitHub Desktop.
Hsv to Rgb method
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
/// <summary> | |
/// RGBへ変換する | |
/// </summary> | |
/// <returns>RGBオブジェクト</returns> | |
public RGB ToRGB() | |
{ | |
// まず、もしSが0.0と等しいなら、最終的な色は無色もしくは灰色である。 | |
if (Saturation == 0) | |
{ | |
return new RGB(Value, Value, Value); | |
} | |
double r = 0, g = 0, b = 0; | |
double f = 0; | |
double p = 0, q = 0, t = 0; | |
var h = Math.Min(360.0, Math.Max(0, Hue)); | |
var s = Math.Min(1.0, Math.Max(0, Saturation)); | |
var v = Math.Min(255.0, Math.Max(0, Value)); | |
h = (int)(h / 60); | |
f = h - Hue; | |
p = v * (1 - s); | |
q = v * (1 - f * s); | |
t = v * (1 - (1 - s) * s); | |
if (h == 0) | |
{ | |
r = v; g = t; b = p; | |
} | |
else if (h == 1) | |
{ | |
r = q; g = v; b = p; | |
} | |
else if (h == 2) | |
{ | |
r = p; g = v; b = t; | |
} | |
else if (h == 3) | |
{ | |
r = p; g = q; b = v; | |
} | |
else if (h == 4) | |
{ | |
r = t; g = p; b = v; | |
} | |
else if (h == 5) | |
{ | |
r = v; g = p; b = q; | |
} | |
return new RGB(r, g, b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hが0より少ない、360より多い時の計算が抜けていたのと、fの求め方がおかしかったので修正してみた。