Added custom gradient support to Image effects -> draw text

This commit is contained in:
Jaex 2014-09-08 09:40:04 +03:00
parent a76926cf58
commit 5ae9ba42e9
21 changed files with 751 additions and 42 deletions

View file

@ -1,6 +1,6 @@
namespace HelpersLib
{
partial class DialogColor
partial class ColorPickerForm
{
/// <summary>
/// Required designer variable.

View file

@ -29,7 +29,7 @@ You should have received a copy of the GNU General Public License
namespace HelpersLib
{
public partial class DialogColor : Form
public partial class ColorPickerForm : Form
{
public MyColor NewColor { get; protected set; }
public MyColor OldColor { get; private set; }
@ -37,12 +37,12 @@ public partial class DialogColor : Form
private bool oldColorExist;
private bool controlChangingColor;
public DialogColor()
public ColorPickerForm()
: this(Color.Empty)
{
}
public DialogColor(Color currentColor)
public ColorPickerForm(Color currentColor)
{
NewColor = Color.Red;
Initialize(currentColor);
@ -50,7 +50,7 @@ public DialogColor(Color currentColor)
public static Color GetColor(Color currentColor)
{
using (DialogColor dialog = new DialogColor(currentColor))
using (ColorPickerForm dialog = new ColorPickerForm(currentColor))
{
dialog.rbSaturation.Checked = true;

View file

@ -0,0 +1,82 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-2014 ShareX Developers
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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
namespace HelpersLib
{
public class GradientInfo
{
[DefaultValue(LinearGradientMode.Vertical)]
public LinearGradientMode Type { get; set; }
public List<GradientStop> Colors { get; set; }
public bool IsValid
{
get
{
return Colors != null && Colors.Count > 1 && Colors[0].Location == 0f && Colors[Colors.Count - 1].Location == 100f;
}
}
public GradientInfo()
{
Type = LinearGradientMode.Vertical;
Colors = new List<GradientStop>();
}
public void Draw(Graphics g, Rectangle rect)
{
if (IsValid)
{
try
{
using (LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, rect.Width, rect.Height), Color.Black, Color.Black, Type))
{
ColorBlend colorBlend = new ColorBlend();
IEnumerable<GradientStop> gradient = Colors.OrderBy(x => x.Location);
colorBlend.Colors = gradient.Select(x => x.Color).ToArray();
colorBlend.Positions = gradient.Select(x => x.Location / 100).ToArray();
brush.InterpolationColors = colorBlend;
g.FillRectangle(brush, rect);
}
}
catch { }
}
}
public override string ToString()
{
return "Gradient";
}
}
}

View file

@ -0,0 +1,220 @@
namespace HelpersLib
{
partial class GradientPickerForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lvGradientPoints = new System.Windows.Forms.ListView();
this.chLocation = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.btnAdd = new System.Windows.Forms.Button();
this.btnRemove = new System.Windows.Forms.Button();
this.nudLocation = new System.Windows.Forms.NumericUpDown();
this.lblLocation = new System.Windows.Forms.Label();
this.cbGradientType = new System.Windows.Forms.ComboBox();
this.lblGradientType = new System.Windows.Forms.Label();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.pbPreview = new System.Windows.Forms.PictureBox();
this.lblPreview = new System.Windows.Forms.Label();
this.cbtnCurrentColor = new HelpersLib.ColorButton();
((System.ComponentModel.ISupportInitialize)(this.nudLocation)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pbPreview)).BeginInit();
this.SuspendLayout();
//
// lvGradientPoints
//
this.lvGradientPoints.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.chLocation});
this.lvGradientPoints.FullRowSelect = true;
this.lvGradientPoints.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
this.lvGradientPoints.HideSelection = false;
this.lvGradientPoints.Location = new System.Drawing.Point(128, 112);
this.lvGradientPoints.Name = "lvGradientPoints";
this.lvGradientPoints.Size = new System.Drawing.Size(80, 160);
this.lvGradientPoints.TabIndex = 1;
this.lvGradientPoints.UseCompatibleStateImageBehavior = false;
this.lvGradientPoints.View = System.Windows.Forms.View.Details;
this.lvGradientPoints.SelectedIndexChanged += new System.EventHandler(this.lvGradientPoints_SelectedIndexChanged);
this.lvGradientPoints.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.lvGradientPoints_MouseDoubleClick);
//
// chLocation
//
this.chLocation.Text = "Location";
this.chLocation.Width = 76;
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(8, 112);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(112, 23);
this.btnAdd.TabIndex = 3;
this.btnAdd.Text = "Add";
this.btnAdd.UseVisualStyleBackColor = true;
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnRemove
//
this.btnRemove.Location = new System.Drawing.Point(8, 136);
this.btnRemove.Name = "btnRemove";
this.btnRemove.Size = new System.Drawing.Size(112, 23);
this.btnRemove.TabIndex = 4;
this.btnRemove.Text = "Remove";
this.btnRemove.UseVisualStyleBackColor = true;
this.btnRemove.Click += new System.EventHandler(this.btnRemove_Click);
//
// nudLocation
//
this.nudLocation.DecimalPlaces = 2;
this.nudLocation.Location = new System.Drawing.Point(64, 188);
this.nudLocation.Name = "nudLocation";
this.nudLocation.Size = new System.Drawing.Size(56, 20);
this.nudLocation.TabIndex = 5;
this.nudLocation.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.nudLocation.ValueChanged += new System.EventHandler(this.nudLocation_ValueChanged);
//
// lblLocation
//
this.lblLocation.AutoSize = true;
this.lblLocation.Location = new System.Drawing.Point(8, 192);
this.lblLocation.Name = "lblLocation";
this.lblLocation.Size = new System.Drawing.Size(51, 13);
this.lblLocation.TabIndex = 6;
this.lblLocation.Text = "Location:";
//
// cbGradientType
//
this.cbGradientType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbGradientType.FormattingEnabled = true;
this.cbGradientType.Location = new System.Drawing.Point(88, 84);
this.cbGradientType.Name = "cbGradientType";
this.cbGradientType.Size = new System.Drawing.Size(120, 21);
this.cbGradientType.TabIndex = 7;
this.cbGradientType.SelectedIndexChanged += new System.EventHandler(this.cbGradientType_SelectedIndexChanged);
//
// lblGradientType
//
this.lblGradientType.AutoSize = true;
this.lblGradientType.Location = new System.Drawing.Point(8, 88);
this.lblGradientType.Name = "lblGradientType";
this.lblGradientType.Size = new System.Drawing.Size(73, 13);
this.lblGradientType.TabIndex = 8;
this.lblGradientType.Text = "Gradient type:";
//
// btnOK
//
this.btnOK.Location = new System.Drawing.Point(8, 280);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(96, 23);
this.btnOK.TabIndex = 9;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(112, 280);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(96, 23);
this.btnCancel.TabIndex = 10;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// pbPreview
//
this.pbPreview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pbPreview.Location = new System.Drawing.Point(8, 26);
this.pbPreview.Name = "pbPreview";
this.pbPreview.Size = new System.Drawing.Size(200, 50);
this.pbPreview.TabIndex = 11;
this.pbPreview.TabStop = false;
//
// lblPreview
//
this.lblPreview.AutoSize = true;
this.lblPreview.Location = new System.Drawing.Point(8, 8);
this.lblPreview.Name = "lblPreview";
this.lblPreview.Size = new System.Drawing.Size(48, 13);
this.lblPreview.TabIndex = 12;
this.lblPreview.Text = "Preview:";
//
// cbtnCurrentColor
//
this.cbtnCurrentColor.Color = System.Drawing.Color.White;
this.cbtnCurrentColor.Location = new System.Drawing.Point(8, 160);
this.cbtnCurrentColor.Name = "cbtnCurrentColor";
this.cbtnCurrentColor.Size = new System.Drawing.Size(112, 24);
this.cbtnCurrentColor.TabIndex = 2;
this.cbtnCurrentColor.Text = "Color";
this.cbtnCurrentColor.UseVisualStyleBackColor = true;
this.cbtnCurrentColor.ColorChanged += new HelpersLib.ColorButton.ColorChangedEventHandler(this.cbtnCurrentColor_ColorChanged);
//
// GradientPickerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(219, 311);
this.Controls.Add(this.pbPreview);
this.Controls.Add(this.lblPreview);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.lblGradientType);
this.Controls.Add(this.cbGradientType);
this.Controls.Add(this.lblLocation);
this.Controls.Add(this.nudLocation);
this.Controls.Add(this.btnRemove);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.cbtnCurrentColor);
this.Controls.Add(this.lvGradientPoints);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "GradientPickerForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Gradient";
((System.ComponentModel.ISupportInitialize)(this.nudLocation)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pbPreview)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListView lvGradientPoints;
private System.Windows.Forms.ColumnHeader chLocation;
private ColorButton cbtnCurrentColor;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnRemove;
private System.Windows.Forms.NumericUpDown nudLocation;
private System.Windows.Forms.Label lblLocation;
private System.Windows.Forms.ComboBox cbGradientType;
private System.Windows.Forms.Label lblGradientType;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.PictureBox pbPreview;
private System.Windows.Forms.Label lblPreview;
}
}

