//select or add faders protected override void OnMouseDown(MouseEventArgs e) { if (_blend != null && e.Button == MouseButtons.Left && !ReadOnly) { bool foc; Selection = GetFaderUnderMouse(e.Location, ref _offset, out foc); FocusSelection = foc; if (_selection == null) { //create new color or alpha point Rectangle area = Rectangle.Inflate(this.ClientRectangle, -BORDER, -BORDER); double pos = PointToPos(e.Location); _offset = Point.Empty; // if (_orientation == Orientation.Horizontal ? e.Y > area.Bottom : e.X > area.Right) { ColorPoint pnt = new ColorPoint(_blend.GetColorAt((float)pos), pos); _selection = pnt; _blend.Colors.Add(pnt); } else if (_orientation == Orientation.Horizontal ? e.Y < area.Y : e.X < area.X) { // MS: 25/09/11: disable drawing alpha points, we don't want to use them (yet). //AlphaPoint pnt = new AlphaPoint(_blend.GetColorAt((float)pos).A, pos); //_selection = pnt; //_blend.Alphas.Add(pnt); } } } base.OnMouseDown(e); }
//updates all controls private void UpdateUI() { if (edit.Gradient == null) { grpStops.Enabled = false; } else { grpStops.Enabled = true; ColorPoint cpt = edit.Selection as ColorPoint; if (cpt != null) { vColorLoc.Enabled = true; lblColorSelect.Enabled = btnDeleteColor.Enabled = !edit.FocusSelection; // vColorLoc.Value = (int)((edit.FocusSelection ? cpt.Focus : cpt.Position) * 100.0); lblColorSelect.Color = edit.FocusSelection ? Color.DimGray : lblColorSelect.OldColor = (_xyz = cpt.Color).ToRGB().ToArgb(); } else { lblColorSelect.Enabled = vColorLoc.Enabled = btnDeleteColor.Enabled = false; lblColorSelect.Color = lblColorSelect.OldColor = Color.DimGray; vColorLoc.Value = 0; } } }
//active color dragged private void lblColorSelect_ColorChanged(object sender, EventArgs e) { if (edit.Gradient == null || edit.FocusSelection) { return; } ColorPoint pt = edit.Selection as ColorPoint; if (pt != null) { pt.Color = XYZ.FromRGB(new RGB(lblColorSelect.Color)); } }
public ColorGradient GetSubGradient(double start, double end) { double range = end - start; if (range < 0) { throw new ArgumentException("end must be after start"); } ColorGradient result = new ColorGradient(); result.Colors.Clear(); result.Colors.Add(new ColorPoint(GetColorAt(start), 0)); ColorPoint previous = null; foreach (ColorPoint cp in Colors) { if (cp.Position > start && cp.Position < end) { double scaledPos = (cp.Position - start) / range; if (scaledPos > 1.0 || scaledPos < 0.0) { throw new Exception("Error calculating position: " + scaledPos + " out of range"); } result.Colors.Add(new ColorPoint(cp.Color.ToRGB(), scaledPos)); } } //Sample a few more colors out for more accuracy for (double d = start + .2d; d < end; d += .2d) { if (d < end) { var c = GetColorAt(d); double scaledPos = (d - start) / range; if (scaledPos > 1.0 || scaledPos < 0.0) { throw new Exception("Error calculating position: " + scaledPos + " out of range"); } result.Colors.Add(new ColorPoint(c, scaledPos)); } } result.Colors.Add(new ColorPoint(GetColorAt(end), 1)); return(result); }
//active color point location private void vColorLoc_ValueChanged(ValueControl sender, ValueChangedEventArgs e) { if (edit.Gradient == null) { return; } ColorPoint pt = edit.Selection as ColorPoint; if (pt == null) { return; } if (edit.FocusSelection) { pt.Focus = (double)vColorLoc.Value / 100.0; } else { pt.Position = (double)vColorLoc.Value / 100.0; } }
// edits the selected color in the 'edit' control private void editSelectedColor() { if (edit.Gradient == null || edit.FocusSelection) { return; } ColorPoint pt = edit.Selection as ColorPoint; if (pt == null) { return; } using (ColorPicker frm = new ColorPicker(_mode, _fader)) { frm.LockValue_V = LockColorEditorHSV_Value; frm.Color = _xyz; if (frm.ShowDialog(this.FindForm()) == DialogResult.OK) { pt.Color = _xyz = frm.Color; lblColorSelect.Color = _xyz.ToRGB().ToArgb(); _mode = frm.SecondaryMode; _fader = frm.PrimaryFader; } } }
// edits the selected color in the 'edit' control private void editSelectedPoints() { if (edit.Gradient == null || edit.FocusSelection) { return; } if (DiscreteColors) { List <Color> selectedColors = new List <Color>(); foreach (ColorGradient.Point point in edit.Selection) { ColorPoint pt = point as ColorPoint; if (pt == null) { continue; } selectedColors.Add(pt.Color.ToRGB().ToArgb()); } using (DiscreteColorPicker picker = new DiscreteColorPicker()) { picker.ValidColors = ValidDiscreteColors; picker.SelectedColors = selectedColors; if (picker.ShowDialog() == DialogResult.OK) { if (picker.SelectedColors.Count() == 0) { DeleteColor(); } else if (picker.SelectedColors.Count() == selectedColors.Count) { int i = 0; foreach (Color selectedColor in picker.SelectedColors) { ColorPoint pt = edit.Selection[i] as ColorPoint; pt.Color = XYZ.FromRGB(selectedColor); } } else { double position = edit.Selection.First().Position; foreach (ColorGradient.Point point in edit.Selection) { edit.Gradient.Colors.Remove(point as ColorPoint); } foreach (Color selectedColor in picker.SelectedColors) { ColorPoint newPoint = new ColorPoint(selectedColor, position); edit.Gradient.Colors.Add(newPoint); } } } } } else { if (edit.Selection.Count > 1) { //messageBox Arguments are (Text, Title, No Button Visible, Cancel Button Visible) MessageBoxForm.msgIcon = SystemIcons.Error; //this is used if you want to add a system icon to the message form. var messageBox = new MessageBoxForm("Non-discrete color gradient, >1 selected point. oops! please report it.", "Delete library gradient?", false, false); messageBox.ShowDialog(); } ColorPoint pt = edit.Selection.FirstOrDefault() as ColorPoint; if (pt == null) { return; } using (ColorPicker frm = new ColorPicker(_mode, _fader)) { frm.LockValue_V = LockColorEditorHSV_Value; frm.Color = _xyz; if (frm.ShowDialog(this.FindForm()) == DialogResult.OK) { pt.Color = _xyz = frm.Color; lblColorSelect.Color = _xyz.ToRGB().ToArgb(); _mode = frm.SecondaryMode; _fader = frm.PrimaryFader; } } } }
//select or add faders protected override void OnMouseDown(MouseEventArgs e) { if (_blend != null && e.Button == MouseButtons.Left && !ReadOnly) { bool foc; Selection = GetFadersUnderMouse(e.Location, ref _offset, out foc); FocusSelection = foc; if (_selection == null || _selection.Count == 0) { //create new color or alpha point Rectangle area = Rectangle.Inflate(this.ClientRectangle, -BORDER, -BORDER); double pos = PointToPos(e.Location); _offset = Point.Empty; // if (_orientation == Orientation.Horizontal ? e.Y > area.Bottom : e.X > area.Right) { List <ColorGradient.Point> newColorPoints; if (DiscreteColors) { List <Color> newColors = new List <Color>(); double targetPos = -1; foreach (ColorPoint colorPoint in _blend.Colors.SortedArray()) { if (targetPos < 0 || (colorPoint.Position < pos && colorPoint.Position != targetPos)) { targetPos = colorPoint.Position; newColors = new List <Color>(); } if (colorPoint.Position == targetPos) { newColors.Add(colorPoint.Color.ToRGB()); } if (colorPoint.Position > targetPos) { break; } } newColorPoints = new List <ColorGradient.Point>(); foreach (Color newColor in newColors) { ColorPoint point = new ColorPoint(newColor, targetPos); newColorPoints.Add(point); } } else { newColorPoints = new List <ColorGradient.Point> { new ColorPoint(_blend.GetColorAt((float)pos), pos) }; } _selection = newColorPoints; foreach (ColorPoint newColorPoint in newColorPoints) { _blend.Colors.Add(newColorPoint); } } else if (_orientation == Orientation.Horizontal ? e.Y < area.Y : e.X < area.X) { // MS: 25/09/11: disable drawing alpha points, we don't want to use them (yet). //AlphaPoint pnt = new AlphaPoint(_blend.GetColorAt((float)pos).A, pos); //_selection = pnt; //_blend.Alphas.Add(pnt); } } } base.OnMouseDown(e); }
//draw gradient and faders protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (this.Width < BORDER * 2 || this.Height < BORDER * 2) { return; } e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //draw gradient Rectangle area = new Rectangle(BORDER, BORDER, this.Width - BORDER * 2 - 1, this.Height - BORDER * 2 - 1); if (_blend != null) { using (HatchBrush brs = new HatchBrush(HatchStyle.LargeCheckerBoard, Color.Silver, Color.White)) { e.Graphics.FillRectangle(brs, area); } using (Bitmap bmp = _blend.GenerateColorGradientImage(area.Size, DiscreteColors)) { e.Graphics.DrawImage(bmp, area.X, area.Y); } } //border e.Graphics.DrawRectangle(Pens.Silver, area); if (_blend != null) { List <ColorPoint> sortedColors = new List <ColorPoint>(_blend.Colors.SortedArray()); // we'll assume they're sorted, so any colorpoints at the same position are contiguous in the array for (int i = 0; i < sortedColors.Count; i++) { ColorPoint currentPoint = sortedColors[i]; double currentPos = currentPoint.Position; List <Color> currentColors = new List <Color>(); currentColors.Add(currentPoint.GetColor(Color.Black)); while (i + 1 < sortedColors.Count && sortedColors[i + 1].Position == currentPos) { currentColors.Add(sortedColors[i + 1].GetColor(Color.Black)); i++; } DrawFader(e.Graphics, currentPoint.Position, currentColors, false, _selection != null && _selection.Contains(currentPoint) && !_focussel); } ////draw color points //foreach (ColorPoint pnt in _blend.Colors) // DrawFader(e.Graphics, pnt.Position, // pnt.GetColor(Color.Black), // false, pnt == _selection && !_focussel); // MS: 25/09/11: disable drawing alpha points, we don't want to use them (yet). // draw alpha points //foreach (AlphaPoint pnt in _blend.Alphas) // DrawFader(e.Graphics, pnt.Position, // pnt.GetColor(Color.Black), // true, pnt == _selection && !_focussel); // draw selected focus if (_selection != null && _selection.Count > 0) { DrawDiamond(e.Graphics, _blend.GetFocusPosition(_selection.First()), _selection.First() is AlphaPoint, _focussel); } } }
// edits the selected color in the 'edit' control private void editSelectedPoints() { if (edit.Gradient == null || edit.FocusSelection) { return; } if (DiscreteColors) { List <Color> selectedColors = new List <Color>(); foreach (ColorGradient.Point point in edit.Selection) { ColorPoint pt = point as ColorPoint; if (pt == null) { continue; } selectedColors.Add(pt.Color.ToRGB().ToArgb()); } using (DiscreteColorPicker picker = new DiscreteColorPicker()) { picker.ValidColors = ValidDiscreteColors; picker.SelectedColors = selectedColors; if (picker.ShowDialog() == DialogResult.OK) { if (picker.SelectedColors.Count() == 0) { DeleteColor(); } else if (picker.SelectedColors.Count() == selectedColors.Count) { int i = 0; foreach (Color selectedColor in picker.SelectedColors) { ColorPoint pt = edit.Selection[i] as ColorPoint; pt.Color = XYZ.FromRGB(selectedColor); } } else { double position = edit.Selection.First().Position; foreach (ColorGradient.Point point in edit.Selection) { edit.Gradient.Colors.Remove(point as ColorPoint); } foreach (Color selectedColor in picker.SelectedColors) { ColorPoint newPoint = new ColorPoint(selectedColor, position); edit.Gradient.Colors.Add(newPoint); } } } } } else { if (edit.Selection.Count > 1) { MessageBox.Show("Non-discrete color gradient, >1 selected point. oops! please report it."); } ColorPoint pt = edit.Selection.FirstOrDefault() as ColorPoint; if (pt == null) { return; } using (ColorPicker frm = new ColorPicker(_mode, _fader)) { frm.LockValue_V = LockColorEditorHSV_Value; frm.Color = _xyz; if (frm.ShowDialog(this.FindForm()) == DialogResult.OK) { pt.Color = _xyz = frm.Color; lblColorSelect.Color = _xyz.ToRGB().ToArgb(); _mode = frm.SecondaryMode; _fader = frm.PrimaryFader; } } } }
/// <summary> /// creates color blend out of color point data /// </summary> /// <returns></returns> private ColorBlend CreateColorBlend(Color?filterColor = null) { //sort all points ColorPoint[] colpoints = Colors.SortedArray(); AlphaPoint[] alphapoints = Alphas.SortedArray(); //init out vars SortedList <float, Color> positions = new SortedList <float, Color>(); // if we're filtering the colors, the iterate through the color list, setting all non-matching colors to black if (filterColor != null) { List <ColorPoint> newColorPoints = new List <ColorPoint>(); foreach (ColorPoint colorPoint in colpoints) { ColorPoint newPoint = (ColorPoint)colorPoint.Clone(); if (newPoint.Color.ToRGB().ToArgb() != filterColor) { // it's not the desired color. Make it black and add it, but only if there's // not an entry in the list with this position already if (newColorPoints.Where(x => x.Position == newPoint.Position).Count() == 0) { newPoint.Color = XYZ.FromRGB(Color.Black); newColorPoints.Add(newPoint); } } else { // it's the desired color. Find any others in the list that are in this position // and black, remove them, and then replace it with this one newColorPoints.RemoveAll( x => x.Position == newPoint.Position && x.Color.ToRGB().ToArgb().ToArgb() == Color.Black.ToArgb()); newColorPoints.Add(newPoint); } } colpoints = newColorPoints.ToArray(); } //add color points for (int c = 0; c < colpoints.Length; c++) { // focus point; if filtered, non-standard focus points aren't supported. if (c > 0 && colpoints[c].Focus != .5 && filterColor == null) { AddPosition(colpoints, alphapoints, positions, colpoints[c - 1].Position + (colpoints[c].Position - colpoints[c - 1].Position) * colpoints[c].Focus); } //color AddPosition(colpoints, alphapoints, positions, colpoints[c].Position); } // We aren't using alpha points, and first/last points get added below ////add alpha points //for (int a = 0; a < alphapoints.Length; a++) //{ // if (a > 0 && alphapoints[a].Focus != .5)//focus // AddPosition(colpoints, alphapoints, positions, // alphapoints[a - 1].Position + (alphapoints[a].Position - alphapoints[a - 1].Position) * alphapoints[a].Focus); // //alpha // AddPosition(colpoints, alphapoints, positions, alphapoints[a].Position); //} //add first/last point if (positions.Count < 1) { positions.Add(0f, Color.Transparent); } if (!positions.ContainsKey(0f)) { if (filterColor != null) { float earliest = positions.Keys[0]; Color c = Color.Black; for (int i = 0; i < positions.Count && positions.Keys[i] == earliest; i++) { if (positions.Values[i].ToArgb() != Color.Black.ToArgb()) { c = positions.Values[i]; break; } } positions.Add(0f, c); } else { positions.Add(0f, positions.Values[0]); } } if (positions.Count < 2) { Color c = positions.Values[0]; if (filterColor != null && c.ToArgb() != ((Color)filterColor).ToArgb()) { c = Color.Black; } positions.Add(1f, c); } if (!positions.ContainsKey(1f)) { if (filterColor != null) { float latest = positions.Keys[positions.Count - 1]; Color c = Color.Black; for (int i = positions.Count - 1; i >= 0 && positions.Keys[i] == latest; i--) { if (positions.Values[i].ToArgb() != Color.Black.ToArgb()) { c = positions.Values[i]; break; } } positions.Add(1f, c); } else { positions.Add(1f, positions.Values[positions.Count - 1]); } } ColorBlend ret = new ColorBlend(); Color[] col = new Color[positions.Count]; positions.Values.CopyTo(col, 0); ret.Colors = col; float[] pos = new float[positions.Count]; positions.Keys.CopyTo(pos, 0); ret.Positions = pos; return(ret); }