Skip to content

Instantly share code, notes, and snippets.

@CH3COOH
Created August 17, 2012 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CH3COOH/3374737 to your computer and use it in GitHub Desktop.
Save CH3COOH/3374737 to your computer and use it in GitHub Desktop.
HsvFromRgb method
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