View file

@ -0,0 +1,171 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-2014 ShareX Developers
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 System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HelpersLib
{
public partial class GradientPickerForm : Form
{
public GradientInfo Gradient { get; set; }
private bool isReady;
public GradientPickerForm(GradientInfo gradient)
{
Gradient = gradient;
InitializeComponent();
Icon = ShareXResources.Icon;
cbGradientType.Items.AddRange(Helpers.GetEnumNamesProper<LinearGradientMode>());
cbGradientType.SelectedIndex = (int)Gradient.Type;
foreach (GradientStop gradientStop in Gradient.Colors)
{
AddGradientStop(gradientStop);
}
isReady = true;
UpdatePreview();
}
private void UpdatePreview()
{
if (isReady)
{
Bitmap bmp = new Bitmap(pbPreview.ClientRectangle.Width, pbPreview.ClientRectangle.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
Gradient.Draw(g, new Rectangle(0, 0, bmp.Width, bmp.Height));
}
if (pbPreview.Image != null)
{
pbPreview.Image.Dispose();
}
pbPreview.Image = bmp;
}
}
private void cbGradientType_SelectedIndexChanged(object sender, EventArgs e)
{
Gradient.Type = (LinearGradientMode)cbGradientType.SelectedIndex;
UpdatePreview();
}
private void AddGradientStop(GradientStop gradientStop)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = string.Format(" {0:0.##}%", gradientStop.Location);
lvi.BackColor = gradientStop.Color;
lvi.ForeColor = ColorHelpers.VisibleTextColor(gradientStop.Color);
lvi.Tag = gradientStop;
lvGradientPoints.Items.Add(lvi);
}
private void btnAdd_Click(object sender, EventArgs e)
{
Color color = cbtnCurrentColor.Color;
float offset = (float)(nudLocation.Value / 100);
GradientStop gradientStop = new GradientStop(color, offset);
Gradient.Colors.Add(gradientStop);
AddGradientStop(gradientStop);
UpdatePreview();
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (lvGradientPoints.SelectedItems.Count > 0)
{
ListViewItem lvi = lvGradientPoints.SelectedItems[0];
GradientStop gradientStop = (GradientStop)lvi.Tag;
Gradient.Colors.Remove(gradientStop);
lvGradientPoints.Items.Remove(lvi);
UpdatePreview();
}
}
private void cbtnCurrentColor_ColorChanged(Color color)
{
if (lvGradientPoints.SelectedItems.Count > 0)
{
ListViewItem lvi = lvGradientPoints.SelectedItems[0];
GradientStop gradientStop = (GradientStop)lvi.Tag;
gradientStop.Color = color;
lvi.BackColor = gradientStop.Color;
lvi.ForeColor = ColorHelpers.VisibleTextColor(gradientStop.Color);
UpdatePreview();
}
}
private void nudLocation_ValueChanged(object sender, EventArgs e)
{
if (lvGradientPoints.SelectedItems.Count > 0)
{
ListViewItem lvi = lvGradientPoints.SelectedItems[0];
GradientStop gradientStop = (GradientStop)lvi.Tag;
gradientStop.Location = (float)nudLocation.Value;
lvi.Text = string.Format(" {0:0.##}%", gradientStop.Location);
UpdatePreview();
}
}
private void lvGradientPoints_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvGradientPoints.SelectedItems.Count > 0)
{
ListViewItem lvi = lvGradientPoints.SelectedItems[0];
GradientStop gradientStop = (GradientStop)lvi.Tag;
cbtnCurrentColor.Color = gradientStop.Color;
nudLocation.Value = (decimal)gradientStop.Location;
}
}
private void lvGradientPoints_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && lvGradientPoints.SelectedItems.Count > 0)
{
cbtnCurrentColor.ShowColorDialog();
}
}
private void btnOK_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
}
}

