LinearColorAxis.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="LinearColorAxis.cs" company="OxyPlot">
  3. // Copyright (c) 2014 OxyPlot contributors
  4. // </copyright>
  5. // <summary>
  6. // Provides a Avalonia wrapper for the <see cref="OxyPlot.Axes.LinearColorAxis" />.
  7. // </summary>
  8. // --------------------------------------------------------------------------------------------------------------------
  9. using System.Diagnostics.Contracts;
  10. using Avalonia;
  11. namespace OxyPlot.Avalonia
  12. {
  13. using global::Avalonia.Media;
  14. using global::Avalonia.Metadata;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Linq;
  19. /// <summary>
  20. /// Provides a Avalonia wrapper for the <see cref="Axes.LinearColorAxis" />.
  21. /// </summary>
  22. public class LinearColorAxis : Axis
  23. {
  24. /// <summary>
  25. /// Identifies the <see cref="GradientStopsProperty"/> dependency property.
  26. /// </summary>
  27. public static readonly StyledProperty<IList<GradientStop>> GradientStopsProperty = AvaloniaProperty.Register<LinearColorAxis, IList<GradientStop>>(nameof(GradientStops));
  28. /// <summary>
  29. /// Identifies the <see cref="HighColorProperty"/> dependency property.
  30. /// </summary>
  31. public static readonly StyledProperty<Color> HighColorProperty = AvaloniaProperty.Register<LinearColorAxis, Color>(nameof(HighColor), Colors.White);
  32. /// <summary>
  33. /// Identifies the <see cref="LowColorProperty"/> dependency property.
  34. /// </summary>
  35. public static readonly StyledProperty<Color> LowColorProperty = AvaloniaProperty.Register<LinearColorAxis, Color>(nameof(LowColor), Colors.Black);
  36. /// <summary>
  37. /// Identifies the <see cref="PaletteSizeProperty"/> dependency property.
  38. /// </summary>
  39. public static readonly StyledProperty<int> PaletteSizeProperty = AvaloniaProperty.Register<LinearColorAxis, int>(nameof(PaletteSize), 20, validate: ValidatePaletteSize);
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="LinearColorAxis"/> class.
  42. /// </summary>
  43. public LinearColorAxis()
  44. {
  45. InternalAxis = new Axes.LinearColorAxis();
  46. GradientStops = new List<GradientStop>();
  47. }
  48. /// <summary>
  49. /// Gets or sets the palette size.
  50. /// </summary>
  51. public int PaletteSize
  52. {
  53. get
  54. {
  55. return GetValue(PaletteSizeProperty);
  56. }
  57. set
  58. {
  59. SetValue(PaletteSizeProperty, value);
  60. }
  61. }
  62. /// <summary>
  63. /// Gets or sets the high color.
  64. /// </summary>
  65. public Color HighColor
  66. {
  67. get
  68. {
  69. return GetValue(HighColorProperty);
  70. }
  71. set
  72. {
  73. SetValue(HighColorProperty, value);
  74. }
  75. }
  76. /// <summary>
  77. /// Gets or sets the low color.
  78. /// </summary>
  79. public Color LowColor
  80. {
  81. get
  82. {
  83. return GetValue(LowColorProperty);
  84. }
  85. set
  86. {
  87. SetValue(LowColorProperty, value);
  88. }
  89. }
  90. /// <summary>
  91. /// Gets or sets the gradient stops.
  92. /// </summary>
  93. [Content]
  94. public IList<GradientStop> GradientStops
  95. {
  96. get
  97. {
  98. return GetValue(GradientStopsProperty);
  99. }
  100. set
  101. {
  102. SetValue(GradientStopsProperty, value);
  103. }
  104. }
  105. /// <summary>
  106. /// Creates the model.
  107. /// </summary>
  108. /// <returns>
  109. /// An axis object.
  110. /// </returns>
  111. public override Axes.Axis CreateModel()
  112. {
  113. SynchronizeProperties();
  114. return InternalAxis;
  115. }
  116. /// <summary>
  117. /// Synchronizes the properties.
  118. /// </summary>
  119. protected override void SynchronizeProperties()
  120. {
  121. base.SynchronizeProperties();
  122. var axis = InternalAxis as Axes.LinearColorAxis;
  123. Contract.Requires<InvalidOperationException>(axis != null);
  124. if (GradientStops != null)
  125. {
  126. axis.Palette = GradientStops.Count > 2
  127. ? Interpolate(GradientStops.ToList(), PaletteSize)
  128. : new OxyPalette();
  129. }
  130. axis.HighColor = HighColor.ToOxyColor();
  131. axis.LowColor = LowColor.ToOxyColor();
  132. axis.Minimum = Minimum;
  133. axis.Maximum = Maximum;
  134. }
  135. /// <summary>
  136. /// Translates a collection of <see cref="GradientStop" /> to an <see cref="OxyPalette" />.
  137. /// </summary>
  138. /// <param name="stops">
  139. /// The gradient stops collection to convert.
  140. /// </param>
  141. /// <param name="paletteSize">
  142. /// The palette size.
  143. /// </param>
  144. /// <returns>
  145. /// The interpolated <see cref="OxyPalette"/>.
  146. /// </returns>
  147. private static OxyPalette Interpolate(List<GradientStop> stops, int paletteSize)
  148. {
  149. Debug.Assert(stops.Count >= 2, "Can't interpolate less than 2 gradient stops.");
  150. Debug.Assert(paletteSize > 0, "Palette size must be non-zero positive number.");
  151. var palette = new List<OxyColor>();
  152. stops.Sort((x1, x2) => x1.Offset.CompareTo(x2.Offset));
  153. var palettePositions = stops[0].Offset;
  154. var step = (double)stops.Count / paletteSize;
  155. for (int i = 0; i < stops.Count - 1; i++)
  156. {
  157. var start = stops[i];
  158. var end = stops[i + 1];
  159. while (palettePositions <= end.Offset)
  160. {
  161. palette.Add(
  162. OxyColor.Interpolate(
  163. start.Color.ToOxyColor(),
  164. end.Color.ToOxyColor(),
  165. (palettePositions - start.Offset) / (end.Offset - start.Offset)));
  166. palettePositions += step;
  167. }
  168. }
  169. return new OxyPalette(palette);
  170. }
  171. /// <summary>
  172. /// Validates the palette size.
  173. /// </summary>
  174. /// <param name="value">
  175. /// The property value.
  176. /// </param>
  177. /// <returns>
  178. /// The validation result.
  179. /// </returns>
  180. private static bool ValidatePaletteSize(int value)
  181. {
  182. return value >= 1;
  183. }
  184. static LinearColorAxis()
  185. {
  186. GradientStopsProperty.Changed.AddClassHandler<LinearColorAxis>(AppearanceChanged);
  187. HighColorProperty.Changed.AddClassHandler<LinearColorAxis>(AppearanceChanged);
  188. LowColorProperty.Changed.AddClassHandler<LinearColorAxis>(AppearanceChanged);
  189. PaletteSizeProperty.Changed.AddClassHandler<LinearColorAxis>(AppearanceChanged);
  190. }
  191. }
  192. }