How to change RGB color to HSV?

How to change RGB color to HSV? In C #. I am looking for a very fast method without any external library.

+56
c # colors rgb hsv
Dec 11 '08 at 14:53
source share
6 answers

Have you just considered using the System.Drawing namespace? For example:

System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue); float hue = color.GetHue(); float saturation = color.GetSaturation(); float lightness = color.GetBrightness(); 

Please note that this is not exactly what you asked for (see the differences between HSL and HSV , and the Color class does not have a return from HSL / HSV, but the latter is reasonably easy to add .

+24
Mar 12 '09 at 1:07
source share

Note that Color.GetSaturation() and Color.GetBrightness() return HSL values, not HSV values.
The following code shows the difference.

 Color original = Color.FromArgb(50, 120, 200); // original = {Name=ff3278c8, ARGB=(255, 50, 120, 200)} double hue; double saturation; double value; ColorToHSV(original, out hue, out saturation, out value); // hue = 212.0 // saturation = 0.75 // value = 0.78431372549019607 Color copy = ColorFromHSV(hue, saturation, value); // copy = {Name=ff3278c8, ARGB=(255, 50, 120, 200)} // Compare that to the HSL values that the .NET framework provides: original.GetHue(); // 212.0 original.GetSaturation(); // 0.6 original.GetBrightness(); // 0.490196079 

The following C # code is what you want. It converts RGB and HSV using the algorithms described on Wikipedia . Ranges 0 - 360 for hue and 0 - 1 for saturation or value .

 public static void ColorToHSV(Color color, out double hue, out double saturation, out double value) { int max = Math.Max(color.R, Math.Max(color.G, color.B)); int min = Math.Min(color.R, Math.Min(color.G, color.B)); hue = color.GetHue(); saturation = (max == 0) ? 0 : 1d - (1d * min / max); value = max / 255d; } public static Color ColorFromHSV(double hue, double saturation, double value) { int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; double f = hue / 60 - Math.Floor(hue / 60); value = value * 255; int v = Convert.ToInt32(value); int p = Convert.ToInt32(value * (1 - saturation)); int q = Convert.ToInt32(value * (1 - f * saturation)); int t = Convert.ToInt32(value * (1 - (1 - f) * saturation)); if (hi == 0) return Color.FromArgb(255, v, t, p); else if (hi == 1) return Color.FromArgb(255, q, v, p); else if (hi == 2) return Color.FromArgb(255, p, v, t); else if (hi == 3) return Color.FromArgb(255, p, q, v); else if (hi == 4) return Color.FromArgb(255, t, p, v); else return Color.FromArgb(255, v, p, q); } 
+93
Oct 26 '09 at 17:34
source share

Here's the implementation of C:

http://www.cs.rit.edu/~ncs/color/t_convert.html

It should be very simple to convert to C #, since almost no functions are called - just calculations.

found through google

+5
Dec 11 '08 at 14:57
source share

EasyRGB has many color space conversions. Here is the code for converting RGB-> HSV.

+4
Oct 09 '12 at 21:45
source share

This is the version of VB.net that works fine for me ported from C code in a BlaM post.

Here's the implementation of C:

http://www.cs.rit.edu/~ncs/color/t_convert.html

It should be very simple to convert to C #, since almost no functions are called - just> calculations.




 Public Sub HSVtoRGB(ByRef r As Double, ByRef g As Double, ByRef b As Double, ByVal h As Double, ByVal s As Double, ByVal v As Double) Dim i As Integer Dim f, p, q, t As Double If (s = 0) Then ' achromatic (grey) r = v g = v b = v Exit Sub End If h /= 60 'sector 0 to 5 i = Math.Floor(h) f = h - i 'factorial part of h p = v * (1 - s) q = v * (1 - s * f) t = v * (1 - s * (1 - f)) Select Case (i) Case 0 r = v g = t b = p Exit Select Case 1 r = q g = v b = p Exit Select Case 2 r = p g = v b = t Exit Select Case 3 r = p g = q b = v Exit Select Case 4 r = t g = p b = v Exit Select Case Else 'case 5: r = v g = p b = q Exit Select End Select End Sub 
+3
Jul 27 '09 at 10:51
source share

FIRST: make sure you have the color as a bitmap, for example:

 Bitmap bmp = (Bitmap)pictureBox1.Image.Clone(); paintcolor = bmp.GetPixel(eX, eY); 

(e from the event handler that selected my color!)

What I did when I had this problem, I got the first rgba values โ€‹โ€‹(red, green, blue and alpha). Then I created 3 float: float hue, float saturation, float brightness. Then you just do:

 hue = yourcolor.Gethue; saturation = yourcolor.GetSaturation; brightness = yourcolor.GetBrightness; 

This whole batch is as follows:

 Bitmap bmp = (Bitmap)pictureBox1.Image.Clone(); paintcolor = bmp.GetPixel(eX, eY); float hue; float saturation; float brightness; hue = paintcolor.GetHue(); saturation = paintcolor.GetSaturation(); brightness = paintcolor.GetBrightness(); 

If you want to display them on a shortcut, simply do:

 yourlabelname.Text = hue.ToString; yourlabelname.Text = saturation.ToString; yourlabelname.Text = brightness.ToString; 

Here you go, now you have RGB values โ€‹โ€‹in HSV values โ€‹โ€‹:)

Hope this helps

+1
Jul 02 '16 at 11:34
source share



All Articles