View file

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -28,25 +28,25 @@ You should have received a copy of the GNU General Public License
using System.Drawing;
using System.Drawing.Design;
namespace ImageEffectsLib
namespace HelpersLib
{
public class GradientStop
{
[DefaultValue(typeof(Color), "Black"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color Color { get; set; }
private float offset;
private float location;
[DefaultValue(0f)]
public float Offset
public float Location
{
get
{
return offset;
return location;
}
set
{
offset = value.Between(0, 1);
location = value.Between(0f, 100f);
}
}
@ -58,7 +58,7 @@ public GradientStop()
public GradientStop(Color color, float offset)
{
Color = color;
Offset = offset;
Location = offset;
}
}
}

View file

@ -63,7 +63,12 @@ protected override void OnMouseClick(MouseEventArgs mevent)
{
base.OnMouseClick(mevent);
using (DialogColor dialogColor = new DialogColor(Color))
ShowColorDialog();
}
public void ShowColorDialog()
{
using (ColorPickerForm dialogColor = new ColorPickerForm(Color))
{
if (dialogColor.ShowDialog() == DialogResult.OK)
{

View file

@ -144,6 +144,7 @@ private object Deserialize(string json)
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
serializer.Error += (sender, e) => e.ErrorContext.Handled = true;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
serializer.TypeNameHandling = TypeNameHandling.Auto;
return serializer.Deserialize(textReader, ObjectType);
}

View file

@ -192,8 +192,8 @@ public void LoadImage(Image img)
Reset();
isImageLoading = true;
Image = (Image)img.Clone();
AutoSetSizeMode();
isImageLoading = false;
AutoSetSizeMode();
}
}
}
@ -207,8 +207,8 @@ public void LoadImageFromFile(string filePath)
Reset();
isImageLoading = true;
Image = ImageHelpers.LoadImage(filePath);
AutoSetSizeMode();
isImageLoading = false;
AutoSetSizeMode();
}
}
}

