Rotate Image Hue in c#/.NET
3 July 2009 –I am on vacation at the beach with my wife and a few friends, and was perusing Stackoverflow at the house while waiting for everyone to get ready to go out. I ran across a question about how to rotate hue in code, which I myself have wondered before but never really needed to. Here's what I started with:
- I do enough design work to know the difference between HSB (also called HSV) and RGB, so I know changing the hue is extremely easy if I have the HSB value (the "H" stands for Hue!).
- I also know GDI+ in .NET works primarily in (A)RGB. I can iterate over the pixels of a Bitmap object and Get and Set the pixel value in RGB.
- There is a mathematical way to convert an RGB value to HSB and vice-versa, but I have no idea what that is.
A few minutes on Google yielded the equations, so I slammed this together in a few minutes more. The basic workflow is:
- Load an image and an amount to shift the hue by
- Iterate over all the pixels in the image
- For each pixel, convert its RGB value to HSB, change the hue, convert back to RGB, set the pixel's new RGB value
- Save the image
Here's a sample input/output:
Here's the central code which changes the hue for a single pixel:
private Color CalculateHueChange(Color oldColor, float hue)
{
HLSRGB colorConverter = new HLSRGB(
oldColor.R,
oldColor.G,
oldColor.B);
float startHue = colorConverter.Hue;
colorConverter.Hue = startHue + hue;
return Color.FromArgb(
colorConverter.Red,
colorConverter.Green,
colorConverter.Blue);
}
Not exactly rocket science, but interesting to me and presumably the person who asked the question. Here's the source. Disclaimer: This is *not* an efficient way to do any kind of image manipulation. It is just a proof-of-concept. Cheers!
Content available under a Creative Commons license.
Site code and design may not be reproduced.

