Color Adjustment

All the previously discussed algorithms work with the intensity values of pixels. They modify the tone features of the bitmap. However sometimes it is necessary to work with the pixel's colors rather than their tone characteristics. Let's examine approaches that are used in Graphics Mill to accomplish this task.

Channel Balance

Graphics Mill supports two types of color pixel formats - RGB and CMYK. Both of them assume that each pixel consists of several channels:

  • RGB pixels consist of three values (channels) - red, green, and blue.
  • CMYK pixels consist of four values (channels) - cyan, magenta, yellow, and black.

It is obvious that if you modify the value of only one channel, the colors of all pixels will be changed. The results will be predictable, e.g. if you increase the value of the red channel of an RGB pixel, the color will become more reddish, etc.

The method ChannelBalance(Single[], Single[]) of the ColorAdjustmentProvider class (as well as an appropriate ChannelBalance transform) uses this rule. It takes two arrays as arguments that both modify bitmap channels:

  • The first element of the first array will be added to the first channel, the value of the second element will be added to the second channel, etc.
  • Each channel of every pixel will be multiplied by a corresponding element from the second array passed as the argument.

Values in both arrays are specified in the range [-1, 1] (they will be normalized depending on the number of bits per channel).

Here is a code example which demonstrates this:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
{
    bitmap.ColorAdjustment.ChannelBalance(new Single[3] { 0.2F, -0.1F, 0.05F }, new Single[3] { 1.0F, 1.0F, 1.0F });

    bitmap.Save(@"Images\Output\out.jpg");
}

The same result using the ChannelBalance transform class:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var channelBalance = new ChannelBalance())
{
    channelBalance.Addends = new Single[3] { 0.0F, 0.0F, 0.0F };
    channelBalance.Multipliers = new Single[3] { 1.0F, 1.0F, 1.0F };

    using (var newbitmap = channelBalance.Apply(bitmap))
        newbitmap.Save(@"Images\Output\out.jpg");
}

Hue-Saturation-Lightness

Modifying the RGB or CMYK channels is often inconvenient. It allows the user to predict whether the color will be more bluish or less reddish, but it is hard to foretell whether the color will be lighter or less saturated.

There is an alternative color adjustment method available that utilizes hue, saturation, and lightness, which are more familiar parameters for modifying the color. Each color is converted into HSL color space (HSL stands for Hue-Saturation-Lightness), these modifiers are added to the hue, saturation, and lightness channels, and then pixels are converted back to the original color space.

The ColorAdjustmentProvider provides a method named AdjustHsl(Single, Single, Single) (it has a sibling AdjustHsl transform class). These take three modifiers as parameters - hue, saturation, and lightness.

  • To specify the tint of the color you should change the value of the hue parameter. This float value is specified in the range [-1; 1]. A zero value causes no changes. Changing the hue value allows for shifting image colors from red through yellow, green, and blue.
  • The second parameter is saturation, which modifies the depth of color. This amount is also specified in the range [-1; 1]. If the amount is zero, it does not change an image at all. If the amount more than 0, the image becomes more saturated, and vice versa, if it is less than 0, the image becomes less saturated (amount = -1 makes an image completely discolored).
  • To change the brightness of the color you should specify the lightness parameter. This parameter is also a float value specified in the range [-1; 1]. The bigger the value, the lighter the image. The value=1 makes an image completely white, and the value=-1 makes the image completely dark.

The code example for the HSL adjustment is demonstrated below:

C#
using (var bitmap = new Bitmap(@"Images\balloons.jpg"))
{
    bitmap.ColorAdjustment.AdjustHsl(0.3F, 0.2F, 0.05F);

    bitmap.Save(@"Images\Output\out5.jpg");
}

The same result using the AdjustHsl transform class:

C#
using (var bitmap = new Bitmap(@"Images\in.jpg"))
using (var hsl = new AdjustHsl())
{
    hsl.Hue = 0.1F;
    hsl.Saturation = -0.2F;
    hsl.Lightness = 0.1F;

    using (var newbitmap = hsl.Apply(bitmap))
        newbitmap.Save(@"Images\Output\out.jpg");
}

Graphics Mill also provides a way to discolor an image using the Desaturate() method (or the corresponding Desaturate transform). This method works faster than AdjustHsl(Single, Single, Single) or converting the image to the grayscale pixel format and then back to the original one.

The following code example demonstrates this:

C#
using (var bitmap = new Bitmap(@"Images\1.jpg"))
{
    bitmap.ColorAdjustment.Desaturate();
    bitmap.Save(@"Images\Output\out.jpg");
}

The similar example demonstrates the Desaturate transform:

C#
using (var bitmap = new Bitmap(@"Images\1.jpg"))
using (var desaturate = new Desaturate())
{
    using (var newbitmap = desaturate.Apply(bitmap))
        newbitmap.Save(@"Images\Output\out.jpg"); 
}

See Also

Reference

Manual