2013-11-03 23:53:49 +13:00
|
|
|
/*
|
|
|
|
* Greenshot - a free and open source screenshot tool
|
2016-03-14 05:07:53 +13:00
|
|
|
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
|
2013-11-03 23:53:49 +13:00
|
|
|
*
|
|
|
|
* For more information see: http://getgreenshot.org/
|
|
|
|
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
|
|
|
|
*
|
|
|
|
* 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 1 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
using Greenshot.Configuration;
|
|
|
|
using Greenshot.Controls;
|
|
|
|
using Greenshot.IniFile;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Drawing;
|
2014-03-29 01:55:41 +13:00
|
|
|
using System.Globalization;
|
2013-11-03 23:53:49 +13:00
|
|
|
using System.Threading;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
namespace Greenshot
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Description of ColorDialog.
|
|
|
|
/// </summary>
|
|
|
|
public partial class ColorDialog : BaseForm
|
|
|
|
{
|
|
|
|
private static ColorDialog uniqueInstance;
|
2016-05-23 03:17:02 +12:00
|
|
|
private static readonly EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
private ColorDialog()
|
|
|
|
{
|
|
|
|
SuspendLayout();
|
|
|
|
InitializeComponent();
|
|
|
|
SuspendLayout();
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorPalette(5, 5, 15, 15);
|
|
|
|
CreateLastUsedColorButtonRow(5, 190, 15, 15);
|
2013-11-03 23:53:49 +13:00
|
|
|
ResumeLayout();
|
2014-12-23 11:54:20 +13:00
|
|
|
UpdateRecentColorsButtonRow();
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
public static ColorDialog GetInstance()
|
|
|
|
{
|
|
|
|
if (uniqueInstance == null)
|
|
|
|
{
|
|
|
|
uniqueInstance = new ColorDialog();
|
|
|
|
}
|
|
|
|
return uniqueInstance;
|
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private readonly List<Button> _colorButtons = new List<Button>();
|
|
|
|
private readonly List<Button> _recentColorButtons = new List<Button>();
|
|
|
|
private readonly ToolTip _toolTip = new ToolTip();
|
2016-05-23 03:17:02 +12:00
|
|
|
private bool _updateInProgress;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
|
|
public Color Color
|
|
|
|
{
|
|
|
|
get { return colorPanel.BackColor; }
|
2014-12-23 11:54:20 +13:00
|
|
|
set { PreviewColor(value, this); }
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#region user interface generation
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void CreateColorPalette(int x, int y, int w, int h)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255, 0, 0, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255, 255 / 2, 0, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255, 255, 0, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255 / 2, 255, 0, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(0, 255, 0, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(0, 255, 255 / 2, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(0, 255, 255, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(0, 255 / 2, 255, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(0, 0, 255, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255 / 2, 0, 255, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255, 0, 255, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255, 0, 255 / 2, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w + 5;
|
2014-12-23 11:54:20 +13:00
|
|
|
CreateColorButtonColumn(255 / 2, 255 / 2, 255 / 2, x, y, w, h, 11);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
Controls.AddRange(_colorButtons.ToArray());
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void CreateColorButtonColumn(int red, int green, int blue, int x, int y, int w, int h, int shades)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
int shadedColorsNum = (shades - 1) / 2;
|
|
|
|
for (int i = 0; i <= shadedColorsNum; i++)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
_colorButtons.Add(CreateColorButton(red * i / shadedColorsNum, green * i / shadedColorsNum, blue * i / shadedColorsNum, x, y + i * h, w, h));
|
|
|
|
if (i > 0) _colorButtons.Add(CreateColorButton(red + (255 - red) * i / shadedColorsNum, green + (255 - green) * i / shadedColorsNum, blue + (255 - blue) * i / shadedColorsNum, x, y + (i + shadedColorsNum) * h, w, h));
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private Button CreateColorButton(int red, int green, int blue, int x, int y, int w, int h)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
return CreateColorButton(Color.FromArgb(255, red, green, blue), x, y, w, h);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private Button CreateColorButton(Color color, int x, int y, int w, int h)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
Button b = new Button();
|
|
|
|
b.BackColor = color;
|
|
|
|
b.FlatAppearance.BorderSize = 0;
|
|
|
|
b.FlatStyle = FlatStyle.Flat;
|
|
|
|
b.Location = new Point(x, y);
|
|
|
|
b.Size = new Size(w, h);
|
|
|
|
b.TabStop = false;
|
2014-12-23 11:54:20 +13:00
|
|
|
b.Click += ColorButtonClick;
|
|
|
|
_toolTip.SetToolTip(b, ColorTranslator.ToHtml(color) + " | R:" + color.R + ", G:" + color.G + ", B:" + color.B);
|
2013-11-03 23:53:49 +13:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void CreateLastUsedColorButtonRow(int x, int y, int w, int h)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 12; i++)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
Button b = CreateColorButton(Color.Transparent, x, y, w, h);
|
2013-11-03 23:53:49 +13:00
|
|
|
b.Enabled = false;
|
2014-12-23 11:54:20 +13:00
|
|
|
_recentColorButtons.Add(b);
|
2013-11-03 23:53:49 +13:00
|
|
|
x += w;
|
|
|
|
}
|
2014-12-23 11:54:20 +13:00
|
|
|
Controls.AddRange(_recentColorButtons.ToArray());
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion user interface generation
|
|
|
|
|
|
|
|
#region update user interface
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void UpdateRecentColorsButtonRow()
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
for (int i = 0; i < editorConfiguration.RecentColors.Count && i < 12; i++)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
_recentColorButtons[i].BackColor = editorConfiguration.RecentColors[i];
|
|
|
|
_recentColorButtons[i].Enabled = true;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void PreviewColor(Color colorToPreview, Control trigger)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
_updateInProgress = true;
|
|
|
|
colorPanel.BackColor = colorToPreview;
|
2013-11-03 23:53:49 +13:00
|
|
|
if (trigger != textBoxHtmlColor)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
textBoxHtmlColor.Text = ColorTranslator.ToHtml(colorToPreview);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
if (trigger != textBoxRed && trigger != textBoxGreen && trigger != textBoxBlue && trigger != textBoxAlpha)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
textBoxRed.Text = colorToPreview.R.ToString();
|
|
|
|
textBoxGreen.Text = colorToPreview.G.ToString();
|
|
|
|
textBoxBlue.Text = colorToPreview.B.ToString();
|
|
|
|
textBoxAlpha.Text = colorToPreview.A.ToString();
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
2014-12-23 11:54:20 +13:00
|
|
|
_updateInProgress = false;
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void AddToRecentColors(Color c)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
editorConfiguration.RecentColors.Remove(c);
|
|
|
|
editorConfiguration.RecentColors.Insert(0, c);
|
2014-12-23 11:54:20 +13:00
|
|
|
if (editorConfiguration.RecentColors.Count > 12)
|
|
|
|
{
|
|
|
|
editorConfiguration.RecentColors.RemoveRange(12, editorConfiguration.RecentColors.Count - 12);
|
|
|
|
}
|
|
|
|
UpdateRecentColorsButtonRow();
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion update user interface
|
|
|
|
|
|
|
|
#region textbox event handlers
|
|
|
|
|
|
|
|
private void TextBoxHexadecimalTextChanged(object sender, EventArgs e)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
if (_updateInProgress)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TextBox textBox = (TextBox)sender;
|
|
|
|
string text = textBox.Text.Replace("#", "");
|
2013-11-03 23:53:49 +13:00
|
|
|
int i = 0;
|
2014-12-23 11:54:20 +13:00
|
|
|
Color c;
|
|
|
|
if (Int32.TryParse(text, NumberStyles.AllowHexSpecifier, Thread.CurrentThread.CurrentCulture, out i))
|
|
|
|
{
|
|
|
|
c = Color.FromArgb(i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
KnownColor knownColor;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), text, true);
|
|
|
|
c = Color.FromKnownColor(knownColor);
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
Color opaqueColor = Color.FromArgb(255, c.R, c.G, c.B);
|
2014-12-23 11:54:20 +13:00
|
|
|
PreviewColor(opaqueColor, textBox);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
private void TextBoxRGBTextChanged(object sender, EventArgs e)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
if (_updateInProgress)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TextBox textBox = (TextBox)sender;
|
|
|
|
PreviewColor(Color.FromArgb(GetColorPartIntFromString(textBoxAlpha.Text), GetColorPartIntFromString(textBoxRed.Text), GetColorPartIntFromString(textBoxGreen.Text), GetColorPartIntFromString(textBoxBlue.Text)), textBox);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
private void TextBoxGotFocus(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
textBoxHtmlColor.SelectAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void TextBoxKeyDown(object sender, KeyEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter)
|
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
AddToRecentColors(colorPanel.BackColor);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion textbox event handlers
|
|
|
|
|
|
|
|
#region button event handlers
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void ColorButtonClick(object sender, EventArgs e)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
Button b = (Button)sender;
|
2014-12-23 11:54:20 +13:00
|
|
|
PreviewColor(b.BackColor, b);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void BtnTransparentClick(object sender, EventArgs e)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
2014-12-23 11:54:20 +13:00
|
|
|
ColorButtonClick(sender, e);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
private void BtnApplyClick(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
DialogResult = DialogResult.OK;
|
|
|
|
Hide();
|
2014-12-23 11:54:20 +13:00
|
|
|
AddToRecentColors(colorPanel.BackColor);
|
2013-11-03 23:53:49 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion button event handlers
|
|
|
|
|
|
|
|
#region helper functions
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private int GetColorPartIntFromString(string s)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
Int32.TryParse(s, out ret);
|
|
|
|
if (ret < 0) ret = 0;
|
|
|
|
else if (ret > 255) ret = 255;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion helper functions
|
|
|
|
|
2014-12-23 11:54:20 +13:00
|
|
|
private void PipetteUsed(object sender, PipetteUsedArgs e)
|
2013-11-03 23:53:49 +13:00
|
|
|
{
|
|
|
|
Color = e.color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|