Code refactoring

This commit is contained in:
Jaex 2024-04-20 09:16:12 +03:00
parent 59e7450b62
commit 019c9a7dae
5 changed files with 36 additions and 29 deletions

View file

@ -260,24 +260,24 @@ public static Bitmap CropBitmap(Bitmap bmp, Rectangle rect)
return null;
}
private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize, Color cutOutBackgroundColor)
private static Bitmap ApplyCutOutEffect(Bitmap bmp, AnchorStyles effectEdge, CutOutEffectType effectType, int effectSize, Color backgroundColor)
{
switch (effectType)
{
case CutOutEffectType.None:
return bmp;
case CutOutEffectType.ZigZag:
return TornEdges(bmp, effectSize, effectSize, effectEdge, false, false, cutOutBackgroundColor);
return TornEdges(bmp, effectSize, effectSize, effectEdge, false, false, backgroundColor);
case CutOutEffectType.TornEdge:
return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false, true, cutOutBackgroundColor);
return TornEdges(bmp, effectSize, effectSize * 2, effectEdge, false, true, backgroundColor);
case CutOutEffectType.Wave:
return WavyEdges(bmp, effectSize, effectSize * 5, effectEdge, cutOutBackgroundColor);
return WavyEdges(bmp, effectSize, effectSize * 5, effectEdge, backgroundColor);
}
throw new NotImplementedException();
}
public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize, Color cutOutBackgroundColor)
public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int start, int size, CutOutEffectType effectType, int effectSize, Color backgroundColor)
{
if (bmp != null && size > 0)
{
@ -290,7 +290,7 @@ public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int
: new Rectangle(0, 0, bmp.Width, Math.Min(start, bmp.Height));
firstPart = CropBitmap(bmp, r);
AnchorStyles effectEdge = orientation == Orientation.Horizontal ? AnchorStyles.Right : AnchorStyles.Bottom;
firstPart = ApplyCutOutEffect(firstPart, effectEdge, effectType, effectSize, cutOutBackgroundColor);
firstPart = ApplyCutOutEffect(firstPart, effectEdge, effectType, effectSize, backgroundColor);
}
int cutDimension = orientation == Orientation.Horizontal ? bmp.Width : bmp.Height;
@ -302,7 +302,7 @@ public static Bitmap CutOutBitmapMiddle(Bitmap bmp, Orientation orientation, int
: new Rectangle(0, end, bmp.Width, bmp.Height - end);
secondPart = CropBitmap(bmp, r);
AnchorStyles effectEdge = orientation == Orientation.Horizontal ? AnchorStyles.Left : AnchorStyles.Top;
secondPart = ApplyCutOutEffect(secondPart, effectEdge, effectType, effectSize, cutOutBackgroundColor);
secondPart = ApplyCutOutEffect(secondPart, effectEdge, effectType, effectSize, backgroundColor);
}
if (firstPart != null && secondPart != null)
@ -1844,7 +1844,12 @@ public static void FastBoxBlur(Bitmap bmp, int radius)
}
}
public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides, Color cutOutBackgroundColor)
public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides)
{
return WavyEdges(bmp, waveDepth, waveRange, sides, Color.Transparent);
}
public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorStyles sides, Color backgroundColor)
{
if (waveDepth < 1 || waveRange < 1 || sides == AnchorStyles.None)
{
@ -1928,22 +1933,31 @@ public static Bitmap WavyEdges(Bitmap bmp, int waveDepth, int waveRange, AnchorS
}
Bitmap bmpResult = bmp.CreateEmptyBitmap();
using (bmp)
using (Graphics g = Graphics.FromImage(bmpResult))
using (TextureBrush brush = new TextureBrush(bmp))
{
if (backgroundColor.A > 0)
{
g.Clear(backgroundColor);
}
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
if (cutOutBackgroundColor.A > 0)
{
g.Clear(cutOutBackgroundColor);
}
g.FillPolygon(brush, points.ToArray());
}
return bmpResult;
}
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random, Color cutOutBackgroundColor)
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random)
{
return TornEdges(bmp, tornDepth, tornRange, sides, curvedEdges, random, Color.Transparent);
}
public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorStyles sides, bool curvedEdges, bool random, Color backgroundColor)
{
if (tornDepth < 1 || tornRange < 1 || sides == AnchorStyles.None)
{
@ -2030,12 +2044,13 @@ public static Bitmap TornEdges(Bitmap bmp, int tornDepth, int tornRange, AnchorS
using (Graphics g = Graphics.FromImage(bmpResult))
using (TextureBrush brush = new TextureBrush(bmp))
{
if (backgroundColor.A > 0)
{
g.Clear(backgroundColor);
}
g.SetHighQuality();
g.PixelOffsetMode = PixelOffsetMode.Half;
if (cutOutBackgroundColor.A > 0)
{
g.Clear(cutOutBackgroundColor);
}
Point[] fillPoints = points.Distinct().ToArray();

View file

@ -26,7 +26,6 @@
using ShareX.HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace ShareX.ImageEffectsLib
@ -46,9 +45,6 @@ internal class TornEdge : ImageEffect
[DefaultValue(true)]
public bool CurvedEdges { get; set; }
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color CutOutBackgroundColor { get; set; }
public TornEdge()
{
this.ApplyDefaultPropertyValues();
@ -56,7 +52,7 @@ public TornEdge()
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges, true, CutOutBackgroundColor);
return ImageHelpers.TornEdges(bmp, Depth, Range, Sides, CurvedEdges, true);
}
protected override string GetSummary()

View file

@ -26,7 +26,6 @@
using ShareX.HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
namespace ShareX.ImageEffectsLib
@ -48,12 +47,9 @@ public WaveEdge()
this.ApplyDefaultPropertyValues();
}
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color CutOutBackgroundColor { get; set; }
public override Bitmap Apply(Bitmap bmp)
{
return ImageHelpers.WavyEdges(bmp, Depth, Range, Sides, CutOutBackgroundColor);
return ImageHelpers.WavyEdges(bmp, Depth, Range, Sides);
}
protected override string GetSummary()

View file

@ -324,7 +324,7 @@ internal class Resources {
}
/// <summary>
/// Looks up a localized string similar to Cut out background color.
/// Looks up a localized string similar to Cut out background color....
/// </summary>
internal static string CutOutBackgroundColor {
get {

View file

@ -831,6 +831,6 @@ Would you like to save the changes before closing the image editor?</value>
<value>..\Resources\control-record-green.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="CutOutBackgroundColor" xml:space="preserve">
<value>Cut out background color</value>
<value>Cut out background color...</value>
</data>
</root>