1 | using System.Drawing;
|
---|
2 | using System.Drawing.Drawing2D;
|
---|
3 |
|
---|
4 | namespace Netron.Diagramming.Core {
|
---|
5 | // ----------------------------------------------------------------------
|
---|
6 | /// <summary>
|
---|
7 | /// The gradient paint style
|
---|
8 | /// </summary>
|
---|
9 | // ----------------------------------------------------------------------
|
---|
10 | public partial class GradientPaintStyle :
|
---|
11 | IPaintStyle,
|
---|
12 | IVersion {
|
---|
13 | // ------------------------------------------------------------------
|
---|
14 | /// <summary>
|
---|
15 | /// Event raised when this paint style is changed.
|
---|
16 | /// </summary>
|
---|
17 | // ------------------------------------------------------------------
|
---|
18 | public event PaintStyleChangedEventHandler PaintStyleChanged;
|
---|
19 |
|
---|
20 | #region Fields
|
---|
21 |
|
---|
22 | // ------------------------------------------------------------------
|
---|
23 | /// <summary>
|
---|
24 | /// Implementation of IVersion - the current version of
|
---|
25 | /// GradientPaintStyle.
|
---|
26 | /// </summary>
|
---|
27 | // ------------------------------------------------------------------
|
---|
28 | protected const double gradientPaintStyleVersion = 1.0;
|
---|
29 |
|
---|
30 | // ------------------------------------------------------------------
|
---|
31 | /// <summary>
|
---|
32 | /// the Angle field.
|
---|
33 | /// </summary>
|
---|
34 | // ------------------------------------------------------------------
|
---|
35 | private float mAngle;
|
---|
36 |
|
---|
37 | // ------------------------------------------------------------------
|
---|
38 | /// <summary>
|
---|
39 | /// the EndColor field
|
---|
40 | /// </summary>
|
---|
41 | // ------------------------------------------------------------------
|
---|
42 | private Color mEndColor;
|
---|
43 |
|
---|
44 | // ------------------------------------------------------------------
|
---|
45 | /// <summary>
|
---|
46 | /// the StartColor field
|
---|
47 | /// </summary>
|
---|
48 | // ------------------------------------------------------------------
|
---|
49 | private Color mStartColor;
|
---|
50 |
|
---|
51 | #endregion
|
---|
52 |
|
---|
53 | #region Properties
|
---|
54 |
|
---|
55 | // ------------------------------------------------------------------
|
---|
56 | /// <summary>
|
---|
57 | /// Gets the current version.
|
---|
58 | /// </summary>
|
---|
59 | // ------------------------------------------------------------------
|
---|
60 | public virtual double Version {
|
---|
61 | get {
|
---|
62 | return gradientPaintStyleVersion;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | // ------------------------------------------------------------------
|
---|
67 | /// <summary>
|
---|
68 | /// Gets or sets the StartColor
|
---|
69 | /// </summary>
|
---|
70 | // ------------------------------------------------------------------
|
---|
71 | public Color StartColor {
|
---|
72 | get {
|
---|
73 | return mStartColor;
|
---|
74 | }
|
---|
75 | set {
|
---|
76 | mStartColor = value;
|
---|
77 | RaisePaintStyleChanged();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | // ------------------------------------------------------------------
|
---|
82 | /// <summary>
|
---|
83 | /// Gets or sets the EndColor
|
---|
84 | /// </summary>
|
---|
85 | // ------------------------------------------------------------------
|
---|
86 | public Color EndColor {
|
---|
87 | get {
|
---|
88 | return mEndColor;
|
---|
89 | }
|
---|
90 | set {
|
---|
91 | mEndColor = value;
|
---|
92 | RaisePaintStyleChanged();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | // ------------------------------------------------------------------
|
---|
97 | /// <summary>
|
---|
98 | /// Gets or sets the Angle
|
---|
99 | /// </summary>
|
---|
100 | // ------------------------------------------------------------------
|
---|
101 | public float Angle {
|
---|
102 | get {
|
---|
103 | return mAngle;
|
---|
104 | }
|
---|
105 | set {
|
---|
106 | mAngle = value;
|
---|
107 | RaisePaintStyleChanged();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | #endregion
|
---|
111 |
|
---|
112 | #region Constructor
|
---|
113 |
|
---|
114 | // ------------------------------------------------------------------
|
---|
115 | ///<summary>
|
---|
116 | ///Default constructor
|
---|
117 | ///</summary>
|
---|
118 | // ------------------------------------------------------------------
|
---|
119 | public GradientPaintStyle(
|
---|
120 | Color startColor,
|
---|
121 | Color endColor,
|
---|
122 | float angle) {
|
---|
123 | mStartColor = startColor;
|
---|
124 | mEndColor = endColor;
|
---|
125 | mAngle = angle;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // ------------------------------------------------------------------
|
---|
129 | /// <summary>
|
---|
130 | /// Initializes a new instance of the
|
---|
131 | /// <see cref="T:GradientPaintStyle"/> class.
|
---|
132 | /// </summary>
|
---|
133 | // ------------------------------------------------------------------
|
---|
134 | public GradientPaintStyle() {
|
---|
135 | mStartColor = ArtPalette.RandomLowSaturationColor;
|
---|
136 | mEndColor = Color.White;
|
---|
137 | mAngle = -135;
|
---|
138 | }
|
---|
139 | #endregion
|
---|
140 |
|
---|
141 | // ------------------------------------------------------------------
|
---|
142 | /// <summary>
|
---|
143 | /// Gets the brush.
|
---|
144 | /// </summary>
|
---|
145 | /// <param name="rectangle">The rectangle.</param>
|
---|
146 | /// <returns>Brush</returns>
|
---|
147 | // ------------------------------------------------------------------
|
---|
148 | public Brush GetBrush(Rectangle rectangle) {
|
---|
149 | if (rectangle.Equals(Rectangle.Empty)) {
|
---|
150 | return new SolidBrush(mStartColor);
|
---|
151 | } else {
|
---|
152 | if (rectangle.Width == 0) {
|
---|
153 | rectangle.Width = 1;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (rectangle.Height == 0) {
|
---|
157 | rectangle.Height = 1;
|
---|
158 | }
|
---|
159 | return new LinearGradientBrush(
|
---|
160 | rectangle,
|
---|
161 | mStartColor,
|
---|
162 | mEndColor,
|
---|
163 | mAngle,
|
---|
164 | true);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | // ------------------------------------------------------------------
|
---|
169 | /// <summary>
|
---|
170 | /// Raises the PaintStyleChanged event.
|
---|
171 | /// </summary>
|
---|
172 | // ------------------------------------------------------------------
|
---|
173 | protected virtual void RaisePaintStyleChanged() {
|
---|
174 | if (this.PaintStyleChanged != null) {
|
---|
175 | // Raise the event
|
---|
176 | this.PaintStyleChanged(
|
---|
177 | this,
|
---|
178 | new PaintStyleChangedEventArgs(
|
---|
179 | this,
|
---|
180 | FillType.LinearGradient));
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|