/// <inheritdoc/>
        protected override bool OnDraw(CanvasDevice device, CanvasDrawingSession session, Vector2 size)
        {
            // Create our Brush
            if (GradientStops != null && GradientStops.Count > 0)
            {
                var gradientBrush = new CanvasRadialGradientBrush(
                    device,
                    GradientStops.ToWin2DGradientStops(),
                    SpreadMethod.ToEdgeBehavior(),
                    (CanvasAlphaMode)(int)AlphaMode,
                    ColorInterpolationMode.ToCanvasColorSpace(),
                    CanvasColorSpace.Srgb,
                    CanvasBufferPrecision.Precision8UIntNormalized)
                {
                    // Calculate Surface coordinates from 0.0-1.0 range given in WPF brush
                    RadiusX = size.X * (float)RadiusX,
                    RadiusY = size.Y * (float)RadiusY,
                    Center  = size * Center.ToVector2(),

                    // Calculate Win2D Offset from origin/center used in WPF brush
                    OriginOffset = size * (GradientOrigin.ToVector2() - Center.ToVector2()),
                };

                // Use brush to draw on our canvas
                session.FillRectangle(size.ToRect(), gradientBrush);

                gradientBrush.Dispose();

                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        internal override CALayer GetLayer(CGSize size)
        {
            var gradientLayer = new CAGradientLayer
            {
                Colors    = GradientStops.SelectToArray(gs => (CGColor)GetColorWithOpacity(gs.Color)),
                Locations = GradientStops.SelectToArray(gs => new NSNumber(gs.Offset))
            };
            var transform = RelativeTransform;

            var startPoint = transform?.TransformPoint(StartPoint) ?? StartPoint;
            var endPoint   = transform?.TransformPoint(EndPoint) ?? EndPoint;

            var width  = size.Width;
            var height = size.Height;

            if (transform != null)
            {
                var matrix = transform.ToMatrix(Point.Zero, new Size(width, height));
                matrix.M31 *= (float)width;
                matrix.M32 *= (float)height;

                startPoint = matrix.Transform(startPoint);
                endPoint   = matrix.Transform(endPoint);
            }
            if (MappingMode == BrushMappingMode.Absolute)
            {
                startPoint = new Point(startPoint.X / size.Width, startPoint.Y / size.Height);
                endPoint   = new Point(endPoint.X / size.Width, endPoint.Y / size.Height);
            }

            gradientLayer.StartPoint = startPoint;
            gradientLayer.EndPoint   = endPoint;

            return(gradientLayer);
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AbstractGradient"/> class.
 /// </summary>
 /// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
 protected AbstractGradient(params GradientStop[] gradientStops)
 {
     foreach (GradientStop gradientStop in gradientStops)
     {
         GradientStops.Add(gradientStop);
     }
 }
        public LinearGradientBrush(Color startColor, Color endColor, double angle)
        {
            EndPoint = EndPointFromAngle(angle);

            GradientStops.Add(new GradientStop(startColor, 0.0f));
            GradientStops.Add(new GradientStop(endColor, 1.0f));
        }
Esempio n. 5
0
        /// <inheritdoc />
        public virtual void Move(double offset)
        {
            offset /= 360.0;

            foreach (GradientStop gradientStop in GradientStops)
            {
                gradientStop.Offset += offset;
            }

            while (GradientStops.All(x => x.Offset > 1))
            {
                foreach (GradientStop gradientStop in GradientStops)
                {
                    gradientStop.Offset -= 1;
                }
            }

            while (GradientStops.All(x => x.Offset < 0))
            {
                foreach (GradientStop gradientStop in GradientStops)
                {
                    gradientStop.Offset += 1;
                }
            }
        }
Esempio n. 6
0
        /// <inheritdoc />
        /// <summary>
        /// Gets the linear interpolated <see cref="T:RGB.NET.Core.Color" /> at the given offset.
        /// </summary>
        /// <param name="offset">The percentage offset to take the color from.</param>
        /// <returns>The <see cref="T:RGB.NET.Core.Color" /> at the specific offset.</returns>
        public override Color GetColor(double offset)
        {
            if (GradientStops.Count == 0)
            {
                return(Color.Transparent);
            }
            if (GradientStops.Count == 1)
            {
                return(GradientStops[0].Color);
            }

            if (_isOrderedGradientListDirty)
            {
                _orderedGradientStops = new LinkedList <GradientStop>(GradientStops.OrderBy(x => x.Offset));
            }

            (GradientStop gsBefore, GradientStop gsAfter) = GetEnclosingGradientStops(offset, _orderedGradientStops, WrapGradient);

            double blendFactor = 0;

            if (!gsBefore.Offset.Equals(gsAfter.Offset))
            {
                blendFactor = ((offset - gsBefore.Offset) / (gsAfter.Offset - gsBefore.Offset));
            }

            double colA = ((gsAfter.Color.A - gsBefore.Color.A) * blendFactor) + gsBefore.Color.A;
            double colR = ((gsAfter.Color.R - gsBefore.Color.R) * blendFactor) + gsBefore.Color.R;
            double colG = ((gsAfter.Color.G - gsBefore.Color.G) * blendFactor) + gsBefore.Color.G;
            double colB = ((gsAfter.Color.B - gsBefore.Color.B) * blendFactor) + gsBefore.Color.B;

            return(new Color(colA, colR, colG, colB));
        }
Esempio n. 7
0
        internal override UIElement ToSvgElement()
        {
            var center = Center;

            var radiusX = RadiusX;
            var radiusY = RadiusY;

            if (MappingMode != BrushMappingMode.RelativeToBoundingBox)
            {
                // TODO: not supported right now
            }

            var linearGradient = new SvgElement("radialGradient");

            // TODO: support ellipse shaped radial on SVG using a gradientTransform.
            var radius = (radiusX + radiusY) / 2d;

            linearGradient.SetAttribute(
                ("cx", center.X.ToStringInvariant()),
                ("cy", center.Y.ToStringInvariant()),
                ("r", radius.ToStringInvariant())
                );

            var stops = GradientStops
                        .Select(stop => $"<stop offset=\"{stop.Offset.ToStringInvariant()}\" style=\"stop-color:{stop.Color.ToHexString()}\" />");

            linearGradient.SetHtmlContent(string.Join(Environment.NewLine, stops));

            return(linearGradient);
        }
Esempio n. 8
0
        protected override Paint GetPaintInner(Windows.Foundation.Rect destinationRect)
        {
            var paint = new Paint();

            // Android LinearGradient requires two ore more stop points.
            if (GradientStops.Count >= 2)
            {
                var colors    = GradientStops.Select(s => ((Android.Graphics.Color)s.Color).ToArgb()).ToArray();
                var locations = GradientStops.Select(s => (float)s.Offset).ToArray();

                var width  = destinationRect.Width;
                var height = destinationRect.Height;

                var transform = RelativeTransform?.ToNative(size: new Windows.Foundation.Size(width, height), isBrush: true);

                //Matrix .MapPoints takes an array of floats
                var pts = new[] { StartPoint, EndPoint }
                .Select(p => new float[] { (float)(p.X * width), (float)(p.Y * height) })
                .SelectMany(p => p)
                .ToArray();

                transform?.MapPoints(pts);


                var shader = new LinearGradient(pts[0], pts[1], pts[2], pts[3], colors, locations, Shader.TileMode.Mirror);

                paint.SetShader(shader);
            }

            return(paint);
        }
Esempio n. 9
0
 private void Remove_Click(object sender, RoutedEventArgs e)
 {
     if (GradientStops != null && SelectedGradientStop != null)
     {
         GradientStops.Remove(SelectedGradientStop);
         RefreshGradientStops();
     }
 }
Esempio n. 10
0
        public LinearGradientBrush(Color color1, Color color2, Point startPoint, Point endPoint)
        {
            StartPoint = startPoint;
            EndPoint   = endPoint;

            GradientStops.Add(new GradientStop(color1, 0.0));
            GradientStops.Add(new GradientStop(color2, 1.0));
        }
        public LinearGradientBrush(Color startColor, Color endColor, Point startPoint, Point endPoint)
        {
            StartPoint = startPoint;
            EndPoint   = endPoint;

            GradientStops.Add(new GradientStop(startColor, 0.0f));
            GradientStops.Add(new GradientStop(endColor, 1.0f));
        }
Esempio n. 12
0
        public void Render(DrawingContext context, Size size, Thickness depth, Thickness extent, CornerRadius radii, Color bg)
        {
            Color bgTrans = new Color(0, bg.R, bg.G, bg.B);

            GradientStops stops = new GradientStops()
            {
                new GradientStop(bg, 0),
                new GradientStop(bgTrans, 1)
            };

            Grid grid = new Grid()
            {
                ColumnDefinitions = new ColumnDefinitions(depth.Left + ", Auto, " + depth.Right),
                RowDefinitions    = new RowDefinitions(depth.Top + ", Auto, " + depth.Bottom)
            };

            RelativePoint tlCenter = new RelativePoint(1, 1, RelativeUnit.Relative);
            Rectangle     tl       = new Rectangle()
            {
                Fill = new RadialGradientBrush()
                {
                    Center         = tlCenter,
                    GradientOrigin = tlCenter,
                    GradientStops  = stops
                }
            };
            RelativePoint trCenter = new RelativePoint(0, 1, RelativeUnit.Relative);
            Rectangle     tr       = new Rectangle()
            {
                Fill = new RadialGradientBrush()
                {
                    Center         = trCenter,
                    GradientOrigin = trCenter,
                    GradientStops  = stops
                }
            };
            RelativePoint brCenter = new RelativePoint(0, 0, RelativeUnit.Relative);
            Rectangle     br       = new Rectangle()
            {
                Fill = new RadialGradientBrush()
                {
                    Center         = brCenter,
                    GradientOrigin = brCenter,
                    GradientStops  = stops
                }
            };
            RelativePoint blCenter = new RelativePoint(1, 1, RelativeUnit.Relative);
            Rectangle     bl       = new Rectangle()
            {
                Fill = new RadialGradientBrush()
                {
                    Center         = blCenter,
                    GradientOrigin = blCenter,
                    GradientStops  = stops
                }
            };
            VisualBrush brush = new VisualBrush();
        }
Esempio n. 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AbstractGradient"/> class.
        /// </summary>
        /// <param name="wrapGradient">Specifies whether the gradient should wrapp or not (see <see cref="WrapGradient"/> for an example of what this means).</param>
        /// <param name="gradientStops">The stops with which the gradient should be initialized.</param>
        protected AbstractGradient(bool wrapGradient, params GradientStop[] gradientStops)
        {
            this.WrapGradient = wrapGradient;

            foreach (GradientStop gradientStop in gradientStops)
            {
                GradientStops.Add(gradientStop);
            }
        }
Esempio n. 14
0
 public LinearGradientBrush GetBrush()
 {
     return(new LinearGradientBrush
     {
         StartPoint = StartPoint,
         EndPoint = EndPoint,
         GradientStops = new GradientStopCollection(GradientStops?.Select(x => new GradientStop(x.Color, x.Offset)) ?? Enumerable.Empty <GradientStop>())
     });
 }
Esempio n. 15
0
 public override int GetHashCode()
 {
     unchecked
     {
         int hashCode = Name.GetHashCode();
         hashCode = (hashCode * 397) ^ Type.GetHashCode();
         hashCode = (hashCode * 397) ^ GradientStops.GetHashCode();
         return(hashCode);
     }
 }
Esempio n. 16
0
        /// <summary>
        /// Gets the linear interpolated color at the given offset.
        /// </summary>
        /// <param name="offset">The percentage offset to take the color from.</param>
        /// <returns>The color at the specific offset.</returns>
        public override CorsairColor GetColor(float offset)
        {
            if (GradientStops.Count == 0)
            {
                return(CorsairColor.Transparent);
            }
            if (GradientStops.Count == 1)
            {
                return(GradientStops.First().Color);
            }

            GradientStop gsBefore;
            GradientStop gsAfter;

            IList <GradientStop> orderedStops = GradientStops.OrderBy(x => x.Offset).ToList();

            if (WrapGradient)
            {
                gsBefore = orderedStops.LastOrDefault(n => n.Offset <= offset);
                if (gsBefore == null)
                {
                    GradientStop lastStop = orderedStops[orderedStops.Count - 1];
                    gsBefore = new GradientStop(lastStop.Offset - 1f, lastStop.Color);
                }

                gsAfter = orderedStops.FirstOrDefault(n => n.Offset >= offset);
                if (gsAfter == null)
                {
                    GradientStop firstStop = orderedStops[0];
                    gsAfter = new GradientStop(firstStop.Offset + 1f, firstStop.Color);
                }
            }
            else
            {
                offset = ClipOffset(offset);

                gsBefore = orderedStops.Last(n => n.Offset <= offset);
                gsAfter  = orderedStops.First(n => n.Offset >= offset);
            }

            float blendFactor = 0f;

            if (!gsBefore.Offset.Equals(gsAfter.Offset))
            {
                blendFactor = ((offset - gsBefore.Offset) / (gsAfter.Offset - gsBefore.Offset));
            }

            byte colA = (byte)((gsAfter.Color.A - gsBefore.Color.A) * blendFactor + gsBefore.Color.A);
            byte colR = (byte)((gsAfter.Color.R - gsBefore.Color.R) * blendFactor + gsBefore.Color.R);
            byte colG = (byte)((gsAfter.Color.G - gsBefore.Color.G) * blendFactor + gsBefore.Color.G);
            byte colB = (byte)((gsAfter.Color.B - gsBefore.Color.B) * blendFactor + gsBefore.Color.B);

            return(new CorsairColor(colA, colR, colG, colB));
        }
Esempio n. 17
0
 public RadialGradientBrush GetBrush()
 {
     return(new RadialGradientBrush
     {
         Center = Center,
         RadiusX = RadiusX,
         RadiusY = RadiusY,
         GradientOrigin = GradientOrigin,
         GradientStops = new GradientStopCollection(GradientStops?.Select(x => new GradientStop(x.Color, x.Offset)) ?? Enumerable.Empty <GradientStop>())
     });
 }
Esempio n. 18
0
 private void Down_Click(object sender, RoutedEventArgs e)
 {
     if (GradientStops != null && SelectedGradientStop != null && GradientStops.IndexOf(SelectedGradientStop) < GradientStops.Count - 1)
     {
         var gs  = SelectedGradientStop;
         int idx = GradientStops.IndexOf(gs);
         GradientStops.RemoveAt(idx);
         GradientStops.Insert(idx + 1, gs);
         SelectedGradientStop = gs;
         RefreshGradientStops();
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="RadialGradientBrush"/> class
 /// with two colors specified for GradientStops at
 /// offsets 0.0 and 1.0.
 /// </summary>
 /// <param name="startColor"> The Color at offset 0.0. </param>
 /// <param name="endColor"> The Color at offset 1.0. </param>
 public RadialGradientBrush(Color startColor, Color endColor)
     : this()
 {
     GradientStops.Add(new GradientStop()
     {
         Color = startColor, Offset = 0.0
     });
     GradientStops.Add(new GradientStop()
     {
         Color = endColor, Offset = 1.0
     });
 }
        internal CAGradientLayer GetLayer(CGSize size)
        {
            var gradientLayer = new CAGradientLayer();

            gradientLayer.Colors    = GradientStops.Select(gs => (CGColor)gs.Color).ToArray();
            gradientLayer.Locations = GradientStops.Select(gs => new NSNumber(gs.Offset)).ToArray();
            var transform = RelativeTransform?.ToNativeTransform(size);

            gradientLayer.StartPoint = transform?.TransformPoint(StartPoint) ?? StartPoint;
            gradientLayer.EndPoint   = transform?.TransformPoint(EndPoint) ?? EndPoint;

            return(gradientLayer);
        }
Esempio n. 21
0
        /// <summary>
        ///   Clips the offset and ensures, that it is inside the bounds of the stop list.
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        protected float ClipOffset(float offset)
        {
            var max = GradientStops.Max(n => n.Offset);

            if (offset > max)
            {
                return(max);
            }

            var min = GradientStops.Min(n => n.Offset);

            return(offset < min ? min : offset);
        }
Esempio n. 22
0
        /// <summary>
        /// Clips the offset and ensures, that it is inside the bounds of the stop list.
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        protected double ClipOffset(double offset)
        {
            double max = GradientStops.Max(n => n.Offset);

            if (offset > max)
            {
                return(max);
            }

            double min = GradientStops.Min(n => n.Offset);

            return(offset < min ? min : offset);
        }
        protected internal override Shader GetShader(Size size)
        {
            if (GradientStops.Count == 0)
            {
                return(null);
            }

            var colors    = GradientStops.SelectToList(s => ((Android.Graphics.Color)GetColorWithOpacity(s.Color)).ToArgb());
            var locations = GradientStops.SelectToList(s => (float)s.Offset);

            if (GradientStops.Count == 1)
            {
                // Android LinearGradient requires two ore more stop points.
                // We work around this by duplicating the first gradient stop.
                colors.Add(colors[0]);
                locations.Add(locations[0]);
            }

            var width  = size.Width;
            var height = size.Height;

            Android.Graphics.Matrix nativeTransformMatrix = null;
            if (RelativeTransform != null)
            {
                var matrix = RelativeTransform.ToMatrix(Point.Zero, new Size(width, height));
                matrix.M31           *= (float)width;
                matrix.M32           *= (float)height;
                nativeTransformMatrix = matrix.ToNative();
            }

            //Matrix .MapPoints takes an array of floats
            var pts = MappingMode == BrushMappingMode.RelativeToBoundingBox
                                ? new[]
            {
                (float)(StartPoint.X * width),
                (float)(StartPoint.Y * height),
                (float)(EndPoint.X * width),
                (float)(EndPoint.Y * height)
            }
                                : new[]
            {
                (float)ViewHelper.LogicalToPhysicalPixels(StartPoint.X),
                (float)ViewHelper.LogicalToPhysicalPixels(StartPoint.Y),
                (float)ViewHelper.LogicalToPhysicalPixels(EndPoint.X),
                (float)ViewHelper.LogicalToPhysicalPixels(EndPoint.Y)
            };

            nativeTransformMatrix?.MapPoints(pts);
            nativeTransformMatrix?.Dispose();
            return(new LinearGradient(pts[0], pts[1], pts[2], pts[3], colors.ToArray(), locations.ToArray(), Shader.TileMode.Clamp));
        }
Esempio n. 24
0
        public RadialGradientBrush(Color startColor, Color endColor)
            : this()
        {
            GradientStop start = new GradientStop();

            start.Color  = startColor;
            start.Offset = 0.0d;
            GradientStops.Add(start);

            GradientStop stop = new GradientStop();

            stop.Color  = endColor;
            stop.Offset = 1.0d;
            GradientStops.Add(stop);
        }
Esempio n. 25
0
        public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager)
        {
            Detach();
            base.DeepCopy(source, copyManager);
            GradientBrush b = (GradientBrush)source;

            ColorInterpolationMode = b.ColorInterpolationMode;
            SpreadMethod           = b.SpreadMethod;
            MappingMode            = b.MappingMode;
            foreach (GradientStop stop in b.GradientStops)
            {
                GradientStops.Add(copyManager.GetCopy(stop));
            }
            Attach();
        }
Esempio n. 26
0
 public RadialGradient(float x0, float y0, float r, Color[] colors, float[] positions)
 {
     _x0 = x0;
     _y0 = y0;
     _r  = r;
     _canvasGradientStopCollection = new GradientStops();
     for (var i = 0; i < colors.Length; i++)
     {
         _canvasGradientStopCollection.Add(new GradientStop
         {
             Color  = colors[i],
             Offset = positions[i]
         });
     }
 }
Esempio n. 27
0
        /// <summary>
        /// Lấy màu radiant
        /// </summary>
        /// <param name="gradientStops"></param>
        /// <param name="msoGradientStyle"></param>
        /// <returns></returns>
        private GradientColor GetRadialColor(GradientStops gradientStops, MsoGradientStyle msoGradientStyle)
        {
            GradientColor gradientColor = new GradientColor();

            gradientColor.Type = GradientColorType.Radial;
            for (int i = 1; i <= gradientStops.Count; i++)
            {
                gradientColor.LinearElements.Add(new LinearElement()
                {
                    Color = ConvertColor(gradientStops[i].Color.RGB), SpecialName = GetSpecialName(gradientStops[i].Color.ObjectThemeColor), Brightness = gradientStops[i].Color.Brightness, Offset = gradientStops[i].Position
                });
            }
            gradientColor.LinearElements = gradientColor.LinearElements.OrderBy(s => s.Offset).ToList();
            return(gradientColor);
        }
Esempio n. 28
0
        internal CAGradientLayer GetLayer(CGSize size)
        {
            var gradientLayer = new CAGradientLayer();

            gradientLayer.Colors    = GradientStops.Select(gs => (CGColor)gs.Color).ToArray();
            gradientLayer.Locations = GradientStops.Select(gs => new NSNumber(gs.Offset)).ToArray();
            var transform = RelativeTransform;

#if __IOS__
            gradientLayer.StartPoint = transform?.TransformPoint(StartPoint) ?? StartPoint;
            gradientLayer.EndPoint   = transform?.TransformPoint(EndPoint) ?? EndPoint;
            return(gradientLayer);
#elif __MACOS__
            throw new NotImplementedException();
#endif
        }
Esempio n. 29
0
 public LinearGradient(float x0, float y0, float x1, float y1, Color[] colors, float[] positions)
 {
     _x0 = x0;
     _y0 = y0;
     _x1 = x1;
     _y1 = y1;
     _canvasGradientStopCollection = new GradientStops();
     for (var i = 0; i < colors.Length; i++)
     {
         _canvasGradientStopCollection.Add(new GradientStop
         {
             Color  = colors[i],
             Offset = positions[i]
         });
     }
 }
Esempio n. 30
0
        internal string ToCssString(Size size)
        {
            var startPoint = StartPoint;
            var endPoint   = EndPoint;

            var xDiff = (endPoint.X * size.Width) - (startPoint.X * size.Width);
            var yDiff = (startPoint.Y * size.Height) - (endPoint.Y * size.Height);

            var angle = Math.Atan2(xDiff, yDiff);

            var stops = string.Join(
                ",",
                GradientStops.Select(p => $"{GetColorWithOpacity(p.Color).ToCssString()} {(p.Offset * 100).ToStringInvariant()}%"));

            return($"linear-gradient({angle}rad,{stops})");
        }