using System; using System.Text; using System.Drawing; using System.ComponentModel; using System.Drawing.Drawing2D; namespace DevComponents.WinForms.Drawing { public class GradientFill : Fill { #region Constructor /// /// Initializes a new instance of the GradientFill class. /// public GradientFill() { } /// /// Initializes a new instance of the GradientFill class. /// /// /// public GradientFill(Color color1, Color color2) { _Color1 = color1; _Color2 = color2; } /// /// Initializes a new instance of the GradientFill class. /// /// /// /// public GradientFill(Color color1, Color color2, float angle) { _Color1 = color1; _Color2 = color2; _Angle = angle; } /// /// Initializes a new instance of the GradientFill class. /// /// public GradientFill(ColorStop[] interpolationColors) { _InterpolationColors.AddRange(interpolationColors); } /// /// Initializes a new instance of the GradientFill class. /// /// public GradientFill(ColorStop[] interpolationColors, int angle) { _InterpolationColors.AddRange(interpolationColors); _Angle = angle; } #endregion #region Internal Implementation /// /// Creates the brush for fill. /// /// Bounds for the brush /// Returns brush or null if brush cannot be created for given bounds or colors are not set. It is responsibility of caller to Dispose the brush. public override Brush CreateBrush(Rectangle bounds) { if (_Color1.IsEmpty && _Color2.IsEmpty && _InterpolationColors.Count == 0 || bounds.Width < 1 || bounds.Height < 1) return null; LinearGradientBrush brush=new LinearGradientBrush(bounds, _Color1, _Color2, _Angle); if (_InterpolationColors.Count == 0) return brush; brush.InterpolationColors = _InterpolationColors.GetColorBlend(); return brush; } private Color _Color1 = Color.Empty; /// /// Gets or sets the starting gradient fill color. /// [Description("Indicates the fill color.")] public Color Color1 { get { return _Color1; } set { _Color1 = value; } } /// /// Gets whether property should be serialized. /// /// true if property should be serialized [EditorBrowsable(EditorBrowsableState.Never)] public bool ShouldSerializeColor1() { return !_Color1.IsEmpty; } /// /// Sets the property to its default value. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ResetColor1() { Color1 = Color.Empty; } private Color _Color2 = Color.Empty; /// /// Gets or sets the end gradient fill color. /// [Description("Indicates the fill color.")] public Color Color2 { get { return _Color2; } set { _Color2 = value; } } /// /// Gets whether property should be serialized. /// /// true if property should be serialized [EditorBrowsable(EditorBrowsableState.Never)] public bool ShouldSerializeColor2() { return !_Color2.IsEmpty; } /// /// Sets the property to its default value. /// [EditorBrowsable(EditorBrowsableState.Never)] public void ResetColor2() { Color2 = Color.Empty; } private ColorBlendCollection _InterpolationColors = new ColorBlendCollection(); /// /// Gets the collection that defines the multicolor gradient background. /// /// /// Setting this property creates a multicolor gradient with one color at each position along the gradient line. Setting this property nullifies all previous color, position, and falloff settings for this gradient fill. /// [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Content), Description("Collection that defines the multicolor gradient background.")] public ColorBlendCollection InterpolationColors { get { return _InterpolationColors; } } private float _Angle = 90; /// /// Gets or sets the gradient fill angle. Default value is 90. /// [DefaultValue(90), Description("Indicates gradient fill angle.")] public float Angle { get { return _Angle; } set { _Angle = value; } } /// /// Creates a pen based on fill parameters. /// /// Width of the pen to create /// new instance of pen or null if pen cannot be created. public override Pen CreatePen(int width) { if (!_Color1.IsEmpty) return new Pen(_Color1, width); if (!_Color2.IsEmpty) return new Pen(_Color2, width); return null; } #endregion } }