private void HSV_Load(object sender, EventArgs e) { var rnd = new Random(); var rgb = new ColorRGB(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255)); var hsv = ColorHelper.RgbToHsv(rgb); picDestinationColor.BackColor = Color.FromArgb(255, rgb.R, rgb.G, rgb.B); nudH.Value = hsv.H; nudS.Value = hsv.S; nudV.Value = hsv.V; refreshColorPanel(hsv); picH.Value = Convert.ToInt32(nudH.Value); picS.Value = Convert.ToInt32(nudS.Value); picV.Value = Convert.ToInt32(nudV.Value); }
public static ColorHSV RgbToHsv(ColorRGB rgb) { float min, max, tmp, H, S, V; float R = rgb.R * 1.0f / 255, G = rgb.G * 1.0f / 255, B = rgb.B * 1.0f / 255; tmp = Math.Min(R, G); min = Math.Min(tmp, B); tmp = Math.Max(R, G); max = Math.Max(tmp, B); // H H = 0; if (max == min) { H = 0; } else if (max == R && G > B) { H = 60 * (G - B) * 1.0f / (max - min) + 0; } else if (max == R && G < B) { H = 60 * (G - B) * 1.0f / (max - min) + 360; } else if (max == G) { H = H = 60 * (B - R) * 1.0f / (max - min) + 120; } else if (max == B) { H = H = 60 * (R - G) * 1.0f / (max - min) + 240; } // S if (max == 0) { S = 0; } else { S = (max - min) * 1.0f / max; } // V V = max; return(new ColorHSV((int)H, (int)(S * 255), (int)(V * 255))); }