Update ForceProportions effect

- more consistent code formatting
- better color selection
This commit is contained in:
L1Q 2020-08-05 01:49:19 +03:00
parent 2039c99909
commit 4b1a3d6229

View file

@ -27,17 +27,18 @@
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Drawing.Design;
namespace ShareX.ImageEffectsLib namespace ShareX.ImageEffectsLib
{ {
[Description("Force Proportions")]
internal class ForceProportions : ImageEffect internal class ForceProportions : ImageEffect
{ {
private int width = 1; private int width = 1;
private int height = 1; private int height = 1;
[DefaultValue(typeof(int), "1")] [DefaultValue(typeof(int), "1")]
public int Width public int ProportionalWidth
{ {
get { get {
return width; return width;
@ -48,7 +49,7 @@ public int Width
} }
} }
[DefaultValue(typeof(int), "1")] [DefaultValue(typeof(int), "1")]
public int Height public int ProportionalHeight
{ {
get get
{ {
@ -68,6 +69,7 @@ public enum ForceMethod
public ForceMethod Method { get; set; } = ForceMethod.Grow; public ForceMethod Method { get; set; } = ForceMethod.Grow;
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color GrowFillColor { get; set; } = Color.FromArgb(0); public Color GrowFillColor { get; set; } = Color.FromArgb(0);
public ForceProportions() public ForceProportions()
@ -77,33 +79,40 @@ public ForceProportions()
public override Bitmap Apply(Bitmap bmp) public override Bitmap Apply(Bitmap bmp)
{ {
float current_ratio = (float)bmp.Width / (float)bmp.Height; float current_ratio = bmp.Width / (float)bmp.Height;
float target_ratio = (float)width / (float)height; float target_ratio = width / (float)height;
bool is_target_wider = target_ratio > current_ratio; bool is_target_wider = target_ratio > current_ratio;
int target_width = bmp.Width; int target_width = bmp.Width;
int target_height = bmp.Height; int target_height = bmp.Height;
int margin_left = 0;
int margin_top = 0;
if (Method == ForceMethod.Crop) if (Method == ForceMethod.Crop)
{ {
if (is_target_wider) if (is_target_wider)
{
target_height = (int)Math.Round(bmp.Width / target_ratio); target_height = (int)Math.Round(bmp.Width / target_ratio);
margin_top = (bmp.Height - target_height) / 2;
}
else else
{
target_width = (int)Math.Round(bmp.Height * target_ratio); target_width = (int)Math.Round(bmp.Height * target_ratio);
int margin_left = is_target_wider margin_left = (bmp.Width - target_width) / 2;
? 0 }
: (bmp.Width - target_width) / 2;
int margin_top = is_target_wider
? (bmp.Height - target_height) / 2
: 0;
return ImageHelpers.CropBitmap(bmp, new Rectangle(margin_left, margin_top, target_width, target_height)); return ImageHelpers.CropBitmap(bmp, new Rectangle(margin_left, margin_top, target_width, target_height));
} }
if (Method == ForceMethod.Grow) if (Method == ForceMethod.Grow)
{ {
if (is_target_wider) if (is_target_wider)
{
target_width = (int)Math.Round(bmp.Height * target_ratio); target_width = (int)Math.Round(bmp.Height * target_ratio);
}
else else
{
target_height = (int)Math.Round(bmp.Width / target_ratio); target_height = (int)Math.Round(bmp.Width / target_ratio);
}
return ImageHelpers.ResizeImage(bmp, target_width, target_height, false, true, GrowFillColor); return ImageHelpers.ResizeImage(bmp, target_width, target_height, false, true, GrowFillColor);
} }