Added "Replace color" image effect

This commit is contained in:
Jaex 2021-10-10 13:00:26 +03:00
parent f24a303d49
commit d806c9efd4
4 changed files with 82 additions and 0 deletions

View file

@ -2325,6 +2325,30 @@ public static void SelectiveColor(Bitmap bmp, Color lightColor, Color darkColor,
}
}
public static void ReplaceColor(Bitmap bmp, Color sourceColor, Color targetColor, bool autoSourceColor = false)
{
ColorBgra sourceBgra = new ColorBgra(sourceColor);
ColorBgra targetBgra = new ColorBgra(targetColor);
using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bmp, true))
{
if (autoSourceColor)
{
sourceBgra = unsafeBitmap.GetPixel(0);
}
for (int i = 0; i < unsafeBitmap.PixelCount; i++)
{
ColorBgra color = unsafeBitmap.GetPixel(i);
if (color == sourceBgra)
{
unsafeBitmap.SetPixel(i, targetBgra);
}
}
}
}
public static Size GetImageFileDimensions(string filePath)
{
using (Bitmap bmp = LoadImage(filePath))

View file

@ -0,0 +1,56 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2021 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
namespace ShareX.ImageEffectsLib
{
[Description("Replace color")]
internal class ReplaceColor : ImageEffect
{
[DefaultValue(typeof(Color), "White"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color SourceColor { get; set; }
[DefaultValue(false)]
public bool AutoSourceColor { get; set; }
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color TargetColor { get; set; }
public ReplaceColor()
{
this.ApplyDefaultPropertyValues();
}
public override Bitmap Apply(Bitmap bmp)
{
ImageHelpers.ReplaceColor(bmp, SourceColor, TargetColor, AutoSourceColor);
return bmp;
}
}
}

View file

@ -180,6 +180,7 @@ private void AddAllEffectsToContextMenu()
typeof(Hue),
typeof(Inverse),
typeof(Polaroid),
typeof(ReplaceColor),
typeof(Saturation),
typeof(SelectiveColor),
typeof(Sepia));

View file

@ -105,6 +105,7 @@
<Compile Include="Adjustments\Alpha.cs" />
<Compile Include="Adjustments\BlackWhite.cs" />
<Compile Include="Adjustments\Polaroid.cs" />
<Compile Include="Adjustments\ReplaceColor.cs" />
<Compile Include="Adjustments\SelectiveColor.cs" />
<Compile Include="Adjustments\Sepia.cs" />
<Compile Include="Adjustments\MatrixColor.cs" />