How to change RGB color to HSV? In C# language. I search for very fast method without any external library.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
There's a C implementation here:
http://www.cs.rit.edu/~ncs/color/t_convert.html
Should be very straightforward to convert to C#, as almost no functions are called - just calculations.
found via Google
FIRST: make sure you have a color as a bitmap, like this:
(e is from the event handler wich picked my color!)
What I did when I had this problem a whilke ago, I first got the rgba (red, green, blue and alpha) values. Next I created 3 floats: float hue, float saturation, float brightness. Then you simply do:
The whole lot looks like this:
If you now want to display them in a label, just do:
Here you go, you now have RGB Values into HSV values :)
Hope this helps
Have you considered simply using System.Drawing namespace? For example:
Note that it's not exactly what you've asked for (see differences between HSL and HSV and the Color class does not have a conversion back from HSL/HSV but the latter is reasonably easy to add.
The EasyRGB has many color space conversions. Here is the code for the RGB->HSV conversion.
Note that
Color.GetSaturation()
andColor.GetBrightness()
return HSL values, not HSV.The following code demonstrates the difference.
The following C# code is what you want. It converts between RGB and HSV using the algorithms described on Wikipedia. The ranges are 0 - 360 for
hue
, and 0 - 1 forsaturation
orvalue
.This is the VB.net version which works fine for me ported from the C code in BlaM's post.