按标准公式计算。
// RGB转HSV
public static void RGB2HSV(int r, int g, int b, out float h, out float s, out float v)
{
float max = Math.Max(r, Math.Max(g,b));
float min = Math.Min(r, Math.Min(g,b));
// 计算hue
if(min == max) { h = 0;}
else if(max == r && g >= b)
{
h = 60*(g - b) / (max - min);
}
else if(max == r && g < b)
{
h = 360 + 60 * (g - b) / (max - min);
}
else if(max == g)
{
h = 120 + 60 * (b - r) / (max - min);
}
else
{
h = 240 + 60*(r - g) / (max - min);
}
// 计算saturation
if(max==0)
{
s = 0;
}
else
{
s = (max - min) / max;
}
// 计算v / brightness
v = max / 255;
}
// HSV转RGB
public static void HSV2RGB(float h, float s, float v, out int r, out int g, out int b)
{
// s,v为小数表示,如0.25表示25
float h