Created
August 17, 2012 00:17
-
-
Save CH3COOH/3374737 to your computer and use it in GitHub Desktop.
HsvFromRgb 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
private HSV HsvFromRgb(double r, double g, double b) | |
{ | |
var max = Math.Max(Math.Max(r, g), b); | |
var min = Math.Min(Math.Min(r, g), b); | |
var sub = max - min; | |
double h = 0, s = 0, v = 0; | |
// Calculate Hue | |
if (sub == 0) | |
{ | |
// MAX = MIN(例・S = 0)のとき、 Hは定義されない。 | |
h = 0; | |
} | |
else | |
{ | |
if(max == r) | |
{ | |
h = (60 * (g - b) / sub); | |
} | |
else if(max == g) | |
{ | |
h = (60 * (b - r) / sub) + 120; | |
} | |
else if(max == b) | |
{ | |
h = (60 * (r - g) / sub) + 240; | |
} | |
// さらに H += 360 if H < 0 | |
if (h < 0) | |
{ | |
h += 360; | |
} | |
} | |
// Calculate Saturation | |
if (max > 0) | |
{ | |
s = sub / max; | |
} | |
// Calculate Value | |
v = max; | |
return new HSV(h, s, v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment