private void gradientControl1_MouseMove(object sender, MouseEventArgs e) { if (!bInitialized) { return; } if (mSelectedPoint >= 0 && mSelectedPoint < gradientControl1.Gradient.Count - 1) { double translatedX = (double)e.X / (double)gradientControl1.ClientRectangle.Width; GradientPoint point = gradientControl1.Gradient[mSelectedPoint]; point.Alpha = System.Math.Min(System.Math.Max(0, translatedX), 1.0f); //-- now clamp the x according to their neighbors int leftNeighborIndex = mSelectedPoint - 1; if (leftNeighborIndex >= 0 && leftNeighborIndex < gradientControl1.Gradient.Count) { double neighborAlpha = gradientControl1.Gradient[leftNeighborIndex].Alpha; point.Alpha = System.Math.Max(neighborAlpha, point.Alpha); } int rightNeighborIndex = mSelectedPoint + 1; if (rightNeighborIndex >= 0 && rightNeighborIndex < gradientControl1.Gradient.Count) { double neighborAlpha = gradientControl1.Gradient[rightNeighborIndex].Alpha; point.Alpha = System.Math.Min(neighborAlpha, point.Alpha); } point.Alpha = System.Math.Round(point.Alpha, 2); refreshListBox(); gradientControl1.Invalidate(); } }
public void addPoint(double alpha, Color color) { if (!bInitialized) { return; } if (alpha > 1.0f) { alpha = 1.0f; } if (alpha < 0.0f) { alpha = 0.0f; } GradientPoint point = new GradientPoint(); point.Alpha = alpha; point.Color = color; mPointList.Add(point); Invalidate(); }
public GradientPoint clone() { GradientPoint clone = new GradientPoint(); clone.Alpha = Alpha; clone.Color = Color; return(clone); }
//-- interface public void addStage(Color color, double valueVariance, double alpha) { GradientPoint newStage = new GradientPoint(); newStage.Color = color; newStage.Alpha = alpha; mStages.Add(newStage); }
public void insertStage(int atIndex, Color color, double alpha) { GradientPoint newStage = new GradientPoint(); newStage.Color = color; newStage.Alpha = alpha; Stages.Insert(atIndex, newStage); }
private void fixupAlphaValues() { //-- fixup the alpha values for (int i = 0; i < mPointList.Count; i++) { GradientPoint p = mPointList[i]; int numSegments = mPointList.Count - 1; p.Alpha = ((double)(i) / (double)numSegments); } }
private void gradientControl1_MouseDoubleClick(object sender, MouseEventArgs e) { if (!bInitialized) { return; } //-- unselect mSelectedPoint = -1; colorDialog1.FullOpen = true; DialogResult result = colorDialog1.ShowDialog(); if (result != DialogResult.OK) { return; } Rectangle clientRect = gradientControl1.ClientRectangle; double translatedX = (double)e.X / (double)clientRect.Width; for (int i = 0; i < gradientControl1.Gradient.Count; i++) { GradientPoint point = gradientControl1.Gradient[i]; if (translatedX < point.Alpha) { //-- now clamp the x according to their neighbors int leftNeighborIndex = i - 1; if (leftNeighborIndex >= 0 && leftNeighborIndex < gradientControl1.Gradient.Count) { double neighborAlpha = gradientControl1.Gradient[leftNeighborIndex].Alpha; translatedX = System.Math.Max(neighborAlpha, translatedX); } int rightNeighborIndex = i; if (rightNeighborIndex >= 0 && rightNeighborIndex < gradientControl1.Gradient.Count) { double neighborAlpha = gradientControl1.Gradient[rightNeighborIndex].Alpha; translatedX = System.Math.Min(neighborAlpha, translatedX); } translatedX = System.Math.Round(translatedX, 2); gradientControl1.insertPoint(i, translatedX, colorDialog1.Color); refreshListBox(); gradientControl1.Invalidate(); break; } } }
public void insertPoint(int atIndex, double alpha, Color color) { if (!bInitialized) { return; } GradientPoint point = new GradientPoint(); point.Color = color; point.Alpha = alpha; mPointList.Insert(atIndex, point); }
public void addPoint(Color color) { if (!bInitialized) { return; } GradientPoint point = new GradientPoint(); point.Alpha = 0.0f; point.Color = color; mPointList.Add(point); fixupAlphaValues(); }
private void GradientControl_Paint(object sender, PaintEventArgs e) { if (mPointList == null) { return; } //-- this should only happen in design view if (bInitialized && mPointList.Count < 2) { clearPoints(); addPoint(0.0f, Color.Black); addPoint(1.0f, Color.White); Invalidate(); } Rectangle rect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height); if (e.ClipRectangle.Width == 0) { return; } for (int i = 0; i < mPointList.Count - 1; i++) { GradientPoint p0 = mPointList[i]; GradientPoint p1 = mPointList[i + 1]; int x0 = (int)System.Math.Round(p0.Alpha * (double)e.ClipRectangle.Width, MidpointRounding.AwayFromZero); int x1 = (int)System.Math.Round(p1.Alpha * (double)e.ClipRectangle.Width, MidpointRounding.AwayFromZero); rect.X = x0; rect.Width = x1 - x0; if (rect.Width <= 0) { continue; } drawGradientRegion(p0.Color, p1.Color, rect, e); if (i != 0 && i != mPointList.Count - 1) { drawMarker(x0, e); } } }
public void moveStageDown(int index) { //-- cannot move the endpoints if (index < 0 || index >= mStages.Count) { return; } int newIndex = index + 1; if (newIndex >= mStages.Count) { newIndex = mStages.Count - 1; } //swap members GradientPoint tempStage = mStages[newIndex]; mStages[newIndex].Color = mStages[index].Color; mStages[index].Color = tempStage.Color; }
private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { if (!bInitialized) { return; } e.DrawFocusRectangle(); int index = e.Index; if (index < 0 || index >= gradientControl1.Gradient.Count) { return; } int colorRectWidth = 40; int itemYAdditionalOffset = 4; GradientPoint item = gradientControl1.Gradient[index]; //-- Draw the Color Rectangle Rectangle rect = new Rectangle(); rect.X = e.Bounds.X; rect.Y = e.Bounds.Y; rect.Width = colorRectWidth; rect.Height = e.Bounds.Height - itemYAdditionalOffset; //-- fill the rectangle Brush colorBrush = new SolidBrush(item.Color); e.Graphics.FillRectangle(colorBrush, rect); colorBrush.Dispose(); //-- Draw the border Pen pen = new Pen(Color.Gray, 2); pen.Alignment = PenAlignment.Inset; pen.DashStyle = DashStyle.Solid; pen.LineJoin = LineJoin.Round; e.Graphics.DrawRectangle(pen, rect); pen.Dispose(); Brush brush = Brushes.Black; if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { brush = Brushes.White; } //-- Draw the back ground Brush brushBackground = new SolidBrush(e.BackColor); if (listBox1.Enabled == false) { brush = Brushes.Gray; brushBackground = Brushes.DarkGray; } e.Graphics.FillRectangle(brushBackground, e.Bounds.X + 45, e.Bounds.Y, e.Bounds.Width - colorRectWidth, e.Bounds.Height - itemYAdditionalOffset); e.Graphics.DrawString(item.ToString(), e.Font, brush, e.Bounds.X + 45, e.Bounds.Y); }