View file

@ -224,7 +224,7 @@ private void rbRedGreenBlue_CheckedChanged(object sender, EventArgs e)
private void btnColorDialog_Click(object sender, EventArgs e)
{
using (DialogColor dialogColor = new DialogColor(Color.FromArgb(tbRed.Value, tbGreen.Value, tbBlue.Value)))
using (ColorPickerForm dialogColor = new ColorPickerForm(Color.FromArgb(tbRed.Value, tbGreen.Value, tbBlue.Value)))
{
if (dialogColor.ShowDialog() == DialogResult.OK)
{

View file

@ -350,5 +350,18 @@ public static Color Mix(List<Color> colors)
return Color.FromArgb(a / count, r / count, g / count, b / count);
}
public static int PerceivedBrightness(Color c)
{
return (int)Math.Sqrt(
c.R * c.R * .299 +
c.G * c.G * .587 +
c.B * c.B * .114);
}
public static Color VisibleTextColor(Color c)
{
return PerceivedBrightness(c) > 130 ? Color.Black : Color.White;
}
}
}

View file

@ -80,6 +80,14 @@
<Compile Include="ColorMatrixManager.cs" />
<Compile Include="Colors\CMYK.cs" />
<Compile Include="Colors\ColorEventHandler.cs" />
<Compile Include="Colors\GradientInfo.cs" />
<Compile Include="Colors\GradientPickerForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Colors\GradientPickerForm.Designer.cs">
<DependentUpon>GradientPickerForm.cs</DependentUpon>
</Compile>
<Compile Include="Colors\GradientStop.cs" />
<Compile Include="Colors\HSB.cs" />
<Compile Include="Colors\MyColor.cs" />
<Compile Include="Colors\RGBA.cs" />
@ -154,6 +162,7 @@
<Compile Include="TextBoxTraceListener.cs" />
<Compile Include="UITypeEditors\EnumDescriptionConverter.cs" />
<Compile Include="UITypeEditors\DirectoryNameEditor.cs" />
<Compile Include="UITypeEditors\GradientEditor.cs" />
<Compile Include="UITypeEditors\MyColorConverter.cs" />
<Compile Include="UITypeEditors\MyColorEditor.cs" />
<Compile Include="UITypeEditors\ImageFileNameEditor.cs" />
@ -204,11 +213,11 @@
<Compile Include="Colors\ColorUserControl.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Colors\DialogColor.cs">
<Compile Include="Colors\ColorPickerForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Colors\DialogColor.Designer.cs">
<DependentUpon>DialogColor.cs</DependentUpon>
<Compile Include="Colors\ColorPickerForm.Designer.cs">
<DependentUpon>ColorPickerForm.cs</DependentUpon>
</Compile>
<Compile Include="Extensions\EnumExtensions.cs" />
<Compile Include="Extensions\GraphicsExtensions.cs" />
@ -346,8 +355,11 @@
<Compile Include="WindowState.cs" />
<Compile Include="XmlColor.cs" />
<Compile Include="XmlFont.cs" />
<EmbeddedResource Include="Colors\DialogColor.resx">
<DependentUpon>DialogColor.cs</DependentUpon>
<EmbeddedResource Include="Colors\ColorPickerForm.resx">
<DependentUpon>ColorPickerForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Colors\GradientPickerForm.resx">
<DependentUpon>GradientPickerForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="DNS\DNSChangerForm.resx">
<DependentUpon>DNSChangerForm.cs</DependentUpon>

