using System; using System.Drawing; using System.Text.RegularExpressions; using System.Windows.Forms; namespace TradeIdeas.TIProGUI.Charting.Controls.ColorHexagonPicker { public partial class MultiColorPicker : UserControl { #region Fields public Color CurrentSelectedColor { get; set; } //public Color ColorToSet { get; set; } public Label LabelToSet { get; set; } // Added to use this control in the QuickButtons form - RVH20220317 public PictureBox PictureBoxToSet { get; set; } private HslColor colorHsl = HslColor.FromAhsl(0xff); private ColorModes colorMode = ColorModes.Hue; private Color colorRgb = Color.Empty; private bool lockUpdates = false; #endregion public MultiColorPicker() //public FullColorPicker(Label labelToSet) { InitializeComponent(); this.colorBox2D.ColorMode = this.colorMode; this.colorSlider.ColorMode = this.colorMode; } public void SetColorSliderColor(Color colorToSet) { //TODO: Find if it's possible to set the Hexagon or colorWheel //colorHexagon.SelectedColor = colorToSet; //colorWheel.Color = colorToSet; // Set Color Slider colors: this.colorHsl = HslColor.FromColor(colorToSet); this.colorRgb = colorToSet; this.colorBox2D.ColorHSL = this.colorHsl; this.colorSlider.ColorHSL = this.colorHsl; this.textboxHexColor.Text = HexConverter(this.colorRgb); //Set RGB and HSL numbers UpdateRgbAndHslColorFields(); } public void EnabledVerticalScroll(bool enabled) { tabColorBox.AutoScroll = false; tabColorBox.HorizontalScroll.Enabled = false; tabColorBox.HorizontalScroll.Visible = false; tabColorBox.HorizontalScroll.Maximum = 0; tabColorBox.VerticalScroll.Enabled = true; tabColorBox.VerticalScroll.Visible = true; tabColorBox.AutoScroll = true; } public void SetColorWheelColor(Color colorToSet) { colorWheel.Color = colorToSet; } private void colorHexagon_ColorChanged(object sender, ColorChangedEventArgs args) { labelCurrentColor.BackColor = colorHexagon.SelectedColor; CurrentSelectedColor = colorHexagon.SelectedColor; // Added to use this control in the QuickButtons form - RVH20220317 if (PictureBoxToSet != null) PictureBoxToSet.BackColor = colorHexagon.SelectedColor; else LabelToSet.BackColor = colorHexagon.SelectedColor; textboxHexColor.Text = HexConverter(colorHexagon.SelectedColor); SetColorSliderColor(colorHexagon.SelectedColor); } private void colorWheel_ColorChanged(object sender, EventArgs e) { labelCurrentColor.BackColor = colorWheel.Color; CurrentSelectedColor = colorWheel.Color; // Added to use this control in the QuickButtons form - RVH20220317 if (PictureBoxToSet != null) PictureBoxToSet.BackColor = colorWheel.Color; else LabelToSet.BackColor = colorWheel.Color; textboxHexColor.Text = HexConverter(colorWheel.Color); SetColorSliderColor(colorWheel.Color); } private void colorSlider_ColorChanged(object sender, ColorChangedEventArgs args) { if (!this.lockUpdates) { HslColor colorHSL = this.colorSlider.ColorHSL; this.colorHsl = colorHSL; this.colorRgb = this.colorHsl.RgbValue; this.lockUpdates = true; this.colorBox2D.ColorHSL = this.colorHsl; this.lockUpdates = false; labelCurrentColor.BackColor = this.colorRgb; CurrentSelectedColor = this.colorRgb; // Added to use this control in the QuickButtons form - RVH20220317 if (PictureBoxToSet != null) PictureBoxToSet.BackColor = this.colorRgb; else LabelToSet.BackColor = this.colorRgb; textboxHexColor.Text = HexConverter(this.colorRgb); UpdateRgbAndHslColorFields(); //SetColorWheelColor(this.colorRgb); } } private void colorBox2D_ColorChanged(object sender, ColorChangedEventArgs args) { if (!this.lockUpdates) { HslColor colorHSL = this.colorBox2D.ColorHSL; this.colorHsl = colorHSL; this.colorRgb = this.colorHsl.RgbValue; this.lockUpdates = true; this.colorSlider.ColorHSL = this.colorHsl; this.lockUpdates = false; labelCurrentColor.BackColor = this.colorRgb; CurrentSelectedColor = this.colorRgb; // Added to use this control in the QuickButtons form - RVH20220317 if (PictureBoxToSet != null) PictureBoxToSet.BackColor = this.colorRgb; else LabelToSet.BackColor = this.colorRgb; textboxHexColor.Text = HexConverter(this.colorRgb); UpdateRgbAndHslColorFields(); } } private void textboxHexColor_TextChanged(object sender, System.EventArgs e) { if (!this.lockUpdates) { var colorRgb = Color.Empty; try { var regexColorCode = new Regex("^#[a-fA-F0-9]{6}$"); var hexColorText = !string.IsNullOrEmpty(textboxHexColor.Text) ? textboxHexColor.Text.Trim() : string.Empty; if (regexColorCode.IsMatch(hexColorText)) { colorRgb = ColorTranslator.FromHtml(hexColorText); } } catch { } if (colorRgb != Color.Empty) { this.colorBox2D.ColorRGB = colorRgb; HslColor colorHSL = this.colorBox2D.ColorHSL; this.colorHsl = colorHSL; this.colorRgb = this.colorHsl.RgbValue; this.lockUpdates = true; this.colorSlider.ColorHSL = this.colorHsl; this.lockUpdates = false; labelCurrentColor.BackColor = this.colorRgb; CurrentSelectedColor = this.colorRgb; // Added to use this control in the QuickButtons form - RVH20220317 if (PictureBoxToSet != null) PictureBoxToSet.BackColor = this.colorRgb; else LabelToSet.BackColor = this.colorRgb; UpdateRgbAndHslColorFields(); } } } private void ColorModeChangedHandler(object sender, EventArgs e) { if (sender == this.radioRed) { this.colorMode = ColorModes.Red; } else if (sender == this.radioGreen) { this.colorMode = ColorModes.Green; } else if (sender == this.radioBlue) { this.colorMode = ColorModes.Blue; } else if (sender == this.radioHue) { this.colorMode = ColorModes.Hue; } else if (sender == this.radioSaturation) { this.colorMode = ColorModes.Saturation; } else if (sender == this.radioLuminance) { this.colorMode = ColorModes.Luminance; } this.colorSlider.ColorMode = this.colorMode; this.colorBox2D.ColorMode = this.colorMode; } private void UpdateRgbAndHslColorFields() { this.lockUpdates = true; this.numRed.Value = this.colorRgb.R; this.numGreen.Value = this.colorRgb.G; this.numBlue.Value = this.colorRgb.B; var tempValue = (int)(((decimal)this.colorHsl.H) * 360M); if (tempValue > 360) this.numHue.Value = tempValue; this.numSaturation.Value = (int)(((decimal)this.colorHsl.S) * 100M); this.numLuminance.Value = (int)(((decimal)this.colorHsl.L) * 100M); this.lockUpdates = false; } private void UpdateRgbFields(Color newColor) { this.colorHsl = HslColor.FromColor(newColor); this.colorRgb = newColor; this.textboxHexColor.Text = HexConverter(this.colorRgb); this.lockUpdates = true; this.numHue.Value = (int)(((decimal)this.colorHsl.H) * 360M); this.numSaturation.Value = (int)(((decimal)this.colorHsl.S) * 100M); this.numLuminance.Value = (int)(((decimal)this.colorHsl.L) * 100M); this.lockUpdates = false; this.colorSlider.ColorHSL = this.colorHsl; this.colorBox2D.ColorHSL = this.colorHsl; } private void UpdateHslFields(HslColor newColor) { this.colorHsl = newColor; this.colorRgb = newColor.RgbValue; this.textboxHexColor.Text = HexConverter(this.colorRgb); this.lockUpdates = true; this.numRed.Value = this.colorRgb.R; this.numGreen.Value = this.colorRgb.G; this.numBlue.Value = this.colorRgb.B; this.lockUpdates = false; this.colorSlider.ColorHSL = this.colorHsl; this.colorBox2D.ColorHSL = this.colorHsl; } private string HexConverter(Color c) { return "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); } private void numRed_ValueChanged(object sender, EventArgs e) { if (!this.lockUpdates) { UpdateRgbFields(Color.FromArgb((int)this.numRed.Value, (int)this.numGreen.Value, (int)this.numBlue.Value)); } } private void numGreen_ValueChanged(object sender, EventArgs e) { if (!this.lockUpdates) { UpdateRgbFields(Color.FromArgb((int)this.numRed.Value, (int)this.numGreen.Value, (int)this.numBlue.Value)); } } private void numBlue_ValueChanged(object sender, EventArgs e) { if (!this.lockUpdates) { UpdateRgbFields(Color.FromArgb((int)this.numRed.Value, (int)this.numGreen.Value, (int)this.numBlue.Value)); } } private void numHue_ValueChanged(object sender, EventArgs e) { if (!this.lockUpdates) { HslColor newColor = HslColor.FromAhsl((double)(((float)((int)this.numHue.Value)) / 360f), this.colorHsl.S, this.colorHsl.L); this.UpdateHslFields(newColor); } } private void numSaturation_ValueChanged(object sender, EventArgs e) { if (!this.lockUpdates) { HslColor newColor = HslColor.FromAhsl(this.colorHsl.A, this.colorHsl.H, (double)(((float)((int)this.numSaturation.Value)) / 100f), this.colorHsl.L); this.UpdateHslFields(newColor); } } private void numLuminance_ValueChanged(object sender, EventArgs e) { if (!this.lockUpdates) { HslColor newColor = HslColor.FromAhsl(this.colorHsl.A, this.colorHsl.H, this.colorHsl.S, (double)(((float)((int)this.numLuminance.Value)) / 100f)); this.UpdateHslFields(newColor); } } } }