2014-09-08 18:40:04 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2021-07-29 15:22:51 +12:00
|
|
|
|
Copyright (c) 2007-2021 ShareX Team
|
2014-09-08 18:40:04 +12:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
2020-07-06 11:23:59 +12:00
|
|
|
|
using Newtonsoft.Json;
|
2020-07-29 00:55:35 +12:00
|
|
|
|
using System;
|
2014-09-08 18:40:04 +12:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-09-08 18:40:04 +12:00
|
|
|
|
{
|
|
|
|
|
public class GradientInfo
|
|
|
|
|
{
|
|
|
|
|
[DefaultValue(LinearGradientMode.Vertical)]
|
|
|
|
|
public LinearGradientMode Type { get; set; }
|
|
|
|
|
|
|
|
|
|
public List<GradientStop> Colors { get; set; }
|
|
|
|
|
|
2020-07-06 11:23:59 +12:00
|
|
|
|
[JsonIgnore]
|
2020-06-09 07:53:46 +12:00
|
|
|
|
public bool IsValid => Colors != null && Colors.Count > 0;
|
2014-09-08 18:40:04 +12:00
|
|
|
|
|
2020-07-20 15:25:38 +12:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
public bool IsVisible => IsValid && Colors.Any(x => x.Color.A > 0);
|
|
|
|
|
|
2020-08-09 14:37:04 +12:00
|
|
|
|
[JsonIgnore]
|
2020-09-30 10:52:20 +13:00
|
|
|
|
public bool IsTransparent => IsValid && Colors.Any(x => x.Color.IsTransparent());
|
2020-08-09 14:37:04 +12:00
|
|
|
|
|
2014-09-08 18:40:04 +12:00
|
|
|
|
public GradientInfo()
|
|
|
|
|
{
|
|
|
|
|
Type = LinearGradientMode.Vertical;
|
|
|
|
|
Colors = new List<GradientStop>();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 03:45:34 +12:00
|
|
|
|
public GradientInfo(params GradientStop[] colors) : this()
|
|
|
|
|
{
|
|
|
|
|
Colors = colors.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 23:14:40 +12:00
|
|
|
|
public GradientInfo(params Color[] colors) : this()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < colors.Length; i++)
|
|
|
|
|
{
|
2020-07-29 00:55:35 +12:00
|
|
|
|
Colors.Add(new GradientStop(colors[i], (int)Math.Round(100f / (colors.Length - 1) * i)));
|
2020-07-28 23:14:40 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-23 03:44:24 +12:00
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
Colors.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 08:55:36 +12:00
|
|
|
|
public void Sort()
|
|
|
|
|
{
|
|
|
|
|
Colors.Sort((x, y) => x.Location.CompareTo(y.Location));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 09:18:21 +12:00
|
|
|
|
public void Reverse()
|
|
|
|
|
{
|
|
|
|
|
Colors.Reverse();
|
|
|
|
|
|
|
|
|
|
foreach (GradientStop color in Colors)
|
|
|
|
|
{
|
|
|
|
|
color.Location = 100 - color.Location;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-15 19:05:55 +12:00
|
|
|
|
public ColorBlend GetColorBlend()
|
|
|
|
|
{
|
2020-06-09 07:32:20 +12:00
|
|
|
|
List<GradientStop> colors = new List<GradientStop>(Colors.OrderBy(x => x.Location));
|
|
|
|
|
|
|
|
|
|
if (!colors.Any(x => x.Location == 0))
|
|
|
|
|
{
|
|
|
|
|
colors.Insert(0, new GradientStop(colors[0].Color, 0f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!colors.Any(x => x.Location == 100))
|
|
|
|
|
{
|
|
|
|
|
colors.Add(new GradientStop(colors[colors.Count - 1].Color, 100f));
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-15 19:05:55 +12:00
|
|
|
|
ColorBlend colorBlend = new ColorBlend();
|
2020-06-09 07:32:20 +12:00
|
|
|
|
colorBlend.Colors = colors.Select(x => x.Color).ToArray();
|
|
|
|
|
colorBlend.Positions = colors.Select(x => x.Location / 100).ToArray();
|
2019-09-15 19:05:55 +12:00
|
|
|
|
return colorBlend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LinearGradientBrush GetGradientBrush(Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Transparent, Color.Transparent, Type);
|
|
|
|
|
brush.InterpolationColors = GetColorBlend();
|
|
|
|
|
return brush;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 14:11:15 +12:00
|
|
|
|
public void Draw(Graphics g, Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
if (IsValid)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (LinearGradientBrush brush = GetGradientBrush(new Rectangle(0, 0, rect.Width, rect.Height)))
|
|
|
|
|
{
|
|
|
|
|
g.FillRectangle(brush, rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 14:46:36 +12:00
|
|
|
|
public void Draw(Image img)
|
|
|
|
|
{
|
|
|
|
|
if (IsValid)
|
|
|
|
|
{
|
|
|
|
|
using (Graphics g = Graphics.FromImage(img))
|
|
|
|
|
{
|
|
|
|
|
Draw(g, new Rectangle(0, 0, img.Width, img.Height));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 14:37:04 +12:00
|
|
|
|
public Bitmap CreateGradientPreview(int width, int height, bool border = false, bool checkers = false)
|
2020-07-28 03:45:34 +12:00
|
|
|
|
{
|
|
|
|
|
Bitmap bmp = new Bitmap(width, height);
|
|
|
|
|
Rectangle rect = new Rectangle(0, 0, width, height);
|
|
|
|
|
|
|
|
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
|
|
|
|
{
|
2020-08-09 14:37:04 +12:00
|
|
|
|
if (checkers && IsTransparent)
|
|
|
|
|
{
|
|
|
|
|
using (Image checker = ImageHelpers.CreateCheckerPattern())
|
|
|
|
|
using (Brush checkerBrush = new TextureBrush(checker, WrapMode.Tile))
|
|
|
|
|
{
|
|
|
|
|
g.FillRectangle(checkerBrush, rect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 14:11:15 +12:00
|
|
|
|
Draw(g, rect);
|
2020-08-09 14:37:04 +12:00
|
|
|
|
|
|
|
|
|
if (border)
|
|
|
|
|
{
|
|
|
|
|
g.DrawRectangleProper(Pens.Black, rect);
|
|
|
|
|
}
|
2020-07-28 03:45:34 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return bmp;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-08 18:40:04 +12:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return "Gradient";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|