BUG-1719: Although it's more a feature... I made the parsing respect HTML color NAMES.

This commit is contained in:
RKrom 2014-12-08 09:29:12 +01:00
commit a4210cbd89
2 changed files with 123 additions and 111 deletions

View file

@ -75,7 +75,7 @@ namespace Greenshot {
this.btnTransparent.TabStop = false; this.btnTransparent.TabStop = false;
this.btnTransparent.Text = "Transparent"; this.btnTransparent.Text = "Transparent";
this.btnTransparent.UseVisualStyleBackColor = false; this.btnTransparent.UseVisualStyleBackColor = false;
this.btnTransparent.Click += new System.EventHandler(this.btnTransparentClick); this.btnTransparent.Click += new System.EventHandler(this.BtnTransparentClick);
// //
// colorPanel // colorPanel
// //
@ -225,7 +225,7 @@ namespace Greenshot {
this.pipette.Name = "pipette"; this.pipette.Name = "pipette";
this.pipette.Size = new System.Drawing.Size(33, 23); this.pipette.Size = new System.Drawing.Size(33, 23);
this.pipette.TabIndex = 13; this.pipette.TabIndex = 13;
this.pipette.PipetteUsed += new System.EventHandler<Greenshot.Controls.PipetteUsedArgs>(this.pipetteUsed); this.pipette.PipetteUsed += new System.EventHandler<Greenshot.Controls.PipetteUsedArgs>(this.PipetteUsed);
// //
// ColorDialog // ColorDialog
// //

View file

@ -18,6 +18,10 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
@ -25,10 +29,6 @@ using System.Globalization;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using Greenshot.Configuration;
using Greenshot.Controls;
using Greenshot.IniFile;
namespace Greenshot { namespace Greenshot {
/// <summary> /// <summary>
/// Description of ColorDialog. /// Description of ColorDialog.
@ -41,188 +41,200 @@ namespace Greenshot {
SuspendLayout(); SuspendLayout();
InitializeComponent(); InitializeComponent();
SuspendLayout(); SuspendLayout();
createColorPalette(5,5,15,15); CreateColorPalette(5, 5, 15, 15);
createLastUsedColorButtonRow(5,190,15,15); CreateLastUsedColorButtonRow(5, 190, 15, 15);
ResumeLayout(); ResumeLayout();
updateRecentColorsButtonRow(); UpdateRecentColorsButtonRow();
} }
public static ColorDialog GetInstance() { public static ColorDialog GetInstance() {
if(uniqueInstance == null) { if (uniqueInstance == null) {
uniqueInstance = new ColorDialog(); uniqueInstance = new ColorDialog();
} }
return uniqueInstance; return uniqueInstance;
} }
private List<Button> colorButtons = new List<Button>(); private readonly List<Button> _colorButtons = new List<Button>();
private List<Button> recentColorButtons = new List<Button>(); private readonly List<Button> _recentColorButtons = new List<Button>();
private ToolTip toolTip = new ToolTip(); private readonly ToolTip _toolTip = new ToolTip();
private bool updateInProgress = false; private bool _updateInProgress = false;
public Color Color { public Color Color {
get {return colorPanel.BackColor;} get { return colorPanel.BackColor; }
set {previewColor(value,this);} set { PreviewColor(value, this); }
} }
#region user interface generation #region user interface generation
private void createColorPalette(int x, int y, int w, int h) { private void CreateColorPalette(int x, int y, int w, int h) {
createColorButtonColumn(255,0,0, x, y, w, h, 11); CreateColorButtonColumn(255, 0, 0, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(255,255/2,0, x, y, w, h, 11); CreateColorButtonColumn(255, 255 / 2, 0, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(255,255,0, x, y, w, h, 11); CreateColorButtonColumn(255, 255, 0, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(255/2,255,0, x, y, w, h, 11); CreateColorButtonColumn(255 / 2, 255, 0, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(0,255,0, x, y, w, h, 11); CreateColorButtonColumn(0, 255, 0, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(0,255,255/2, x, y, w, h, 11); CreateColorButtonColumn(0, 255, 255 / 2, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(0,255,255, x, y, w, h, 11); CreateColorButtonColumn(0, 255, 255, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(0,255/2,255, x, y, w, h, 11); CreateColorButtonColumn(0, 255 / 2, 255, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(0,0,255, x, y, w, h, 11); CreateColorButtonColumn(0, 0, 255, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(255/2,0,255, x, y, w, h, 11); CreateColorButtonColumn(255 / 2, 0, 255, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(255,0,255, x, y, w, h, 11); CreateColorButtonColumn(255, 0, 255, x, y, w, h, 11);
x += w; x += w;
createColorButtonColumn(255,0,255/2, x, y, w, h, 11); CreateColorButtonColumn(255, 0, 255 / 2, x, y, w, h, 11);
x += w + 5; x += w + 5;
createColorButtonColumn(255/2,255/2,255/2, x, y, w, h, 11); CreateColorButtonColumn(255 / 2, 255 / 2, 255 / 2, x, y, w, h, 11);
Controls.AddRange(colorButtons.ToArray()); Controls.AddRange(_colorButtons.ToArray());
} }
private void createColorButtonColumn(int red, int green, int blue, int x, int y, int w, int h, int shades) {
private void CreateColorButtonColumn(int red, int green, int blue, int x, int y, int w, int h, int shades) {
int shadedColorsNum = (shades - 1) / 2; int shadedColorsNum = (shades - 1) / 2;
for(int i=0; i<=shadedColorsNum; i++) { for (int i = 0; i <= shadedColorsNum; i++) {
colorButtons.Add(createColorButton(red * i / shadedColorsNum, green * i /shadedColorsNum, blue * i / shadedColorsNum, x, y + i * h, w, h)); _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)); 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));
} }
} }
private Button createColorButton(int red, int green, int blue, int x, int y, int w, int h) {
return createColorButton(Color.FromArgb(255, red, green, blue), x, y, w, h); private Button CreateColorButton(int red, int green, int blue, int x, int y, int w, int h) {
return CreateColorButton(Color.FromArgb(255, red, green, blue), x, y, w, h);
} }
private Button createColorButton(Color color, int x, int y, int w, int h) {
private Button CreateColorButton(Color color, int x, int y, int w, int h) {
Button b = new Button(); Button b = new Button();
b.BackColor = color; b.BackColor = color;
b.FlatAppearance.BorderSize = 0; b.FlatAppearance.BorderSize = 0;
b.FlatStyle = FlatStyle.Flat; b.FlatStyle = FlatStyle.Flat;
b.Location = new Point(x,y); b.Location = new Point(x, y);
b.Size = new Size(w,h); b.Size = new Size(w, h);
b.TabStop = false; b.TabStop = false;
b.Click += colorButtonClick; b.Click += ColorButtonClick;
toolTip.SetToolTip(b,ColorTranslator.ToHtml(color)+" | R:"+color.R +", G:"+color.G+", B:"+color.B); _toolTip.SetToolTip(b, ColorTranslator.ToHtml(color) + " | R:" + color.R + ", G:" + color.G + ", B:" + color.B);
return b; return b;
} }
private void createLastUsedColorButtonRow(int x, int y, int w, int h) {
for(int i=0; i<12; i++) { private void CreateLastUsedColorButtonRow(int x, int y, int w, int h) {
Button b = createColorButton(Color.Transparent, x, y, w, h); for (int i = 0; i < 12; i++) {
Button b = CreateColorButton(Color.Transparent, x, y, w, h);
b.Enabled = false; b.Enabled = false;
recentColorButtons.Add(b); _recentColorButtons.Add(b);
x += w; x += w;
} }
Controls.AddRange(recentColorButtons.ToArray()); Controls.AddRange(_recentColorButtons.ToArray());
} }
#endregion #endregion
#region update user interface #region update user interface
private void updateRecentColorsButtonRow() { private void UpdateRecentColorsButtonRow() {
for(int i=0; i<editorConfiguration.RecentColors.Count && i<12; i++) { for (int i = 0; i < editorConfiguration.RecentColors.Count && i < 12; i++) {
recentColorButtons[i].BackColor = editorConfiguration.RecentColors[i]; _recentColorButtons[i].BackColor = editorConfiguration.RecentColors[i];
recentColorButtons[i].Enabled = true; _recentColorButtons[i].Enabled = true;
} }
} }
private void previewColor(Color c, Control trigger) { private void PreviewColor(Color colorToPreview, Control trigger) {
updateInProgress = true; _updateInProgress = true;
colorPanel.BackColor = c; colorPanel.BackColor = colorToPreview;
if(trigger != textBoxHtmlColor) { if (trigger != textBoxHtmlColor) {
textBoxHtmlColor.Text = ColorTranslator.ToHtml(c); textBoxHtmlColor.Text = ColorTranslator.ToHtml(colorToPreview);
} else {
if(!textBoxHtmlColor.Text.StartsWith("#")) {
int selStart = textBoxHtmlColor.SelectionStart;
int selLength = textBoxHtmlColor.SelectionLength;
textBoxHtmlColor.Text = "#" +textBoxHtmlColor.Text;
textBoxHtmlColor.Select(selStart+1, selLength+1);
} }
if (trigger != textBoxRed && trigger != textBoxGreen && trigger != textBoxBlue && trigger != textBoxAlpha) {
textBoxRed.Text = colorToPreview.R.ToString();
textBoxGreen.Text = colorToPreview.G.ToString();
textBoxBlue.Text = colorToPreview.B.ToString();
textBoxAlpha.Text = colorToPreview.A.ToString();
} }
if(trigger != textBoxRed && trigger != textBoxGreen && trigger != textBoxBlue && trigger != textBoxAlpha) { _updateInProgress = false;
textBoxRed.Text = c.R.ToString();
textBoxGreen.Text = c.G.ToString();
textBoxBlue.Text = c.B.ToString();
textBoxAlpha.Text = c.A.ToString();
}
updateInProgress = false;
} }
private void addToRecentColors(Color c) { private void AddToRecentColors(Color c) {
editorConfiguration.RecentColors.Remove(c); editorConfiguration.RecentColors.Remove(c);
editorConfiguration.RecentColors.Insert(0, c); editorConfiguration.RecentColors.Insert(0, c);
if(editorConfiguration.RecentColors.Count > 12) editorConfiguration.RecentColors.RemoveRange(12,editorConfiguration.RecentColors.Count-12); if (editorConfiguration.RecentColors.Count > 12) {
updateRecentColorsButtonRow(); editorConfiguration.RecentColors.RemoveRange(12, editorConfiguration.RecentColors.Count - 12);
}
UpdateRecentColorsButtonRow();
} }
#endregion #endregion
#region textbox event handlers #region textbox event handlers
void TextBoxHexadecimalTextChanged(object sender, EventArgs e) void TextBoxHexadecimalTextChanged(object sender, EventArgs e) {
{ if (_updateInProgress) {
if(updateInProgress) return; return;
TextBox tb = (TextBox) sender; }
string t = tb.Text.Replace("#",""); TextBox textBox = (TextBox)sender;
string text = textBox.Text.Replace("#", "");
int i = 0; int i = 0;
Int32.TryParse(t, NumberStyles.AllowHexSpecifier, Thread.CurrentThread.CurrentCulture, out i); Color c;
Color c = Color.FromArgb(i); 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;
}
}
Color opaqueColor = Color.FromArgb(255, c.R, c.G, c.B); Color opaqueColor = Color.FromArgb(255, c.R, c.G, c.B);
previewColor(opaqueColor, tb); PreviewColor(opaqueColor, textBox);
} }
void TextBoxRGBTextChanged(object sender, EventArgs e)
{ void TextBoxRGBTextChanged(object sender, EventArgs e) {
if(updateInProgress) return; if (_updateInProgress) {
TextBox tb = (TextBox) sender; return;
previewColor(Color.FromArgb(getColorPartIntFromString(textBoxAlpha.Text),getColorPartIntFromString(textBoxRed.Text),getColorPartIntFromString(textBoxGreen.Text),getColorPartIntFromString(textBoxBlue.Text)), tb);
} }
TextBox textBox = (TextBox)sender;
PreviewColor(Color.FromArgb(GetColorPartIntFromString(textBoxAlpha.Text), GetColorPartIntFromString(textBoxRed.Text), GetColorPartIntFromString(textBoxGreen.Text), GetColorPartIntFromString(textBoxBlue.Text)), textBox);
}
void TextBoxGotFocus(object sender, EventArgs e) { void TextBoxGotFocus(object sender, EventArgs e) {
textBoxHtmlColor.SelectAll(); textBoxHtmlColor.SelectAll();
} }
void TextBoxKeyDown(object sender, KeyEventArgs e) { void TextBoxKeyDown(object sender, KeyEventArgs e) {
if(e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter) { if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter) {
addToRecentColors(colorPanel.BackColor); AddToRecentColors(colorPanel.BackColor);
} }
} }
#endregion #endregion
#region button event handlers #region button event handlers
void colorButtonClick(object sender, EventArgs e) { void ColorButtonClick(object sender, EventArgs e) {
Button b = (Button) sender; Button b = (Button)sender;
previewColor(b.BackColor, b); PreviewColor(b.BackColor, b);
} }
void btnTransparentClick(object sender, EventArgs e) void BtnTransparentClick(object sender, EventArgs e) {
{ ColorButtonClick(sender, e);
colorButtonClick(sender, e);
} }
void BtnApplyClick(object sender, EventArgs e) void BtnApplyClick(object sender, EventArgs e) {
{
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
Hide(); Hide();
addToRecentColors(colorPanel.BackColor); AddToRecentColors(colorPanel.BackColor);
} }
#endregion #endregion
#region helper functions #region helper functions
private int getColorPartIntFromString(string s){ private int GetColorPartIntFromString(string s) {
int ret = 0; int ret = 0;
Int32.TryParse(s,out ret); Int32.TryParse(s, out ret);
if(ret < 0) ret = 0; if (ret < 0) ret = 0;
else if(ret > 255) ret = 255; else if (ret > 255) ret = 255;
return ret; return ret;
} }
#endregion #endregion
private void pipetteUsed(object sender, PipetteUsedArgs e) { private void PipetteUsed(object sender, PipetteUsedArgs e) {
Color = e.color; Color = e.color;
} }
} }