View file

@ -169,6 +169,7 @@ public static T Load<T>(Stream stream, SerializationType type)
JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new StringEnumConverter());
serializer.Error += (sender, e) => e.ErrorContext.Handled = true;
serializer.ObjectCreationHandling = ObjectCreationHandling.Replace;
settings = serializer.Deserialize<T>(jsonReader);
}
break;

View file

@ -0,0 +1,80 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2007-2014 ShareX Developers
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 System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace HelpersLib
{
public class GradientEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (value.GetType() != typeof(GradientInfo))
{
return value;
}
IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (svc != null)
{
GradientInfo gradient = (GradientInfo)value;
using (GradientPickerForm form = new GradientPickerForm(gradient.Copy()))
{
if (svc.ShowDialog(form) == DialogResult.OK)
{
return form.Gradient;
}
}
}
return value;
}
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override void PaintValue(PaintValueEventArgs e)
{
Graphics g = e.Graphics;
GradientInfo gradient = (GradientInfo)e.Value;
gradient.Draw(g, e.Bounds);
g.DrawRectangleProper(Pens.Black, e.Bounds);
}
}
}

View file

@ -52,7 +52,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide
{
Color color = (Color)value;
using (DialogColor form = new DialogColor(color))
using (ColorPickerForm form = new ColorPickerForm(color))
{
if (svc.ShowDialog(form) == DialogResult.OK)
{
@ -84,10 +84,10 @@ public override void PaintValue(PaintValueEventArgs e)
using (SolidBrush brush = new SolidBrush(color))
{
e.Graphics.FillRectangle(brush, e.Bounds);
g.FillRectangle(brush, e.Bounds);
}
e.Graphics.DrawRectangleProper(Pens.Black, e.Bounds);
g.DrawRectangleProper(Pens.Black, e.Bounds);
}
}
}

