SvgColourServer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. #pragma warning disable
  6. namespace Svg
  7. {
  8. public sealed class SvgColourServer : SvgPaintServer
  9. {
  10. /// <summary>
  11. /// An unspecified <see cref="SvgPaintServer"/>.
  12. /// </summary>
  13. public static readonly SvgPaintServer NotSet = new SvgColourServer(System.Drawing.Color.Black);
  14. /// <summary>
  15. /// A <see cref="SvgPaintServer"/> that should inherit from its parent.
  16. /// </summary>
  17. public static readonly SvgPaintServer Inherit = new SvgColourServer(System.Drawing.Color.Black);
  18. public SvgColourServer()
  19. : this(System.Drawing.Color.Black)
  20. {
  21. }
  22. public SvgColourServer(Color colour)
  23. {
  24. this._colour = colour;
  25. }
  26. private Color _colour;
  27. public Color Colour
  28. {
  29. get { return this._colour; }
  30. set { this._colour = value; }
  31. }
  32. public override Brush GetBrush(SvgVisualElement styleOwner, ISvgRenderer renderer, float opacity, bool forStroke = false)
  33. {
  34. //is none?
  35. if (this == SvgPaintServer.None) return new SolidBrush(System.Drawing.Color.Transparent);
  36. int alpha = (int)Math.Round((opacity * (this.Colour.A/255.0) ) * 255);
  37. Color colour = System.Drawing.Color.FromArgb(alpha, this.Colour);
  38. return new SolidBrush(colour);
  39. }
  40. public override string ToString()
  41. {
  42. if(this == SvgPaintServer.None)
  43. return "none";
  44. else if(this == SvgColourServer.NotSet)
  45. return "";
  46. Color c = this.Colour;
  47. if (ColorExt.IsKnownColor(c))
  48. return c.Name;
  49. // Return the hex value
  50. return String.Format("#{0}", c.ToArgb().ToString("x").Substring(2));
  51. }
  52. public override SvgElement DeepCopy()
  53. {
  54. return DeepCopy<SvgColourServer>();
  55. }
  56. public override SvgElement DeepCopy<T>()
  57. {
  58. var newObj = base.DeepCopy<T>() as SvgColourServer;
  59. newObj.Colour = this.Colour;
  60. return newObj;
  61. }
  62. public override bool Equals(object obj)
  63. {
  64. var objColor = obj as SvgColourServer;
  65. if (objColor == null)
  66. return false;
  67. if ((this == SvgPaintServer.None && obj != SvgPaintServer.None) ||
  68. (this != SvgPaintServer.None && obj == SvgPaintServer.None) ||
  69. (this == SvgColourServer.NotSet && obj != SvgColourServer.NotSet) ||
  70. (this != SvgColourServer.NotSet && obj == SvgColourServer.NotSet) ||
  71. (this == SvgColourServer.Inherit && obj != SvgColourServer.Inherit) ||
  72. (this != SvgColourServer.Inherit && obj == SvgColourServer.Inherit)) return false;
  73. return this.GetHashCode() == objColor.GetHashCode();
  74. }
  75. public override int GetHashCode()
  76. {
  77. return _colour.GetHashCode();
  78. }
  79. }
  80. }
  81. #pragma warning restore