Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate Hue using ImageAttributes in C#

How can I rotate the hue of an image using GDI+'s ImageAttributes (and presumably ColorMatrix)?

Note that I want to rotate the hue, not tint the image.

EDIT: By rotating the hue, I mean that each color in the image should be shifted to a different color, as opposed to making the entire image a shade of one color.

For example,

Original:http://www.codeguru.com/img/legacy/gdi/Tinter03.jpg

Rotated: http://www.codeguru.com/img/legacy/gdi/Tinter15.jpg or http://www.codeguru.com/img/legacy/gdi/Tinter17.jpg

like image 678
SLaks Avatar asked Oct 14 '25 04:10

SLaks


2 Answers

I threw this together for this question (ZIP file with c# project linked at the bottom of the post). It does not use ImageAttributes or ColorMatrix, but it rotates the hue as you've described:

//rotate hue for a pixel
private Color CalculateHueChange(Color oldColor, float hue)
{
    HLSRGB color = new HLSRGB(
        Convert.ToByte(oldColor.R),
        Convert.ToByte(oldColor.G),
        Convert.ToByte(oldColor.B));

    float startHue = color.Hue;
    color.Hue = startHue + hue;
    return color.Color;
}
like image 116
Rex M Avatar answered Oct 16 '25 17:10

Rex M


I ended up porting QColorMatrix to C# and using its RotateHue method.

like image 45
SLaks Avatar answered Oct 16 '25 18:10

SLaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!