View file

@ -123,22 +123,27 @@ public int CornerRadius
[DefaultValue(true)]
public bool UseGradient { get; set; }
[DefaultValue(LinearGradientMode.Vertical)]
public LinearGradientMode GradientType { get; set; }
[DefaultValue(typeof(Color), "0, 20, 40"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color BackgroundColor2 { get; set; }
[DefaultValue(false)]
public bool UseCustomGradient { get; set; }
// TODO: Need custom editor
public List<GradientStop> CustomGradientList { get; set; }
[DefaultValue(LinearGradientMode.Vertical)]
public LinearGradientMode GradientType { get; set; }
[Editor(typeof(GradientEditor), typeof(UITypeEditor))]
public GradientInfo Gradient { get; set; }
public DrawText()
{
this.ApplyDefaultPropertyValues();
CustomGradientList = new List<GradientStop>();
Gradient = new GradientInfo();
Gradient.Colors.Add(new GradientStop(Color.FromArgb(68, 120, 194), 0f));
Gradient.Colors.Add(new GradientStop(Color.FromArgb(13, 58, 122), 50f));
Gradient.Colors.Add(new GradientStop(Color.FromArgb(6, 36, 78), 50f));
Gradient.Colors.Add(new GradientStop(Color.FromArgb(12, 76, 159), 100f));
}
public override Image Apply(Image img)
@ -180,18 +185,18 @@ public override Image Apply(Image img)
try
{
if (UseGradient)
if (UseCustomGradient && Gradient != null && Gradient.IsValid)
{
backgroundBrush = new LinearGradientBrush(backgroundRect, Color.Transparent, Color.Transparent, Gradient.Type);
ColorBlend colorBlend = new ColorBlend();
IEnumerable<GradientStop> gradient = Gradient.Colors.OrderBy(x => x.Location);
colorBlend.Colors = gradient.Select(x => x.Color).ToArray();
colorBlend.Positions = gradient.Select(x => x.Location / 100).ToArray();
((LinearGradientBrush)backgroundBrush).InterpolationColors = colorBlend;
}
else if (UseGradient)
{
backgroundBrush = new LinearGradientBrush(backgroundRect, BackgroundColor, BackgroundColor2, GradientType);
if (UseCustomGradient && CustomGradientList != null && CustomGradientList.Count > 1)
{
ColorBlend colorBlend = new ColorBlend();
IEnumerable<GradientStop> gradient = CustomGradientList.OrderBy(x => x.Offset);
colorBlend.Colors = gradient.Select(x => x.Color).ToArray();
colorBlend.Positions = gradient.Select(x => x.Offset).ToArray();
((LinearGradientBrush)backgroundBrush).InterpolationColors = colorBlend;
}
}
else
{

View file

@ -124,7 +124,6 @@
<Compile Include="Manipulations\Resize.cs" />
<Compile Include="Manipulations\Rotate.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GradientStop.cs" />
<Compile Include="WatermarkConfig.cs" />
<Compile Include="WatermarkForm.cs">
<SubType>Form</SubType>

View file

@ -214,7 +214,7 @@ private void RectangleAnnotate_KeyDown(object sender, KeyEventArgs e)
try
{
Options.DrawingPenColor = DialogColor.GetColor(Options.DrawingPenColor);
Options.DrawingPenColor = ColorPickerForm.GetColor(Options.DrawingPenColor);
}
finally
{

View file

@ -33,7 +33,7 @@ You should have received a copy of the GNU General Public License
namespace ShareX
{
public partial class ScreenColorPicker : DialogColor
public partial class ScreenColorPicker : ColorPickerForm
{
private Timer colorTimer = new Timer { Interval = 10 };