This repository has been archived on 2023-07-11. You can view files and clone it, but cannot push or open issues or pull requests.
gwen-dotnet/Gwen/Control/HSVColorPicker.cs
Gered 10e057953e initial commit
Contains changes from "gwen-dotnet" removing dependancies on Windows,
which ultimately means certain features (e.g. file load/save dialogs)
do not work. Those classes still exist, but the code has been commented
out.
2013-03-28 18:47:01 -04:00

199 lines
6.4 KiB
C#

using System;
using System.Drawing;
using Gwen.ControlInternal;
namespace Gwen.Control
{
/// <summary>
/// HSV color picker with "before" and "after" color boxes.
/// </summary>
public class HSVColorPicker : Base, IColorPicker
{
private readonly ColorLerpBox m_LerpBox;
private readonly ColorSlider m_ColorSlider;
private readonly ColorDisplay m_Before;
private readonly ColorDisplay m_After;
/// <summary>
/// Invoked when the selected color has changed.
/// </summary>
public event GwenEventHandler ColorChanged;
/// <summary>
/// The "before" color.
/// </summary>
public Color DefaultColor { get { return m_Before.Color; } set { m_Before.Color = value; } }
/// <summary>
/// Selected color.
/// </summary>
public Color SelectedColor { get { return m_LerpBox.SelectedColor; } }
/// <summary>
/// Initializes a new instance of the <see cref="HSVColorPicker"/> class.
/// </summary>
/// <param name="parent">Parent control.</param>
public HSVColorPicker(Base parent)
: base(parent)
{
MouseInputEnabled = true;
SetSize(256, 128);
//ShouldCacheToTexture = true;
m_LerpBox = new ColorLerpBox(this);
m_LerpBox.ColorChanged += ColorBoxChanged;
m_LerpBox.Dock = Pos.Left;
m_ColorSlider = new ColorSlider(this);
m_ColorSlider.SetPosition(m_LerpBox.Width + 15, 5);
m_ColorSlider.ColorChanged += ColorSliderChanged;
m_ColorSlider.Dock = Pos.Left;
m_After = new ColorDisplay(this);
m_After.SetSize(48, 24);
m_After.SetPosition(m_ColorSlider.X + m_ColorSlider.Width + 15, 5);
m_Before = new ColorDisplay(this);
m_Before.SetSize(48, 24);
m_Before.SetPosition(m_After.X, 28);
int x = m_Before.X;
int y = m_Before.Y + 30;
{
Label label = new Label(this);
label.SetText("R:");
label.SizeToContents();
label.SetPosition(x, y);
TextBoxNumeric numeric = new TextBoxNumeric(this);
numeric.Name = "RedBox";
numeric.SetPosition(x + 15, y - 1);
numeric.SetSize(26, 16);
numeric.SelectAllOnFocus = true;
numeric.TextChanged += NumericTyped;
}
y += 20;
{
Label label = new Label(this);
label.SetText("G:");
label.SizeToContents();
label.SetPosition(x, y);
TextBoxNumeric numeric = new TextBoxNumeric(this);
numeric.Name = "GreenBox";
numeric.SetPosition(x + 15, y - 1);
numeric.SetSize(26, 16);
numeric.SelectAllOnFocus = true;
numeric.TextChanged += NumericTyped;
}
y += 20;
{
Label label = new Label(this);
label.SetText("B:");
label.SizeToContents();
label.SetPosition(x, y);
TextBoxNumeric numeric = new TextBoxNumeric(this);
numeric.Name = "BlueBox";
numeric.SetPosition(x + 15, y - 1);
numeric.SetSize(26, 16);
numeric.SelectAllOnFocus = true;
numeric.TextChanged += NumericTyped;
}
SetColor(DefaultColor);
}
private void NumericTyped(Base control)
{
TextBoxNumeric box = control as TextBoxNumeric;
if (null == box) return;
if (box.Text == String.Empty) return;
int textValue = (int)box.Value;
if (textValue < 0) textValue = 0;
if (textValue > 255) textValue = 255;
Color newColor = SelectedColor;
if (box.Name.Contains("Red"))
{
newColor = Color.FromArgb(SelectedColor.A, textValue, SelectedColor.G, SelectedColor.B);
}
else if (box.Name.Contains("Green"))
{
newColor = Color.FromArgb(SelectedColor.A, SelectedColor.R, textValue, SelectedColor.B);
}
else if (box.Name.Contains("Blue"))
{
newColor = Color.FromArgb(SelectedColor.A, SelectedColor.R, SelectedColor.G, textValue);
}
else if (box.Name.Contains("Alpha"))
{
newColor = Color.FromArgb(textValue, SelectedColor.R, SelectedColor.G, SelectedColor.B);
}
SetColor(newColor);
}
private void UpdateControls(Color color)
{
// What in the FUCK
TextBoxNumeric redBox = FindChildByName("RedBox", false) as TextBoxNumeric;
if (redBox != null)
redBox.SetText(color.R.ToString(), false);
TextBoxNumeric greenBox = FindChildByName("GreenBox", false) as TextBoxNumeric;
if (greenBox != null)
greenBox.SetText(color.G.ToString(), false);
TextBoxNumeric blueBox = FindChildByName("BlueBox", false) as TextBoxNumeric;
if (blueBox != null)
blueBox.SetText(color.B.ToString(), false);
m_After.Color = color;
if (ColorChanged != null)
ColorChanged.Invoke(this);
}
/// <summary>
/// Sets the selected color.
/// </summary>
/// <param name="color">Color to set.</param>
/// <param name="onlyHue">Determines whether only the hue should be set.</param>
/// <param name="reset">Determines whether the "before" color should be set as well.</param>
public void SetColor(Color color, bool onlyHue = false, bool reset = false)
{
UpdateControls(color);
if (reset)
m_Before.Color = color;
m_ColorSlider.SelectedColor = color;
m_LerpBox.SetColor(color, onlyHue);
m_After.Color = color;
}
private void ColorBoxChanged(Base control)
{
UpdateControls(SelectedColor);
Invalidate();
}
private void ColorSliderChanged(Base control)
{
if (m_LerpBox != null)
m_LerpBox.SetColor(m_ColorSlider.SelectedColor, true);
Invalidate();
}
}
}