SvgGradientStop.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.ComponentModel;
  6. #pragma warning disable
  7. namespace Svg
  8. {
  9. /// <summary>
  10. /// Represents a colour stop in a gradient.
  11. /// </summary>
  12. [SvgElement("stop")]
  13. public class SvgGradientStop : SvgElement
  14. {
  15. private SvgUnit _offset;
  16. /// <summary>
  17. /// Gets or sets the offset, i.e. where the stop begins from the beginning, of the gradient stop.
  18. /// </summary>
  19. [SvgAttribute("offset")]
  20. public SvgUnit Offset
  21. {
  22. get { return this._offset; }
  23. set
  24. {
  25. SvgUnit unit = value;
  26. if (value.Type == SvgUnitType.Percentage)
  27. {
  28. if (value.Value > 100)
  29. {
  30. unit = new SvgUnit(value.Type, 100);
  31. }
  32. else if (value.Value < 0)
  33. {
  34. unit = new SvgUnit(value.Type, 0);
  35. }
  36. }
  37. else if (value.Type == SvgUnitType.User)
  38. {
  39. if (value.Value > 1)
  40. {
  41. unit = new SvgUnit(value.Type, 1);
  42. }
  43. else if (value.Value < 0)
  44. {
  45. unit = new SvgUnit(value.Type, 0);
  46. }
  47. }
  48. this._offset = unit.ToPercentage();
  49. }
  50. }
  51. /// <summary>
  52. /// Gets or sets the colour of the gradient stop.
  53. /// </summary>
  54. [SvgAttribute("stop-color")]
  55. [TypeConverter(typeof(SvgPaintServerFactory))]
  56. public override SvgPaintServer StopColor
  57. {
  58. get
  59. {
  60. var direct = this.Attributes.GetAttribute<SvgPaintServer>("stop-color", SvgColourServer.NotSet);
  61. if (direct == SvgColourServer.Inherit) return this.Attributes["stop-color"] as SvgPaintServer ?? SvgColourServer.NotSet;
  62. return direct;
  63. }
  64. set { this.Attributes["stop-color"] = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the opacity of the gradient stop (0-1).
  68. /// </summary>
  69. [SvgAttribute("stop-opacity")]
  70. public override float Opacity
  71. {
  72. get { return (this.Attributes["stop-opacity"] == null) ? 1.0f : (float)this.Attributes["stop-opacity"]; }
  73. set { this.Attributes["stop-opacity"] = FixOpacityValue(value); }
  74. }
  75. /// <summary>
  76. /// Initializes a new instance of the <see cref="SvgGradientStop"/> class.
  77. /// </summary>
  78. public SvgGradientStop()
  79. {
  80. this._offset = new SvgUnit(0.0f);
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="SvgGradientStop"/> class.
  84. /// </summary>
  85. /// <param name="offset">The offset.</param>
  86. /// <param name="colour">The colour.</param>
  87. public SvgGradientStop(SvgUnit offset, Color colour)
  88. {
  89. this._offset = offset;
  90. }
  91. public Color GetColor(SvgElement parent)
  92. {
  93. var core = SvgDeferredPaintServer.TryGet<SvgColourServer>(this.StopColor, parent);
  94. if (core == null) throw new InvalidOperationException("Invalid paint server for gradient stop detected.");
  95. return core.Colour;
  96. }
  97. public override SvgElement DeepCopy()
  98. {
  99. return DeepCopy<SvgGradientStop>();
  100. }
  101. public override SvgElement DeepCopy<T>()
  102. {
  103. var newObj = base.DeepCopy<T>() as SvgGradientStop;
  104. newObj.Offset = this.Offset;
  105. return newObj;
  106. }
  107. }
  108. }
  109. #pragma warning restore