// Convert from HSL to RGB color space public static void HSL2RGB(HSL hsl, RGB rgb) { if (hsl.Saturation == 0) { // gray values rgb.Red = rgb.Green = rgb.Blue = (byte)(hsl.Luminance * 255); } else { double v1, v2; double hue = (double)hsl.Hue / 360; v2 = (hsl.Luminance < 0.5) ? (hsl.Luminance * (1 + hsl.Saturation)) : ((hsl.Luminance + hsl.Saturation) - (hsl.Luminance * hsl.Saturation)); v1 = 2 * hsl.Luminance - v2; rgb.Red = (byte)(255 * Hue_2_RGB(v1, v2, hue + (1.0 / 3))); rgb.Green = (byte)(255 * Hue_2_RGB(v1, v2, hue)); rgb.Blue = (byte)(255 * Hue_2_RGB(v1, v2, hue - (1.0 / 3))); } }
// Process the filter private unsafe void ProcessFilter(BitmapData data) { int width = data.Width; int height = data.Height; RGB rgb = new RGB( ); HSL hsl = new HSL( ); int offset = data.Stride - width * 3; // do the job byte *ptr = (byte *)data.Scan0.ToPointer( ); // for each row for (int y = 0; y < height; y++) { // for each pixel for (int x = 0; x < width; x++, ptr += 3) { rgb.Red = ptr[RGB.R]; rgb.Green = ptr[RGB.G]; rgb.Blue = ptr[RGB.B]; // convert to HSL ColorConverter.RGB2HSL(rgb, hsl); // modify Hue value hsl.Hue = hue; // convert back to RGB ColorConverter.HSL2RGB(hsl, rgb); ptr[RGB.R] = rgb.Red; ptr[RGB.G] = rgb.Green; ptr[RGB.B] = rgb.Blue; } ptr += offset; } }