#if OXYPLOT_COREDRAWING
namespace OxyPlot.Core.Drawing
#else
namespace OxyPlot.WindowsForms
#endif
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
///
/// Describes a GDI+ Pen.
///
public class GraphicsPenDescription
{
///
/// Initializes a new instance of the class.
///
/// The color.
/// The thickness.
/// The dash array.
/// The line join.
public GraphicsPenDescription(OxyColor color, double thickness, double[] dashArray = null, OxyPlot.LineJoin lineJoin = OxyPlot.LineJoin.Miter)
{
Color = color;
Thickness = thickness;
DashArray = dashArray;
LineJoin = lineJoin;
cachedHashCode = ComputeHashCode();
}
///
/// Gets the color of the pen.
///
/// The color.
public OxyColor Color { get; }
///
/// Gets the line thickness.
///
/// The line thickness.
public double Thickness { get; }
///
/// Gets the dash array.
///
/// The dash array.
public double[] DashArray { get; }
///
/// Gets the line join type.
///
/// The line join type.
public LineJoin LineJoin { get; }
///
/// The HashCode of the instance, as computed in the constructor.
///
private readonly int cachedHashCode;
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
/// true if the specified is equal to this instance; otherwise, false .
public override bool Equals(object obj)
{
var description = obj as GraphicsPenDescription;
return description != null &&
Color.Equals(description.Color) &&
Thickness == description.Thickness &&
DashArraysEquals(DashArray, description.DashArray) &&
LineJoin == description.LineJoin;
}
///
/// Returns a hash code for this instance.
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
public override int GetHashCode()
{
return cachedHashCode;
}
///
/// Computes the HashCode for the instance.
///
/// The HashCode for the instance.
private int ComputeHashCode()
{
var hashCode = 754997215;
unchecked
{
hashCode = hashCode * -1521134295 + Color.GetHashCode();
hashCode = hashCode * -1521134295 + Thickness.GetHashCode();
hashCode = hashCode * -1521134295 + ComputeDashArrayHashCode(DashArray);
hashCode = hashCode * -1521134295 + LineJoin.GetHashCode();
}
return hashCode;
}
///
/// Computes a HashCode for the given array based on every element in the array.
///
/// The array
/// A HashCode
private static int ComputeDashArrayHashCode(double[] array)
{
if (array == null)
{
return -1;
}
int hashCode = array.Length;
for (int i = 0; i < array.Length; i++)
{
unchecked
{
hashCode = hashCode * 31 + array[i].GetHashCode();
}
}
return hashCode;
}
///
/// Determines whether two arrays are equivalent.
///
/// The left array.
/// The right array.
/// true if the arrays are the same array, are both null, or have the same elements; otherwise false
private static bool DashArraysEquals(double[] l, double[] r)
{
if (l == r)
{
return true;
}
if (l == null || r == null)
{
return false;
}
if (l.Length != r.Length)
{
return false;
}
for (int i = 0; i < l.Length; i++)
{
if (l[i] != r[i])
{
return false;
}
}
return true;
}
}
}