How to determine the color hue? - c#

let's say that I have a HEX color #a08040. How can I determine in C#, if that color is the one of the many hues of the Brown color ?
In the other words, I have 4 colors: Brown, Red, Black, Gray.
How to determine what color hue is my HEX color?

If you are using WinForms, the Color.GetHue method will do exactly what you want.
Edit
Unfortunately, there is no WPF equivalent to GetHue, you'll have to convert to a WinForms Color if you want to use GetHue from WPF. You could also compute the Hue yourself, if you're feeling frisky... Wikipedia has the formula you'd need to use.

Related

Is it neccessary to change the RGB color to HSL in order to change specific range of color?

I wanted to change specific range of color to another color.
For instance, i want to change all shades of yellow color to white.
The main problem here is defining what "all shades of yellow" mean. Working in HSL space this is fairly easy, define a range of Hues that you consider to be "yellow", and apply your change to all of these colors. For example:
Working in RGB space you will need to consider a RGB cube and define part of this volume as "yellow" before applying your transformation. This will likely be more difficult.
You might also want to keep part of the original color, for example keeping the Lightness-part of the color and only changing the saturation, so that a dark murky yellow will remain dark. You might also want to define a "soft border", and apply only part of your transformation, to avoid any sharp transitions between "yellow" and "not yellow".

Display a color from a position on a gradient

Using the Kinect, I'm taking height measurements of both hands from a table, and displaying a color on the screen that's determined by the proportion of the heights. For example, if handProportion = .5, the screen would be green. At any other proportion, the screen is red. However, I need green to transition smoothly into red.
My first instinct is color bitshifting, but that sounds complicated. How can I write a function that displays a color from a gradient based on a position value in that gradient?
Thank you!
It seems like you need to interpolate between two colors given a ratio:
Color Interpolation Between 3 Colors in .NET

How can I tell a RGB color is basically BLUE? in c#

I have a set of images of the sky, some with clouds, and some without.
Some are clear blue skies, some partly cloudy, some murky with white and grey clouds.
How can I determine that a particular RGB value is, more or less, blue?
Any shade of blue is 'blue' - light blue, dark blue, navy blue, sky blue - these are all blue.
I want to reduce the colors in the image down to 16. Then I want to make a histogram of White, Silver, Grey, Blue and 'everything else'.
How do I know from the RGB value what is a blue or a silver, a white or a grey, or something else?
RGB isn't the best color space to work in for this sort of thing. A color space such as HSV (also known as HSB) provides a more intuitive and descriptive model for a given color.
So, to get what you are after you can use the Hue of a given color. If you are dealing with a System.Drawing.Color structure you can use the GetHue() method which returns a hue value in degrees. Given a Hue wheel like the following you can define an upper and lower threshold (in degrees) for what Blue values you will accept.
Image processing and Color theory in general are far from trivial subjects. I would suggest that you find the simplest method that meets your requirements. This may be enough for you, or maybe not. If not then perhaps you can narrow the question down a bit for me.
Also realize that you will still need some threshold for the Brightness and Saturation components to ensure that you aren't actually dealing with black, white, or gray.
If the red and green values are close to the same, and if the blue value is greater than the red and green values, the color is going to be in the blue range. If all three values are about the same, you've got some version of grey, and if they're all close to their maximum the color will look white. It's impossible to be much more precise than that -- you'll need to just look at a color picker and figure out which region looks "blue" to you.
Maybe build a table with the RGB values for each of the 16 colors you want, then compare each pixel RGB value to each of the values in your table.
To compare make a substraction like R1-R2 G1-G2 B1-B2 then sum up the differences and convert it to the color that is less different.
Related question: Compare RGB colors in c#

How to resolve anti-aliasing when changing color of an image in C#?

I am processing images to change their color from black to red, blue, green etc based on the requirement. I use SetPixel methods to change color of each pixel of the image from black to say red.
It works mostly fine except the borders and some curves within the image. Let's say I've circled image filled with black color. Circled image color is changed but still when I zoom, I can see blackish dots around border which is not completely replaced with red color. I tried to dig around and found that it has something to do with anti-aliasing.
Has anything faced similar problem or have thoughts/suggestions on how to fix this issue?
Many thanks in advnace for your help!
Regards,
Tanush
It can be related with anti-aliasing. Anti-aliasing essence is that the more pixel is closer to the edge (boundary of something) the more pixel color is blended with background color (or we can say that it is more 'transparent').
So the problem may be that you need not only to replace source color to destination color, but also pixels which were blended from source color to background color.
To achieve this you need:
1) Run edge detection algorithm of some kind - it may be simple or advanced as you want.
2) If pixel is near edge and pixel is near other pixel of your source color, then calculate it's opacity (1-transparency) factor- which will be
opacity = (pixel_color-background_color)/(source_color-background_color)
3) Now calculate your color to which you must replace current anti-aliased pixel:
new_color = background_color * (1-opacity) + opacity * target_color
And put this new_color instead of antialiased pixel.
In summary:
You need to detect antialiased pixels and replace them with your version of antialiased pixels.
Hardest part of algorithm is detection of antialiased pixels - because you can't be sure that you found all edge pixels with 100% probability. Also you can't be sure was pixel antialiased or was just made initially of such color). Because of this you may get some color noise in final product. But in any case it should be better than just sit and wait :)
good luck

C# Algorithm to Tint a Color a Certain Percent

I have a color and I want get a tint of that color by a certain percent. So 100% is the color itself, 90% is a slightly lighter color, etc. Basically, it's like adding 10% of opacity to the color, if the color is on a white background. I need to convert the color into a hex HTML color value, so I don't want transparency.
Is there an algorithm to get a tint of the System.Drawing.Color RGB value?
Yep there are a lot of different ways to do this. One way would be to convert from RGB to HSB (Hue, Saturation, Brightness) and then crank the brightness down some percentage.

Categories