public HsvColor ToHsv()
{
// In this function, R, G, and B values must be scaled
// to be between 0 and 1.
// HsvColor.Hue will be a value between 0 and 360, and
// HsvColor.Saturation and value are between 0 and 1.
double min;
double max;
double delta;
double r = (double) Red / 255;
double g = (double) Green / 255;
double b = (double) Blue / 255;
double h;
double s;
double v;
min = Math.Min(Math.Min(r, g), b);
max = Math.Max(Math.Max(r, g), b);
v = max;
delta = max - min;
if (max == 0 || delta == 0)
{
// R, G, and B must be 0, or all the same.
// In this case, S is 0, and H is undefined.
// Using H = 0 is as good as any...
s = 0;
h = 0;
}
else
{
s = delta / max;
if (r == max)
{
// Between Yellow and Magenta
h = (g - b) / delta;
}
else if (g == max)
{
// Between Cyan and Yellow
h = 2 + (b - r) / delta;
}
else
{
// Between Magenta and Cyan
h = 4 + (r - g) / delta;
}
}
// Scale h to be between 0 and 360.
// This may require adding 360, if the value
// is negative.
h *= 60;
if (h < 0)
{
h += 360;
}
// Scale to the requirements of this
// application. All values are between 0 and 255.
return new HsvColor((int)h, (int)(s * 100), (int)(v * 100));
}