2
// GradientBackendHandler.cs
5
// Eric Maupin <ermau@xamarin.com>
7
// Copyright (c) 2012 Xamarin, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
30
using System.Drawing.Drawing2D;
34
using Color = Xwt.Drawing.Color;
35
using DrawingColor = System.Drawing.Color;
37
namespace Xwt.WPFBackend
39
public class GradientBackendHandler
40
: Backend, IGradientBackendHandler
42
public object CreateLinear (double x0, double y0, double x1, double y1)
44
return new LinearGradient (
45
new PointF ((float) x0, (float) y0),
46
new PointF ((float) x1, (float) y1));
49
public object CreateRadial (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1)
51
return new RadialGradient (cx0, cy0, radius0, cx1, cy1, radius1);
54
public void AddColorStop (object backend, double position, Color color)
56
((GradientBase)backend).ColorStops.Add (new Tuple<double, Color> (position, color));
60
internal class RadialGradient
67
public RadialGradient (double cx0, double cy0, double radius0, double cx1, double cy1, double radius1)
78
public override Brush CreateBrush ()
80
var path = new GraphicsPath ();
81
path.AddEllipse (new RectangleF ((float)(cx0 - r0), (float)(cy0 - r0), (float)(r0 * 2), (float)(r0 * 2)));
83
var brush = new PathGradientBrush (path);
85
var orderedStops = ColorStops.OrderBy (t => t.Item1).ToArray ();
86
var colors = orderedStops.Select (i => i.Item2.ToDrawingColor ()).ToArray ();
87
var stops = orderedStops.Select (i => (float) (i.Item1)).ToArray ();
89
brush.InterpolationColors = new ColorBlend (colors.Length) {
97
internal class LinearGradient
100
public LinearGradient (PointF start, PointF end)
106
public override System.Drawing.Brush CreateBrush ()
108
if (ColorStops.Count == 0)
109
throw new ArgumentException ();
111
var stops = ColorStops.OrderBy (t => t.Item1).ToArray ();
112
var first = stops[0];
113
var last = stops[stops.Length - 1];
115
var brush = new System.Drawing.Drawing2D.LinearGradientBrush (Start, End, first.Item2.ToDrawingColor (),
116
last.Item2.ToDrawingColor ());
120
internal readonly PointF Start;
121
internal readonly PointF End;
124
internal abstract class GradientBase
126
internal readonly List<Tuple<double, Color>> ColorStops = new List<Tuple<double, Color>> ();
127
public abstract Brush CreateBrush ();
b'\\ No newline at end of file'