public void ParseColorStopWithoutValue() { var stop = ColorStop.Parse("#00a, #fff, #000"); Assert.Equal(Rgba32.Parse("#00a"), stop.Color); Assert.Null(stop.Position); }
private void generateColorStops() { int numColorStops = PaletteManager.CurrentPalette.Count; PaletteColorStops = new List <ColorStop>(); //pnl_ColorEditor.SuspendLayout(); for (int i = 0; i < PaletteManager.CurrentPalette.Count; i++) { PaletteColor pc = PaletteManager.CurrentPalette[i]; Color currentColorStopColor; if (PaletteManager.CurrentPalette.HSL) { currentColorStopColor = Palette.HSLtoRGBConversion(pc.Component1, pc.Component2, pc.Component3); } else { currentColorStopColor = Color.FromArgb((int)pc.Component1, (int)pc.Component2, (int)pc.Component3); } int ColorStopLocation = ((int)(pc.Location * NumOfDivisionsFromZero) * PaletteEditorDisplayColorWidthInPixels) + pctbx_PaletteEditor.Location.X - ColorStopOffset + PaletteEditorPaletteX; ColorStop newCS = new ColorStop(i, ColorStopLocation, currentColorStopColor, i == SelectedColorStop, new EventHandler(ColorStop_Click), new EventHandler(ColorStop_DoubleClick), new MouseEventHandler(ColorStop_MouseDown), new MouseEventHandler(ColorStop_MouseMove)); PaletteColorStops.Add(newCS); pctbx_PaletteEditor.Controls.Add(PaletteColorStops[i].Image); } //pnl_ColorEditor.ResumeLayout(false); }
public void ParseColorStop() { var stop = ColorStop.Parse("#00a 90%"); Assert.Equal(Rgba32.Parse("#00a"), stop.Color); Assert.Equal(0.9, stop.Position); }
/// <summary> /// Gets a cool Highlight brush for highlighting things. /// </summary> /// <param name="box">The rectangle in the box</param> /// <param name="selectionHighlight">The color to use for the higlight</param> /// <returns>The highlight brush.</returns> public static IBrush HighlightBrush(Rectangle box, Rgba32 selectionHighlight) { float med = selectionHighlight.GetBrightness(); float bright = med + 0.05f; if (bright > 1f) { bright = 1f; } float dark = med - 0.05f; if (dark < 0f) { dark = 0f; } Rgba32 brtCol = ColorFromHsl(selectionHighlight.GetHue(), selectionHighlight.GetSaturation(), bright); Rgba32 drkCol = ColorFromHsl(selectionHighlight.GetHue(), selectionHighlight.GetSaturation(), dark); Point p1 = new Point(box.X, box.Y - box.Height / 2); Point p2 = new Point(box.X + box.Width, box.Y - box.Height / 2); ColorStop[] colorStops = new ColorStop[2] { new ColorStop(0, brtCol), new ColorStop(0.5f, drkCol) }; return(new LinearGradientBrush(p1, p2, GradientRepetitionMode.None, colorStops)); }
public void ParseColorStopWithRgb() { var stop = ColorStop.Parse("rgb(153, 116, 186) 0%"); Assert.Equal(new Rgba32(153, 116, 186), stop.Color); Assert.Equal(0, stop.Position); }
public static RadialGradientBrush Resolve(Drawing.RadialGradientBrush radGradientBrush) { RadialGradientBrush glGradient = radGradientBrush.InnerBrush as RadialGradientBrush; if (glGradient == null) { //temp fix //check if some color stop has alpha //create a new one Build(radGradientBrush, out float[] v2f); glGradient = new RadialGradientBrush(v2f); ColorStop[] colorStops = radGradientBrush.ColorStops; for (int i = 0; i < colorStops.Length; ++i) { ColorStop stop = radGradientBrush.ColorStops[i]; if (stop.Color.A < 255 * 0.8) //temp fix 0.8 { glGradient._hasSignificateAlphaCompo = true; break; } } //create a single horizontal line linear gradient bmp //for texture look up //create MemBitmap for this lookup table GradientSpanGenExtensions.GenerateSampleGradientLine(radGradientBrush, out Color[] sampleColors); MemBitmap lookupBmp = new MemBitmap(sampleColors.Length, 1);//1 pixel height unsafe { int *ptr = (int *)MemBitmap.GetBufferPtr(lookupBmp).Ptr; for (int i = 0; i < sampleColors.Length; ++i) { Color c = sampleColors[i]; * ptr = (int)c.ToABGR(); ptr++; } } glGradient._lookupBmp = new GLBitmap(lookupBmp, true); glGradient._cx = radGradientBrush.StartPoint.X; glGradient._cy = radGradientBrush.StartPoint.Y; glGradient._r = (float)radGradientBrush.Length; if (radGradientBrush.CoordTransformer != null) { glGradient._invertedAff = (PixelFarm.CpuBlit.VertexProcessing.Affine)radGradientBrush.CoordTransformer.CreateInvert(); } radGradientBrush.InnerBrush = glGradient; } return(glGradient); }
/// <summary> /// Gets the color for a given coordinate given the color of the pixel before coloration. /// </summary> /// <param name="X">X-coordinate.</param> /// <param name="Y">Y-coordinate.</param> /// <param name="DestinationColor">Color before coloration.</param> /// <returns>Color to use for the corresponding pixel.</returns> public override Color GetColor(int X, int Y, Color DestinationColor) { int dx = X - this.x; int dy = Y - this.y; double t = Math.Sqrt(dx * dx + dy * dy); LinkedListNode <ColorStop> Loop = this.stops.First; ColorStop Stop0 = null; ColorStop Stop1 = Loop.Value; Loop = Loop.Next; while (!(Loop is null) && t > Stop1.Stop) { Stop0 = Stop1; Stop1 = Loop.Value; Loop = Loop.Next; } if (Stop0 is null) { return(Stop1.Color); } if (t > Stop1.Stop) { return(Stop1.Color); } double dt = Stop1.Stop - Stop0.Stop; if (dt <= 0) { return(Stop0.Color); } t = (t - Stop0.Stop) / dt; Color c0 = Stop0.Color; Color c1 = Stop1.Color; double u = 1 - t; int R = (int)(c0.R * u + c1.R * t + 0.5); int G = (int)(c0.G * u + c1.G * t + 0.5); int B = (int)(c0.B * u + c1.B * t + 0.5); int A = (int)(c0.A * u + c1.A * t + 0.5); return(Color.FromArgb(A, R, G, B)); }
public void ArbitraryGradients <TPixel>( TestImageProvider <TPixel> provider, int startX, int startY, int endX, int endY, float[] stopPositions, int[] stopColorCodes) where TPixel : struct, IPixel <TPixel> { TPixel[] colors = { NamedColors <TPixel> .Navy, NamedColors <TPixel> .LightGreen, NamedColors <TPixel> .Yellow, NamedColors <TPixel> .Red }; var coloringVariant = new StringBuilder(); ColorStop <TPixel>[] colorStops = new ColorStop <TPixel> [stopPositions.Length]; Rgba32 rgba = default; for (int i = 0; i < stopPositions.Length; i++) { TPixel color = colors[stopColorCodes[i % colors.Length]]; float position = stopPositions[i]; color.ToRgba32(ref rgba); colorStops[i] = new ColorStop <TPixel>(position, color); coloringVariant.AppendFormat(CultureInfo.InvariantCulture, "{0}@{1};", rgba.ToHex(), position); } FormattableString variant = $"({startX},{startY})_TO_({endX},{endY})__[{coloringVariant}]"; provider.VerifyOperation( image => { var unicolorLinearGradientBrush = new LinearGradientBrush <TPixel>( new SixLabors.Primitives.Point(startX, startY), new SixLabors.Primitives.Point(endX, endY), GradientRepetitionMode.None, colorStops); image.Mutate(x => x.Fill(unicolorLinearGradientBrush)); }, variant, false, false); }
public override IBrush GetBrush() { IBrush brush = base.GetBrush(); RectangleF bounds = Bounds; if (bounds.IsEmpty || bounds.Width == 0 || bounds.Height == 0) { return(brush); } ColorStop[] colorStops = new ColorStop[Colors.Length]; for (int i = 0; i < Colors.Length; i++) { colorStops[i] = new ColorStop(Positions[i], Colors[i]); } switch (GradientType) { case GradientType.Circular: { float radius = bounds.Width / 2; Point center = new Point((int)(bounds.X + bounds.Width / 2), (int)(bounds.Y + bounds.Height / 2)); brush = new RadialGradientBrush(center, radius, GradientRepetitionMode.None, colorStops); } break; //case GradientType.Contour: // break; case GradientType.Linear: Point start = new Point((int)bounds.X, (int)(bounds.Y - bounds.Height / 2)); Point end = new Point((int)(bounds.X + bounds.Width), (int)(bounds.Y - bounds.Height / 2)); brush = new LinearGradientBrush(start, end, GradientRepetitionMode.None, colorStops); break; case GradientType.Rectangular: { Point center = new Point((int)(bounds.X + bounds.Width / 2), (int)(bounds.Y + bounds.Height / 2)); Point referenceAxisEnd = new Point((int)bounds.X, center.Y); float axisRatio = bounds.Width / bounds.Height; brush = new EllipticGradientBrush(center, referenceAxisEnd, axisRatio, GradientRepetitionMode.None, colorStops); } break; } return(brush); }
public void MultiplePointGradients <TPixel>( TestImageProvider <TPixel> provider, int startX, int startY, int endX, int endY, float[] stopPositions, int[] stopColorCodes) where TPixel : unmanaged, IPixel <TPixel> { Color[] colors = { Color.Black, Color.Blue, Color.Red, Color.White, Color.Lime }; var coloringVariant = new StringBuilder(); var colorStops = new ColorStop[stopPositions.Length]; for (int i = 0; i < stopPositions.Length; i++) { Color color = colors[stopColorCodes[i % colors.Length]]; float position = stopPositions[i]; colorStops[i] = new ColorStop(position, color); Rgba32 rgba = color; coloringVariant.AppendFormat(CultureInfo.InvariantCulture, "{0}@{1};", rgba.ToHex(), position); } FormattableString variant = $"({startX},{startY})_TO_({endX},{endY})__[{coloringVariant}]"; provider.VerifyOperation( image => { var unicolorLinearGradientBrush = new LinearGradientBrush( new Point(startX, startY), new Point(endX, endY), GradientRepetitionMode.None, colorStops); image.Mutate(x => x.Fill(unicolorLinearGradientBrush)); }, variant, false, false); }
public void ParseColorStopList2() { var text = "rgb(153, 116, 186) 0%, rgb(194, 234, 9) 20%".AsSpan(); ColorStop stop; int read; stop = ColorStop.Read(text, out read); Assert.Equal(21, read); Assert.Equal(0, stop.Position); stop = ColorStop.Read(text.Slice(read + 2), out read); Assert.Equal(20, read); Assert.Equal(0.2, stop.Position); }
public void ResolveBrush(RadialGradientBrush radialGrBrush) { //for gradient : PointF p1 = radialGrBrush.StartPoint; PointF p2 = radialGrBrush.EndPoint; if (radialGrBrush.CoordTransformer != null) { _invertCoordTx = radialGrBrush.CoordTransformer.CreateInvert(); } _center_x = (int)Math.Round(p1.X); _center_y = (int)Math.Round(p1.Y); float r = (float)Math.Sqrt((p2.X - _center_x) * (p2.X - _center_x) + (p2.Y - _center_y) * (p2.Y - _center_y)); ColorStop[] colorStops = radialGrBrush.ColorStops; int pairCount = colorStops.Length - 1; _orgList = new LinearGradientPair[pairCount]; _pairList = new LinearGradientPair[_orgList.Length]; ColorStop c1 = ColorStop.Empty; for (int i = 0; i < pairCount; ++i) { ColorStop c0 = colorStops[i]; c1 = colorStops[i + 1]; var pairN = new LinearGradientPair( c0.Offset * r, //to actual pixel c1.Offset * r, //to actual pixel c0.Color, c1.Color); _orgList[i] = pairN; } _endColor = c1.Color; this.SpreadMethod = radialGrBrush.SpreadMethod; Opactiy = 1; }
public void ParseColorStopList() { var text = "#00a 90%, #000 91%, #fff 92%".AsSpan(); int read; var stop1 = ColorStop.Read(text, out read); Assert.Equal(8, read); var stop2 = ColorStop.Read(text.Slice(read + 2), out read); Assert.Equal(8, read); Assert.Equal(Rgba32.Parse("#00a"), (Rgba32)stop1.Color); Assert.Equal(0.9, stop1.Position); Assert.Equal(Rgba32.Parse("#000"), (Rgba32)stop2.Color); }
static void CalculateLinearGradientVxs( ArrayList <ColorAndCoord> vrx, bool isFirstPane, bool isLastPane, float x1, float distance, ColorStop stop1, ColorStop stop2) { //TODO: review here again //should not fix 600,800,1800 etc Color c1_color = stop1.Color; Color c2_color = stop2.Color; if (isFirstPane) { //left solid rect pane AddRect(vrx, c1_color, c1_color, -600, -800, x1 + 600, 1800); } //color gradient pane AddRect(vrx, c1_color, c2_color, x1, -800, distance, 1800); if (isLastPane) { //right solid pane if (1200 - (x1 + distance) > 0) { AddRect(vrx, c2_color, c2_color, (x1 + distance), -800, 1200 - (x1 + distance), 1800); } } }
private static void selectColorStop() { ColorStop clickedCS = PaletteColorStops[SelectedColorStop]; if (!clickedCS.Selected) { for (int clrStp = 0; clrStp < PaletteColorStops.Count; clrStp++) { if (PaletteColorStops[clrStp].Selected) { PaletteColorStops[clrStp].Selected = false; PaletteColorStops[clrStp].Image = PaletteColorStops[clrStp].Image; PaletteColorStops[clrStp].Image.Refresh(); } } clickedCS.Selected = true; PaletteColorStops[SelectedColorStop].Image.Refresh(); } setColorStopSliderChevLocs(); }
static void CalculateLinearGradientVxs( ArrayList <VertexC4V3f> vrx, bool isFirstPane, bool isLastPane, float x1, float distance, ColorStop stop1, ColorStop stop2) { //TODO: review here again Color c1 = stop1.Color; Color c2 = stop2.Color; //1. gradient distance if (isFirstPane) { //left solid rect pane AddRect(vrx, c1.ToABGR(), c1.ToABGR(), -600, -800, x1 + 600, 1800); } //color gradient pane AddRect(vrx, c1.ToABGR(), c2.ToABGR(), x1, -800, distance, 1800); if (isLastPane) { //right solid pane if (1200 - (x1 + distance) > 0) { AddRect(vrx, c2.ToABGR(), c2.ToABGR(), (x1 + distance), -800, 1200 - (x1 + distance), 1800); } } }
public static object Parse(string s) { if (string.IsNullOrEmpty(s)) { return(Color.White); } Crow.Gradient tmp; string[] stops = s.Trim().Split('|'); switch (stops[0].Trim()) { case "vgradient": tmp = new Gradient(Type.Vertical); break; case "hgradient": tmp = new Gradient(Type.Horizontal); break; case "ogradient": tmp = new Gradient(Type.Oblic); break; default: throw new Exception("Unknown gradient type: " + stops [0]); } for (int i = 1; i < stops.Length; i++) { tmp.Stops.Add((ColorStop)ColorStop.Parse(stops[i])); } return(tmp); }
public void setColorStop(int order, ColorStop s) { map.Add(order, s); }
public void ResolveBrush(LinearGradientBrush linearGrBrush) { PointF p1 = linearGrBrush.StartPoint; PointF p2 = linearGrBrush.EndPoint; //assume horizontal line _beginX = p1.X; _beginY = p1.Y; _endX = p2.X; _endY = p2.Y; //-------------- //find transformation matrix double angle = Math.Atan2(p2.Y - p1.Y, p2.X - p1.X); ICoordTransformer rotateTx = Affine.NewRotation(angle); if (linearGrBrush.CoordTransformer != null) { //*** IMPORTANT : matrix transform order !** rotateTx = linearGrBrush.CoordTransformer.MultiplyWith(rotateTx); } _transformBackToHorizontal = rotateTx.CreateInvert(); _totalLen = (float)Math.Sqrt((_endX - _beginX) * (_endX - _beginX) + (_endY - _beginY) * (_endY - _beginY)); double tmpX = _beginX; double tmpY = _beginY; _transformBackToHorizontal.Transform(ref tmpX, ref tmpY); _beginX = (float)tmpX; _beginY = (float)tmpY; //-------------- tmpX = _endX; tmpY = _endY; _transformBackToHorizontal.Transform(ref tmpX, ref tmpY); _endX = (float)tmpX; _endY = (float)tmpY; //-------------- ColorStop[] colorStops = linearGrBrush.ColorStops; int pairCount = colorStops.Length - 1; _pairList = new LinearGradientPair[pairCount]; ColorStop c0 = ColorStop.Empty; ColorStop c1 = ColorStop.Empty; for (int i = 0; i < pairCount; ++i) { c0 = colorStops[i]; c1 = colorStops[i + 1]; if (i == 0) { _beginColor = c0.Color; } var pairN = new LinearGradientPair( _beginX + c0.Offset * _totalLen, //to actual pixel _beginX + c1.Offset * _totalLen, //to actual pixel c0.Color, c1.Color); _pairList[i] = pairN; } this.SpreadMethod = linearGrBrush.SpreadMethod; _endColor = c1.Color; }
/// <summary> /// we do not store input linearGradient /// </summary> /// <param name="linearGradient"></param> static void Build(Drawing.LinearGradientBrush linearGradient, out float[] v2f, out float[] colors) { ColorStop[] colorStops = linearGradient.ColorStops; s_vertices.Clear(); s_v2fList.Clear(); s_colorList.Clear(); float x_1 = linearGradient.StartPoint.X; float y_1 = linearGradient.StartPoint.Y; double angleRad = linearGradient.Angle; double totalLen = linearGradient.Length; int pairCount = colorStops.Length - 1; ColorStop c0 = ColorStop.Empty; ColorStop c1 = ColorStop.Empty; //create a simple horizontal linear gradient bar //and we will rotate and translate it to target pos for (int i = 0; i < pairCount; ++i) { c0 = colorStops[i]; c1 = colorStops[i + 1]; CalculateLinearGradientVxs(s_vertices, i == 0, i == pairCount - 1, (float)(x_1 + (c0.Offset * totalLen)), (float)((c1.Offset - c0.Offset) * totalLen), c0, c1); } var txMatrix = PixelFarm.CpuBlit.VertexProcessing.Affine.NewMatix( PixelFarm.CpuBlit.VertexProcessing.AffinePlan.Translate(-x_1, -y_1), PixelFarm.CpuBlit.VertexProcessing.AffinePlan.Rotate(angleRad), PixelFarm.CpuBlit.VertexProcessing.AffinePlan.Translate(x_1, y_1) ); //---------------------------------- int j = s_vertices.Count; for (int m = 0; m < j; ++m) { VertexC4V3f v = s_vertices[m]; double v_x = v.x; double v_y = v.y; txMatrix.Transform(ref v_x, ref v_y); //vrx[i] = new VertexC4V3f(v.color, (float)v_x, (float)v_y); s_v2fList.Add((float)v_x); s_v2fList.Add((float)v_y); uint color = v.color; //a,b,g,r s_colorList.Add((color & 0xff) / 255f); //r s_colorList.Add(((color >> 8) & 0xff) / 255f); //g s_colorList.Add(((color >> 16) & 0xff) / 255f); //b s_colorList.Add(((color >> 24) & 0xff) / 255f); //a } v2f = s_v2fList.ToArray(); colors = s_colorList.ToArray(); }
private void pctbx_PaletteEditor_DoubleClick(object sender, EventArgs e) { Point formScreenCoordLoc = pctbx_PaletteEditor.PointToScreen(new Point(0, 0)); Point convertedMouseCoords = new Point(Control.MousePosition.X - formScreenCoordLoc.X, Control.MousePosition.Y - formScreenCoordLoc.Y); int newCSX = convertedMouseCoords.X - PaletteEditorPaletteX; int newCSLowerBound, newCSUpperBound; int LowerBoundCS = 0, UpperBoundCS = PaletteColorStops.Count - 1; if (newCSX >= 0 && newCSX < pctbx_PaletteEditor.Width) { int LowerBound = 0; for (int clrStp = 0; clrStp < PaletteColorStops.Count; clrStp++) { if (PaletteColorStops[clrStp].CSLocation <newCSX && PaletteColorStops[clrStp].CSLocation> LowerBound) { LowerBound = PaletteColorStops[clrStp].CSLocation; LowerBoundCS = clrStp; } } newCSLowerBound = LowerBound + (int)(PaletteEditorPaletteWidth * 0.02); int UpperBound = pctbx_PaletteEditor.Width; for (int clrStp = 0; clrStp < PaletteColorStops.Count; clrStp++) { if (PaletteColorStops[clrStp].CSLocation > newCSX && PaletteColorStops[clrStp].CSLocation < UpperBound) { UpperBound = PaletteColorStops[clrStp].CSLocation; UpperBoundCS = clrStp; } } newCSUpperBound = UpperBound - (int)(PaletteEditorPaletteWidth * 0.02); if (newCSLowerBound < newCSX && newCSUpperBound > newCSX && UpperBoundCS > LowerBoundCS) { decimal newColorStopLocation = (decimal)newCSX / NumOfDivisionsFromZero; Color newCSColor = ((Bitmap)pctbx_PaletteEditor.Image).GetPixel(newCSX, (int)(pctbx_PaletteEditor.Image.Height / 2)); int ColorStopLocation = ((int)(newColorStopLocation * NumOfDivisionsFromZero) * PaletteEditorDisplayColorWidthInPixels) + pctbx_PaletteEditor.Location.X - ColorStopOffset + PaletteEditorPaletteX; ColorStop newCS = new ColorStop(100, ColorStopLocation, newCSColor, false, new EventHandler(ColorStop_Click), new EventHandler(ColorStop_DoubleClick), new MouseEventHandler(ColorStop_MouseDown), new MouseEventHandler(ColorStop_MouseMove)); PaletteColorStops.Insert(UpperBoundCS, newCS); if (PaletteManager.CurrentPalette.HSL) { PaletteColor pc = new PaletteColor(newColorStopLocation, (decimal)newCSColor.R, (decimal)newCSColor.G, (decimal)newCSColor.B); pc.convertToHSL(); PaletteManager.CurrentPalette.Insert(UpperBoundCS, pc); } else { PaletteColor pc = new PaletteColor(newColorStopLocation, (decimal)newCSColor.R, (decimal)newCSColor.G, (decimal)newCSColor.B); PaletteManager.CurrentPalette.Insert(UpperBoundCS, pc); } int id = 0; foreach (ColorStop cs in PaletteColorStops) { cs.ID = id; cs.Image.Name = id.ToString(); id++; } pctbx_PaletteEditor.Controls.Add(PaletteColorStops[UpperBoundCS].Image); SelectedColorStop = UpperBoundCS; selectColorStop(); } } }