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.
Graphics Mill supports two types of color pixel formats - RGB and CMYK. Both of them assume that each pixel consists of several channels:
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:
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:
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:
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"); }
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.
The code example for the HSL adjustment is demonstrated below:
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:
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:
using (var bitmap = new Bitmap(@"Images\1.jpg")) { bitmap.ColorAdjustment.Desaturate(); bitmap.Save(@"Images\Output\out.jpg"); }
The similar example demonstrates the Desaturate transform:
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"); }