using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.ComponentModel;
namespace Svg
{
///
/// Represents a colour stop in a gradient.
///
[SvgElement("stop")]
public class SvgGradientStop : SvgElement
{
private SvgUnit _offset;
///
/// Gets or sets the offset, i.e. where the stop begins from the beginning, of the gradient stop.
///
[SvgAttribute("offset")]
public SvgUnit Offset
{
get { return this._offset; }
set
{
SvgUnit unit = value;
if (value.Type == SvgUnitType.Percentage)
{
if (value.Value > 100)
{
unit = new SvgUnit(value.Type, 100);
}
else if (value.Value < 0)
{
unit = new SvgUnit(value.Type, 0);
}
}
else if (value.Type == SvgUnitType.User)
{
if (value.Value > 1)
{
unit = new SvgUnit(value.Type, 1);
}
else if (value.Value < 0)
{
unit = new SvgUnit(value.Type, 0);
}
}
this._offset = unit.ToPercentage();
}
}
///
/// Gets or sets the colour of the gradient stop.
///
[SvgAttribute("stop-color")]
[TypeConverter(typeof(SvgPaintServerFactory))]
public SvgPaintServer StopColor
{
get
{
var direct = this.Attributes.GetAttribute("stop-color", SvgColourServer.NotSet);
if (direct == SvgColourServer.Inherit) return this.Attributes["stop-color"] as SvgPaintServer ?? SvgColourServer.NotSet;
return direct;
}
set { this.Attributes["stop-color"] = value; }
}
///
/// Gets or sets the opacity of the gradient stop (0-1).
///
[SvgAttribute("stop-opacity")]
public string Opacity
{
get { return this.Attributes["stop-opacity"] as string; }
set { this.Attributes["stop-opacity"] = value; }
}
public float GetOpacity()
{
var opacity = this.Opacity;
return string.IsNullOrEmpty(opacity) ? 1.0f : float.Parse(opacity);
}
///
/// Initializes a new instance of the class.
///
public SvgGradientStop()
{
this._offset = new SvgUnit(0.0f);
}
///
/// Initializes a new instance of the class.
///
/// The offset.
/// The colour.
public SvgGradientStop(SvgUnit offset, Color colour)
{
this._offset = offset;
}
public Color GetColor(SvgElement parent)
{
var core = SvgDeferredPaintServer.TryGet(this.StopColor, parent);
if (core == null) throw new InvalidOperationException("Invalid paint server for gradient stop detected.");
return core.Colour;
}
public override SvgElement DeepCopy()
{
return DeepCopy();
}
public override SvgElement DeepCopy()
{
var newObj = base.DeepCopy() as SvgGradientStop;
newObj.Offset = this.Offset;
return newObj;
}
}
}