Added Threshold option

This commit is contained in:
Jaex 2021-10-10 13:17:20 +03:00
parent d806c9efd4
commit d95f5e30ad
2 changed files with 14 additions and 3 deletions

View file

@ -2325,7 +2325,7 @@ public static void SelectiveColor(Bitmap bmp, Color lightColor, Color darkColor,
} }
} }
public static void ReplaceColor(Bitmap bmp, Color sourceColor, Color targetColor, bool autoSourceColor = false) public static void ReplaceColor(Bitmap bmp, Color sourceColor, Color targetColor, bool autoSourceColor = false, int threshold = 0)
{ {
ColorBgra sourceBgra = new ColorBgra(sourceColor); ColorBgra sourceBgra = new ColorBgra(sourceColor);
ColorBgra targetBgra = new ColorBgra(targetColor); ColorBgra targetBgra = new ColorBgra(targetColor);
@ -2335,13 +2335,21 @@ public static void ReplaceColor(Bitmap bmp, Color sourceColor, Color targetColor
if (autoSourceColor) if (autoSourceColor)
{ {
sourceBgra = unsafeBitmap.GetPixel(0); sourceBgra = unsafeBitmap.GetPixel(0);
sourceColor = sourceBgra.ToColor();
} }
for (int i = 0; i < unsafeBitmap.PixelCount; i++) for (int i = 0; i < unsafeBitmap.PixelCount; i++)
{ {
ColorBgra color = unsafeBitmap.GetPixel(i); ColorBgra color = unsafeBitmap.GetPixel(i);
if (color == sourceBgra) if (threshold == 0)
{
if (color == sourceBgra)
{
unsafeBitmap.SetPixel(i, targetBgra);
}
}
else if (ColorHelpers.ColorsAreClose(color.ToColor(), sourceColor, threshold))
{ {
unsafeBitmap.SetPixel(i, targetBgra); unsafeBitmap.SetPixel(i, targetBgra);
} }

View file

@ -42,6 +42,9 @@ internal class ReplaceColor : ImageEffect
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))] [DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color TargetColor { get; set; } public Color TargetColor { get; set; }
[DefaultValue(0)]
public int Threshold { get; set; }
public ReplaceColor() public ReplaceColor()
{ {
this.ApplyDefaultPropertyValues(); this.ApplyDefaultPropertyValues();
@ -49,7 +52,7 @@ public ReplaceColor()
public override Bitmap Apply(Bitmap bmp) public override Bitmap Apply(Bitmap bmp)
{ {
ImageHelpers.ReplaceColor(bmp, SourceColor, TargetColor, AutoSourceColor); ImageHelpers.ReplaceColor(bmp, SourceColor, TargetColor, AutoSourceColor, Threshold);
return bmp; return bmp;
} }
} }