📄 hslfilter.cs
字号:
namespace Imps.Client.Utils
{
using Imps.Client.Utils.Win32;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
public class HSLFilter : BasicFilter
{
private double _hue;
private double _lightness;
private double _saturation;
private int adjustHueValue = 1;
private double adjustLuminanceValue = 0.1;
private double adjustSaturationValue = 0.1;
private DoubleRange inLuminance = new DoubleRange(0, 1);
private DoubleRange inSaturation = new DoubleRange(0, 1);
private DoubleRange outLuminance = new DoubleRange(0, 1);
private DoubleRange outSaturation = new DoubleRange(0, 1);
public virtual Bitmap Apply(Bitmap image)
{
BitmapData imageData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
Bitmap bitmap = this.Apply(imageData);
image.UnlockBits(imageData);
return bitmap;
}
public virtual Bitmap Apply(BitmapData imageData)
{
if (imageData.PixelFormat != PixelFormat.Format24bppRgb)
{
throw new ArgumentException();
}
int width = imageData.Width;
int height = imageData.Height;
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
BitmapData data = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
Imps.Client.Utils.Win32.NativeMethods.memcpy(data.Scan0, imageData.Scan0, imageData.Stride * height);
this.ProcessFilter(data);
bitmap.UnlockBits(data);
return bitmap;
}
public static void ChangeImageColor(ref Bitmap bm, int hue, double sat, double lum, RGB transColor)
{
if ((bm != null) && (transColor != null))
{
HSLFilter filter = new HSLFilter();
filter.Hue = hue;
filter.Saturation = sat;
filter.Lightness = lum;
filter.BackgroundColor = transColor.Color;
bm = filter.Apply(bm);
}
}
public override Image ExecuteFilter(Image img)
{
PixelFormat pixelFormat = img.PixelFormat;
if (pixelFormat <= PixelFormat.Format32bppRgb)
{
switch (pixelFormat)
{
case PixelFormat.Format24bppRgb:
case PixelFormat.Format32bppRgb:
goto Label_003D;
}
return img;
}
if (pixelFormat != PixelFormat.Format16bppGrayScale)
{
if (pixelFormat == PixelFormat.Format48bppRgb)
{
return img;
}
if (pixelFormat != PixelFormat.Format32bppArgb)
{
return img;
}
}
else
{
return img;
}
Label_003D:
return this.ExecuteRgb8(img);
}
private Image ExecuteRgb8(Image img)
{
Bitmap bitmap = new Bitmap(img);
bitmap.SetResolution(img.HorizontalResolution, img.VerticalResolution);
BitmapData bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, img.PixelFormat);
int num = Image.GetPixelFormatSize(img.PixelFormat) / 8;
IntPtr source = bitmapdata.Scan0;
int length = bitmapdata.Stride * bitmap.Height;
byte[] destination = new byte[length];
double num20 = (127 * this._saturation) / 100;
double num21 = (127 * this._lightness) / 100;
Marshal.Copy(source, destination, 0, length);
for (int i = 0; i < bitmap.Height; i++)
{
for (int j = 0; j < bitmap.Width; j++)
{
double num7;
double num8;
int index = (i * bitmapdata.Stride) + (j * num);
double num4 = destination[index + 2];
double num5 = destination[index + 1];
double num6 = destination[index];
double num11 = num4;
if (num5 < num11)
{
num11 = num5;
}
if (num6 < num11)
{
num11 = num6;
}
double num12 = num4;
double num15 = 0;
double num16 = num5 - num6;
if (num5 > num12)
{
num12 = num5;
num15 = 120;
num16 = num6 - num4;
}
if (num6 > num12)
{
num12 = num6;
num15 = 240;
num16 = num4 - num5;
}
double num13 = num12 - num11;
double num14 = num12 + num11;
double num9 = 0.5 * num14;
if (num13 == 0)
{
num7 = 0;
num8 = 0;
}
else
{
if (num9 < 127.5)
{
num8 = (255 * num13) / num14;
}
else
{
num8 = (255 * num13) / (510 - num14);
}
num7 = num15 + ((60 * num16) / num13);
if (num7 < 0)
{
num7 += 360;
}
if (num7 >= 360)
{
num7 -= 360;
}
}
num7 += this._hue;
if (num7 >= 360)
{
num7 -= 360;
}
num8 += num20;
if (num8 < 0)
{
num8 = 0;
}
if (num8 > 255)
{
num8 = 255;
}
num9 += num21;
if (num9 < 0)
{
num9 = 0;
}
if (num9 > 255)
{
num9 = 255;
}
if (num8 == 0)
{
num4 = num9;
num5 = num9;
num6 = num9;
}
else
{
double num18;
if (num9 < 127.5)
{
num18 = (0.00392156862745098 * num9) * (255 + num8);
}
else
{
num18 = (num9 + num8) - ((0.00392156862745098 * num8) * num9);
}
double num17 = (2 * num9) - num18;
double num19 = num18 - num17;
double num10 = num7 + 120;
if (num10 >= 360)
{
num10 -= 360;
}
if (num10 < 60)
{
num4 = num17 + ((num19 * num10) * 0.016666666666666666);
}
else if (num10 < 180)
{
num4 = num18;
}
else if (num10 < 240)
{
num4 = num17 + (num19 * (4 - (num10 * 0.016666666666666666)));
}
else
{
num4 = num17;
}
num10 = num7;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -