Use an ImageAttributes object to apply general color tones to an image in C#

apply general color tones to an image

This example uses techniques similar to those described in the post Use an ImageAttributes object to convert an image to shades of red, green, or blue in C# to apply general color tones to an image. It generalizes those techniques to adjust the tones of a picture to match a target color (similar to sepia or violet in previous posts). For example, in the picture on the right, the program is adjusting the picture to use yellow tones.

Use the scroll bars to select a target color. Moving the image’s colors towards the target color may change the image’s overall brightness, so the program also includes a brightness scroll bar you can use to adjust the result. (There may be a way to calculate an adjustment that will leave the brightness unchanged, but this solution seemed easier.)

The following code shows how this program creates its ColorMatrix object.

float scale = scrBright.Value / 128f;

float r = color.R / 255f * scale;
float g = color.G / 255f * scale;
float b = color.B / 255f * scale;

// Make the ColorMatrix.
ColorMatrix cm = new ColorMatrix(new float[][]
{
    new float[] {r, 0, 0, 0, 0},
    new float[] {0, g, 0, 0, 0},
    new float[] {0, 0, b, 0, 0},
    new float[] {0, 0, 0, 1, 0},
    new float[] {0, 0, 0, 0, 1}
});

The code divides the brightness values by 128 to get brightness scaling factors. The scroll bar can take values between 0 and 255, so this scale factor ranges between 0 and roughly 2.

Next the program takes the red, green, and blue components of the target color and divides them by 255 (the largest value allowed by their scroll bars) to get a value between 0 and 1. It then scales the results by the brightness scale factor.

The program plugs the resulting scale factors into the ColorMatrix. Basically this scales each pixel’s color components towards the ratios used by the target color (adjusted for brightness).

Having created the ColorMatrix object, the code creates an ImageAttributes object and uses it to draw the image. See the example Use an ImageAttributes object to adjust an image’s brightness in C# for additional details about how the drawing part works.

This method is extremely fast so you can drag the scroll bars back and forth to change the result in real time. (This gives a sense of how fast drawing with an ImageAttributes object is.)


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in graphics, image processing and tagged , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Use an ImageAttributes object to apply general color tones to an image in C#

  1. qexxixleut says:

    qexxixleut

    BLOG.CSHARPHELPER.COM: Use an ImageAttributes object to apply general color tones to an image in C#

  2. Rainbowize an image in C#

    The example Use an ImageAttributes object to apply general color tones to an image in C# uses a ColorMatrix and an ImageAttributes object to convert an image into a color scale. For example, its techniques let you convert an image to sepia tones, gray tones, or other less typical color schemes such as yellow tones. This example uses the same technique to convert parts of an image into different tones to produce a rainbow effect. The following code shows how the program colors its image. // Process the image. private void Form1_Load(object sender, …

Leave a Reply

Your email address will not be published. Required fields are marked *