简体   繁体   中英

C# How to recolor an image with keeping all of its structure

I want to programmatically recolor an image as seen below:

在此处输入图片说明

So how can I do that without fully painting the whole image to one solid color?

I suggest converting the RGB (Red Green Blue) color to a HSB/HSV (Hue Satuation Brightness / Hue Saturation Value) color. Then you can shift the colors by changing the Hue value. Keep Saturation and Value (Brightness) and convert it back to RGB.

You can get the H, S, V values like this:

Color color = Color.FromArgb(red, green, blue);
float h = color.GetHue();
float s = color.GetSaturation();
float v = color.GetBrightness();

There is no built-in way for the reverse transformation. You can find a C# example here: Converting HSV to RGB colour using C# . Also, don't forget to copy the alpha component containing the transparency information, if the image has transparent parts.

The hue is measured in degrees, ranging from 0.0f through 360.0f, so would change the hue like this:

float delta = 120f; // Arbitrary value in the range 0.0f through 360.0f
h = (h + delta) % 360f;

See also: HSL and HSV

Another excellent article with C# examples: Manipulating colors in .NET - Part 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM