#if WPF
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Xml;
using System.Globalization;
using System.Linq;
#else
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Markup;
using System.Globalization;
#endif
using System.ComponentModel;
using Visifire.Charts;
using Visifire.Commons;
namespace Visifire.Charts
{
///
/// Visifire.Charts.Faces class
///
public class Faces
{
///
/// Initializes a new instance of Visifire.Charts.Faces class
///
public Faces()
{
VisualComponents = new List();
BorderElements = new List();
}
///
/// Contains references to individual components of the border elements in the visual
///
public List BorderElements
{
get;
set;
}
///
/// Contains references to individual components of the elements in the visual
///
public List VisualComponents
{
get;
set;
}
///
/// Different parts of Visuals. Parts are used while doing partial update
///
public List Parts
{
get;
set;
}
///
/// Visual of faces
///
public FrameworkElement Visual
{
get;
set;
}
///
/// Label canvas reference for faces
///
public Canvas LabelCanvas
{
get;
set;
}
}
///
/// Visifire.Charts.ExtendedGraphics class
///
internal class ExtendedGraphics
{
///
/// Initializes a new instance of Visifire.Charts.ExtendedGraphics class
///
public ExtendedGraphics()
{
}
#region Static Methods
///
/// Returns dash array for border
///
/// BorderStyle as BorderStyles
/// DashArray as DoubleCollection
internal static DoubleCollection GetDashArray(BorderStyles borderStyle)
{
return Graphics.LineStyleToStrokeDashArray(borderStyle.ToString());
}
///
/// Returns dash array for line
///
/// LineStyle as LineStyles
/// DashArray as DoubleCollection
internal static DoubleCollection GetDashArray(LineStyles lineStyle)
{
return Graphics.LineStyleToStrokeDashArray(lineStyle.ToString());
}
///
/// Generates a rectangle. The shape of each of the corners can be controlled and is useful for creating single sided
/// curved rectangles.
///
private static PathGeometry GetRectanglePathGeometry(Double width, Double height, CornerRadius xRadius, CornerRadius yRadius)
{
// Create a path geometry object
PathGeometry pathGeometry = new PathGeometry();
pathGeometry.Figures = new PathFigureCollection();
PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(xRadius.TopLeft, 0);
pathFigure.Segments = new PathSegmentCollection();
// Do not change the order of the lines below
// Segmens required to create the rectangle
pathFigure.Segments.Add(Graphics.GetLineSegment(new Point(width - xRadius.TopRight, 0)));
pathFigure.Segments.Add(Graphics.GetArcSegment(new Point(width, yRadius.TopRight), new Size(xRadius.TopRight, yRadius.TopRight), 0, SweepDirection.Clockwise));
pathFigure.Segments.Add(Graphics.GetLineSegment(new Point(width, height - yRadius.BottomRight)));
pathFigure.Segments.Add(Graphics.GetArcSegment(new Point(width - xRadius.BottomRight, height), new Size(xRadius.BottomRight, yRadius.BottomRight), 0, SweepDirection.Clockwise));
pathFigure.Segments.Add(Graphics.GetLineSegment(new Point(xRadius.BottomLeft, height)));
pathFigure.Segments.Add(Graphics.GetArcSegment(new Point(0, height - yRadius.BottomLeft), new Size(xRadius.BottomLeft, yRadius.BottomLeft), 0, SweepDirection.Clockwise));
pathFigure.Segments.Add(Graphics.GetLineSegment(new Point(0, yRadius.TopLeft)));
pathFigure.Segments.Add(Graphics.GetArcSegment(new Point(xRadius.TopLeft, 0), new Size(xRadius.TopLeft, yRadius.TopLeft), 0, SweepDirection.Clockwise));
pathGeometry.Figures.Add(pathFigure);
return pathGeometry;
}
///
/// Get the corrected corner radius
///
/// Radius as CornerRadius
/// Limit as Double
/// CornerRadius
private static CornerRadius GetCorrectedRadius(CornerRadius radius,Double limit)
{
return new CornerRadius(
((radius.TopLeft > limit) ? limit : radius.TopLeft),
((radius.TopRight > limit) ? limit : radius.TopRight),
((radius.BottomRight > limit) ? limit : radius.BottomRight),
((radius.BottomLeft > limit) ? limit : radius.BottomLeft)
);
}
///
/// Get corner shadow gradient brush for a rectangle
///
/// Corner as Corners
/// Brush
private static Brush GetCornerShadowGradientBrush(Corners corner)
{
RadialGradientBrush gradBrush = new RadialGradientBrush();
gradBrush.GradientStops = new GradientStopCollection();
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb(191, 0, 0, 0), 0));
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb(0, 0, 0, 0), 1));
TransformGroup tg = new TransformGroup();
ScaleTransform st = new ScaleTransform() { ScaleX = 2, ScaleY = 2, CenterX = 0.5, CenterY = 0.5 };
TranslateTransform tt = null;
switch (corner)
{
case Corners.TopLeft:
tt = new TranslateTransform() { X = 0.5, Y = 0.5 };
break;
case Corners.TopRight:
tt = new TranslateTransform() { X = -0.5, Y = 0.5 };
break;
case Corners.BottomLeft:
tt = new TranslateTransform() { X = 0.5, Y = -0.5 };
break;
case Corners.BottomRight:
tt = new TranslateTransform() { X = -0.5, Y = -0.5 };
break;
}
tg.Children.Add(st);
tg.Children.Add(tt);
gradBrush.RelativeTransform = tg;
return gradBrush;
}
///
/// Get side shadow gradient brush for a rectangle
///
/// Direction as Directions
/// Brush
private static Brush GetSideShadowGradientBrush(Directions direction)
{
LinearGradientBrush gradBrush = new LinearGradientBrush();
gradBrush.GradientStops = new GradientStopCollection();
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb(191, 0, 0, 0), 0));
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb(0, 0, 0, 0), 1));
switch (direction)
{
case Directions.Top:
gradBrush.StartPoint = new Point(0.5, 1);
gradBrush.EndPoint = new Point(0.5, 0);
break;
case Directions.Right:
gradBrush.StartPoint = new Point(0, 0.5);
gradBrush.EndPoint = new Point(1, 0.5);
break;
case Directions.Left:
gradBrush.StartPoint = new Point(1, 0.5);
gradBrush.EndPoint = new Point(0, 0.5);
break;
case Directions.Bottom:
gradBrush.StartPoint = new Point(0.5, 0);
gradBrush.EndPoint = new Point(0.5, 1);
break;
}
return gradBrush;
}
///
/// Clone a DoubleCollection
///
/// Collection as DoubleCollection
/// DoubleCollection
public static DoubleCollection CloneCollection(DoubleCollection collection)
{
DoubleCollection newCollection = new DoubleCollection();
foreach (Double value in collection)
newCollection.Add(value);
return newCollection;
}
///
/// Creates and returns a rectangle based on the given params
///
/// Visual width
/// Visual height
/// StrokeThickness
/// StrokeDashArray
/// Stroke
/// Fill color
/// XRadius as CornerRadius
/// YRadius as CornerRadius
/// Canvas
public static Canvas Get2DRectangle(FrameworkElement tagReference, Double width, Double height, Double strokeThickness, DoubleCollection strokeDashArray, Brush stroke, Brush fill, CornerRadius xRadius, CornerRadius yRadius)
{
Canvas canvas = new Canvas();
Path rectangle = new Path() { Tag = new ElementData() { Element = tagReference } };
canvas.Width = width;
canvas.Height = height;
rectangle.StrokeThickness = strokeThickness;
rectangle.StrokeDashArray = strokeDashArray != null ? CloneCollection(strokeDashArray) : strokeDashArray;
rectangle.StrokeDashCap = PenLineCap.Flat;
rectangle.StrokeEndLineCap = PenLineCap.Flat;
rectangle.StrokeMiterLimit = 1;
rectangle.StrokeStartLineCap = PenLineCap.Flat;
rectangle.StrokeLineJoin = PenLineJoin.Bevel;
if(strokeThickness > 0)
rectangle.Stroke = stroke;
rectangle.Fill = fill;
rectangle.Data = GetRectanglePathGeometry(
width,
height,
GetCorrectedRadius(xRadius, width),
GetCorrectedRadius(yRadius, height)
);
rectangle.SetValue(Canvas.TopProperty, (Double)0);
rectangle.SetValue(Canvas.LeftProperty, (Double)0);
canvas.Children.Add(rectangle);
return canvas;
}
///
/// Creates and returns a rectangle bevel layer based on the given params
///
/// Visual width
/// Visual height
/// BevelX as Double
/// BevelY as Double
/// TopBrush
/// LeftBrush
/// RightBrush
/// BottomBrush
/// Canvas
public static Canvas Get2DRectangleBevel(FrameworkElement tagReference, Double width, Double height, Double bevelX,Double bevelY, Brush topBrush, Brush leftBrush, Brush rightBrush, Brush bottomBrush)
{
Canvas canvas = new Canvas();
canvas.Width = width;
canvas.Height = height;
Polygon topBevel = new Polygon() { Tag = new ElementData() { Element = tagReference, VisualElementName = "TopBevel" } };
topBevel.Points = new PointCollection();
topBevel.Points.Add(new Point(0, 0));
topBevel.Points.Add(new Point(width, 0));
topBevel.Points.Add(new Point(width - bevelX , bevelY));
topBevel.Points.Add(new Point(bevelX, bevelY));
topBevel.Fill = topBrush;
canvas.Children.Add(topBevel);
Polygon leftBevel = new Polygon() { Tag = new ElementData() { Element = tagReference, VisualElementName = "LeftBevel" } };
leftBevel.Points = new PointCollection();
leftBevel.Points.Add(new Point(0, 0));
leftBevel.Points.Add(new Point(bevelX, bevelY));
leftBevel.Points.Add(new Point(bevelX, height - bevelY));
leftBevel.Points.Add(new Point(0, height));
leftBevel.Fill = leftBrush;
canvas.Children.Add(leftBevel);
Polygon rightBevel = new Polygon() { Tag = new ElementData() { Element = tagReference, VisualElementName="RightBevel" } };
rightBevel.Points = new PointCollection();
rightBevel.Points.Add(new Point(width, 0));
rightBevel.Points.Add(new Point(width, height));
rightBevel.Points.Add(new Point(width - bevelX, height - bevelY));
rightBevel.Points.Add(new Point(width - bevelX, bevelY));
rightBevel.Fill = rightBrush;
canvas.Children.Add(rightBevel);
Polygon bottomBevel = new Polygon() { Tag = new ElementData() { Element = tagReference, VisualElementName = "BottomBevel" } };
bottomBevel.Points = new PointCollection();
bottomBevel.Points.Add(new Point(0, height));
bottomBevel.Points.Add(new Point(bevelX,height - bevelY));
bottomBevel.Points.Add(new Point(width - bevelX, height - bevelY));
bottomBevel.Points.Add(new Point(width, height));
bottomBevel.Fill = bottomBrush;
canvas.Children.Add(bottomBevel);
return canvas;
}
///
/// Creates and returns a rectangle gradient layer based on the given params
///
/// Visual width
/// visual height
/// Brush1
/// Brush2
/// Orientation
/// Canvas
public static Canvas Get2DRectangleGradiance(FrameworkElement tagReference, Double width, Double height, Brush brush1, Brush brush2, Orientation orientation)
{
Canvas canvas = new Canvas();
canvas.Width = width;
canvas.Height = height;
if (orientation == Orientation.Vertical)
{
Rectangle rectLeft = new Rectangle() { Tag = new ElementData() { Element = tagReference, VisualElementName = "GradianceLeft" } };
rectLeft.Width = width / 2 ;
rectLeft.Height = height;
rectLeft.SetValue(Canvas.TopProperty, (Double)0);
rectLeft.SetValue(Canvas.LeftProperty, (Double)0);
rectLeft.Fill = brush1;
canvas.Children.Add(rectLeft);
Rectangle rectRight = new Rectangle() { Tag = new ElementData() { Element = tagReference, VisualElementName="GradianceRight" } };
rectRight.Width = width / 2;
rectRight.Height = height;
rectRight.SetValue(Canvas.TopProperty, (Double)0);
rectRight.SetValue(Canvas.LeftProperty, (Double)width / 2);
rectRight.Fill = brush2;
canvas.Children.Add(rectRight);
}
else
{
Rectangle rectTop = new Rectangle() { Tag = new ElementData() { Element = tagReference, VisualElementName = "GradianceTop" } };
rectTop.Width = width;
rectTop.Height = height / 2;
rectTop.SetValue(Canvas.TopProperty, (Double)0);
rectTop.SetValue(Canvas.LeftProperty, (Double)0);
rectTop.Fill = brush1;
canvas.Children.Add(rectTop);
Rectangle rectBottom = new Rectangle() { Tag = new ElementData() { Element = tagReference, VisualElementName = "GradianceBottom" } };
rectBottom.Width = width;
rectBottom.Height = height / 2;
rectBottom.SetValue(Canvas.TopProperty, (Double)height / 2);
rectBottom.SetValue(Canvas.LeftProperty, (Double)0);
rectBottom.Fill = brush2;
canvas.Children.Add(rectBottom);
}
return canvas;
}
///
/// Creates Path From Points
///
///
///
///
public static Path GetPathFromPoints(Brush fillColor, params Point[] points)
{
Path path = new Path();
PathGeometry pathGeo = new PathGeometry();
PathFigure pathFigure = new PathFigure();
LineSegment lineSegment;
pathFigure.StartPoint = points[0];
foreach (Point point in points)
{
lineSegment = new LineSegment() { Point = point };
pathFigure.Segments.Add(lineSegment);
}
lineSegment = new LineSegment() { Point = points[0] };
pathFigure.Segments.Add(lineSegment);
pathGeo.Figures.Add(pathFigure);
path.Data = pathGeo;
path.Fill = fillColor;
return path;
}
///
/// Creates and returns a rectangle shadow based on the given params
///
/// Visual width
/// Visual height
/// XRadius as CornerRadius
/// YRadius as CornerRadius
/// MinCurvature as Double
/// Grid
public static Grid Get2DRectangleShadow(FrameworkElement tagReference, Double width, Double height, CornerRadius xRadius, CornerRadius yRadius, Double minCurvature)
{
CornerRadius tempXRadius = new CornerRadius(Math.Max(xRadius.TopLeft, minCurvature), Math.Max(xRadius.TopRight, minCurvature), Math.Max(xRadius.BottomRight, minCurvature), Math.Max(xRadius.BottomLeft, minCurvature));
CornerRadius tempYRadius = new CornerRadius(Math.Max(yRadius.TopLeft, minCurvature), Math.Max(yRadius.TopRight, minCurvature), Math.Max(yRadius.BottomRight, minCurvature), Math.Max(yRadius.BottomLeft, minCurvature));
CornerRadius radiusX = GetCorrectedRadius(tempXRadius, width/2);
CornerRadius radiusY = GetCorrectedRadius(tempYRadius, height/2);
Grid visual = new Grid();
visual.Height = height;
visual.Width = width;
for (Int32 index = 0; index < 3; index++)
{
visual.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(0, GridUnitType.Auto) });
visual.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0, GridUnitType.Auto) });
}
Rectangle topLeft = new Rectangle() { Width = radiusX.TopLeft, Height = radiusY.TopLeft,Fill = GetCornerShadowGradientBrush(Corners.TopLeft), Tag = (tagReference != null) ? new ElementData() { Element = tagReference }: null};
Rectangle topRight = new Rectangle() { Width = radiusX.TopRight, Height = radiusY.TopRight, Fill = GetCornerShadowGradientBrush(Corners.TopRight), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle bottomLeft = new Rectangle() { Width = radiusX.BottomLeft, Height = radiusY.BottomLeft, Fill = GetCornerShadowGradientBrush(Corners.BottomLeft), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle bottomRight = new Rectangle() { Width = radiusX.BottomRight, Height = radiusY.BottomRight, Fill = GetCornerShadowGradientBrush(Corners.BottomRight), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle center = new Rectangle() { Width = width - radiusX.TopLeft - radiusX.TopRight, Height = height - radiusY.TopLeft - radiusY.BottomLeft, Fill = new SolidColorBrush(Color.FromArgb((Byte)191, (Byte)0, (Byte)0, (Byte)0)), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle top = new Rectangle() { Width = width - radiusX.TopLeft - radiusX.TopRight, Height = Math.Max(radiusY.TopLeft, radiusY.TopRight), Fill = GetSideShadowGradientBrush(Directions.Top), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle right = new Rectangle() { Width = Math.Max(radiusX.TopRight, radiusX.BottomRight), Height = height - radiusY.TopRight - radiusY.BottomRight, Fill = GetSideShadowGradientBrush(Directions.Right), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle left = new Rectangle() { Width = Math.Max(radiusX.TopLeft, radiusX.BottomLeft), Height = height - radiusY.TopLeft - radiusY.BottomLeft, Fill = GetSideShadowGradientBrush(Directions.Left), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
Rectangle bottom = new Rectangle() { Width = width - radiusX.BottomLeft - radiusX.BottomRight, Height = Math.Max(radiusY.BottomLeft, radiusY.BottomRight), Fill = GetSideShadowGradientBrush(Directions.Bottom), Tag = (tagReference != null) ? new ElementData() { Element = tagReference } : null };
topLeft.SetValue(Grid.RowProperty, (Int32)0); topLeft.SetValue(Grid.ColumnProperty, (Int32)0);
top.SetValue(Grid.RowProperty, (Int32)0); top.SetValue(Grid.ColumnProperty, (Int32)1);
topRight.SetValue(Grid.RowProperty, (Int32)0); topRight.SetValue(Grid.ColumnProperty, (Int32)2);
left.SetValue(Grid.RowProperty, (Int32)1); topLeft.SetValue(Grid.ColumnProperty, (Int32)0);
center.SetValue(Grid.RowProperty, (Int32)1); center.SetValue(Grid.ColumnProperty, (Int32)1);
right.SetValue(Grid.RowProperty, (Int32)1); right.SetValue(Grid.ColumnProperty, (Int32)2);
bottomLeft.SetValue(Grid.RowProperty, (Int32)2); bottomLeft.SetValue(Grid.ColumnProperty, (Int32)0);
bottom.SetValue(Grid.RowProperty, (Int32)2); bottom.SetValue(Grid.ColumnProperty, (Int32)1);
bottomRight.SetValue(Grid.RowProperty, (Int32)2); bottomRight.SetValue(Grid.ColumnProperty, (Int32)2);
visual.Children.Add(topLeft);
visual.Children.Add(top);
visual.Children.Add(topRight);
visual.Children.Add(left);
visual.Children.Add(center);
visual.Children.Add(right);
visual.Children.Add(bottomLeft);
visual.Children.Add(bottom);
visual.Children.Add(bottomRight);
return visual;
}
private enum Corners { TopLeft, TopRight, BottomLeft, BottomRight };
private enum Directions { Top, Left, Right, Bottom };
#endregion
}
}
namespace Visifire.Commons
{
///
/// Visifire.Commons.Graphics class
///
public class Graphics
{
///
/// Initializes a new instance of Visifire.Commons.Graphics class
///
public Graphics()
{
}
#region Static Methods
internal static Random RAND = new Random(DateTime.Now.Millisecond);
public static Brush GetRandonColor()
{
return new SolidColorBrush(Color.FromArgb((byte)255, (byte)RAND.Next(255), (byte)RAND.Next(255), (byte)RAND.Next(255)));
}
internal static Point MidPointOfALine(Point point1, Point point2)
{
return new Point((point1.X + point2.X) / 2, (point1.Y + point2.Y) / 2);
}
internal static Double DistanceBetweenTwoPoints(Point point1, Point point2)
{
return (Math.Sqrt(Math.Pow((point1.X - point2.X), 2) + Math.Pow((point1.Y - point2.Y), 2)));
}
internal static Point IntersectingPointOfTwoLines(Point p1, Point p2, Point p3, Point p4)
{
Double ua = ((p4.X - p3.X) * (p1.Y - p3.Y) - (p4.Y - p3.Y) * (p1.X - p3.X));
ua /= ((p4.Y - p3.Y) * (p2.X - p1.X) - (p4.X - p3.X) * (p2.Y - p1.Y));
Double ub = ((p2.X - p1.X) * (p1.Y - p3.Y) - (p2.Y - p1.Y) * (p1.X - p3.X));
ub /= ((p4.Y - p3.Y) * (p2.X - p1.X) - (p4.X - p3.X) * (p2.Y - p1.Y));
Double x = p1.X + ua * (p2.X - p1.X);
Double y = p1.X + ub * (p2.Y - p1.Y);
return new Point(x, y);
}
internal static void DrawPointAt(Point point, Canvas visual, Color fillColor)
{
Ellipse e = new Ellipse() { Height = 4, Width = 4, Fill = new SolidColorBrush(fillColor), Stroke = new SolidColorBrush(Colors.Red), StrokeThickness = .25 };
e.SetValue(Canvas.LeftProperty, point.X - e.Height / 2);
e.SetValue(Canvas.TopProperty, point.Y - e.Width / 2);
e.SetValue(Canvas.ZIndexProperty, 10001);
visual.Children.Add(e);
}
internal static Point SecondPointOfIntersectionOfCord(Point center, Double xRadius, Double yRadius, Point linePointOutSidePie)
{
Double x = Math.Sqrt(xRadius * xRadius - Math.Pow((linePointOutSidePie.Y + center.Y), 2)) - center.X;
Double y = Math.Sqrt(yRadius * yRadius - Math.Pow((linePointOutSidePie.X + center.X), 2)) - center.Y;
return new Point(x,y);
}
internal static Boolean IsPointInside(Point center, Double radius, Point point)
{
Double distance = Graphics.DistanceBetweenTwoPoints(center, point);
if (!Double.IsNaN(distance))
{
if (distance <= radius)
return true;
else
return false;
}
else
return false;
}
///
/// Calculates visual size
///
/// Visual as FrameworkElement
/// Visual size
internal static Size CalculateVisualSize(FrameworkElement visual)
{
Size retVal = new Size(0,0);
if (visual != null)
{
visual.Measure(new Size(Double.MaxValue, Double.MaxValue));
retVal = visual.DesiredSize;
}
return retVal;
}
///
/// Calculates textblock size
///
/// RadianAngle as Double
/// TextBlock
/// TextBlock size
internal static Size CalculateTextBlockSize(Double radianAngle, TextBlock textBlock)
{
Double actualHeight;
Double actualWidth;
#if WPF
textBlock.Measure(new Size(Double.MaxValue, Double.MaxValue));
actualHeight = textBlock.DesiredSize.Height;
actualWidth = textBlock.DesiredSize.Width;
#else
actualHeight = textBlock.ActualHeight;
actualWidth = textBlock.ActualWidth;
#endif
if (radianAngle != 0)
{
// length of the diagonal from top left to bottom right
Double length = Math.Sqrt(Math.Pow(actualHeight, 2) + Math.Pow(actualWidth, 2));
// angle made by the diagonal with respect to the horizontal
Double beta = Math.Atan(actualHeight / actualWidth);
// calculate the two possible height and width values using the diagonal length and angle
Double height1 = length * Math.Sin(radianAngle + beta);
Double height2 = length * Math.Sin(radianAngle - beta);
Double width1 = length * Math.Cos(radianAngle + beta);
Double width2 = length * Math.Cos(radianAngle - beta);
// Actual height will be the maximum of the two calculated heights
actualHeight = Math.Max(Math.Abs(height1), Math.Abs(height2));
// Actual width will be the maximum of the two calculated widths
actualWidth = Math.Max(Math.Abs(width1), Math.Abs(width2));
}
return new Size(actualWidth, actualHeight);
}
///
/// Generate double collection
///
/// Array of double values
/// DoubleCollection
internal static DoubleCollection GenerateDoubleCollection(params Double[] values)
{
DoubleCollection collection = new DoubleCollection();
foreach (Double value in values)
collection.Add(value);
return collection;
}
///
/// Creates and returns a right gradient brush
///
/// Alpha as Int32
/// Brush
internal static Brush GetRightGradianceBrush(Int32 alpha)
{
LinearGradientBrush gradBrush = new LinearGradientBrush();
gradBrush.GradientStops = new GradientStopCollection();
gradBrush.StartPoint = new Point(1, 1);
gradBrush.EndPoint = new Point(0, 0);
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb((byte)alpha, 0, 0, 0), 0));
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb(0, 0, 0, 0), 0));
return gradBrush;
}
///
/// Creates and returns a top face brush
///
/// Brush
/// Brush
internal static Brush GetTopFaceBrush(Brush brush)
{
if (brush != null)
{
if (typeof(SolidColorBrush).Equals(brush.GetType()))
{
SolidColorBrush solidBrush = brush as SolidColorBrush;
List colors = new List();
List stops = new List();
colors.Add(Graphics.GetDarkerColor(solidBrush.Color, 0.85));
stops.Add(0);
colors.Add(Graphics.GetLighterColor(solidBrush.Color, 0.35));
stops.Add(1);
return Graphics.CreateLinearGradientBrush(-45, new Point(0, 0.5), new Point(1, 0.5), colors, stops);
}
else if (brush is GradientBrush)
{
GradientBrush gradBrush = brush as GradientBrush;
List colors = new List();
List stops = new List();
foreach (GradientStop gradStop in gradBrush.GradientStops)
{
colors.Add(Graphics.GetLighterColor(gradStop.Color, 0.85));
stops.Add(gradStop.Offset);
}
if (brush is LinearGradientBrush)
return Graphics.CreateLinearGradientBrush(-45, new Point(-0.5, 1.5), new Point(0.5, 0), colors, stops);
else
return Graphics.CreateRadialGradientBrush(colors, stops);
}
else
{
return brush;
}
}
else
return null;
}
///
/// Creates and returns a right face brush
///
/// Brush
/// Brush
internal static Brush GetRightFaceBrush(Brush brush)
{
if (brush != null)
{
if (typeof(SolidColorBrush).Equals(brush.GetType()))
{
SolidColorBrush solidBrush = brush as SolidColorBrush;
List colors = new List();
List stops = new List();
colors.Add(Graphics.GetDarkerColor(solidBrush.Color, 0.35));
stops.Add(0);
colors.Add(Graphics.GetDarkerColor(solidBrush.Color, 0.75));
stops.Add(1);
return Graphics.CreateLinearGradientBrush(-120, new Point(0, 0.5), new Point(1, 0.5), colors, stops);
}
else if (brush is GradientBrush)
{
GradientBrush gradBrush = brush as GradientBrush;
List colors = new List();
List stops = new List();
foreach (GradientStop gradStop in gradBrush.GradientStops)
{
colors.Add(Graphics.GetDarkerColor(gradStop.Color, 0.75));
stops.Add(gradStop.Offset);
}
if (brush is LinearGradientBrush)
return Graphics.CreateLinearGradientBrush(0, new Point(0, 1), new Point(1, 0), colors, stops);
else
return Graphics.CreateRadialGradientBrush(colors, stops);
}
else
{
return brush;
}
}
else
return null;
}
///
/// Creates and returns a front face brush
///
/// Brush
/// Brush
internal static Brush GetFrontFaceBrush(Brush brush)
{
if (brush != null)
{
if (typeof(SolidColorBrush).Equals(brush.GetType()))
{
SolidColorBrush solidBrush = brush as SolidColorBrush;
List colors = new List();
List stops = new List();
colors.Add(Graphics.GetDarkerColor(solidBrush.Color, 0.65));
stops.Add(0);
colors.Add(Graphics.GetLighterColor(solidBrush.Color, 0.55));
stops.Add(1);
return Graphics.CreateLinearGradientBrush(-90, new Point(0, 0.5), new Point(1, 0.5), colors, stops);
}
else
{
return brush;
}
}
else
return null;
}
///
/// Creates and returns a left gradient brush
///
/// Alpha as Int32
/// Brush
internal static Brush GetLeftGradianceBrush(Int32 alpha)
{
LinearGradientBrush gradBrush = new LinearGradientBrush();
gradBrush.GradientStops = new GradientStopCollection();
gradBrush.StartPoint = new Point(0, 1);
gradBrush.EndPoint = new Point(1, 0);
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb((byte)alpha, 0, 0, 0), 0));
gradBrush.GradientStops.Add(Graphics.GetGradientStop(Color.FromArgb(0, 0, 0, 0), 0));
return gradBrush;
}
///
/// Creates and returns a line segment
///
/// Point
/// LineSegment
public static LineSegment GetLineSegment(Point point)
{
LineSegment lineSegment = new LineSegment();
lineSegment.Point = point;
return lineSegment;
}
///
/// Creates and returns an arc segment
///
/// Point
/// Size
/// Rotation as Double
/// Sweep as SweepDirection
/// ArcSegment
public static ArcSegment GetArcSegment(Point point, Size size, Double rotation, SweepDirection sweep)
{
ArcSegment arcSegment = new ArcSegment();
arcSegment.Point = point;
arcSegment.Size = size;
arcSegment.RotationAngle = 0;
arcSegment.SweepDirection = SweepDirection.Clockwise;
return arcSegment;
}
///
/// Convert scale based on the parameters
///
/// FromScaleMin
/// FromScaleMax
/// FromValue
/// ToScaleMin
/// ToScaleMax
/// Double
public static Double ConvertScale(Double fromScaleMin, Double fromScaleMax, Double fromValue, Double toScaleMin, Double toScaleMax)
{
return ((fromValue - fromScaleMin) * (toScaleMax - toScaleMin) / (fromScaleMax - fromScaleMin)) + toScaleMin;
}
///
/// Converts value to pixel position
///
/// Min position
/// Max position
/// Min value
/// Max value
/// Value
/// Double
public static Double ValueToPixelPosition(Double positionMin, Double positionMax, Double valueMin, Double valueMax, Double value)
{
return ((value - valueMin) / (valueMax - valueMin)) * (positionMax - positionMin) + positionMin;
}
///
/// Converts pixel position to value
///
/// Min position
/// Max position
/// Min value
/// Max value
/// position
/// Double
public static Double PixelPositionToValue(Double positionMin, Double positionMax, Double valueMin, Double valueMax, Double position)
{
return ((position - positionMin) / (positionMax - positionMin) * (valueMax - valueMin)) + valueMin;
}
///
/// Creates and returns gradient stop
///
/// Color as Color
/// Stop as Double
/// GradientStop
public static GradientStop GetGradientStop(Color color, Double stop)
{
GradientStop gradStop = new GradientStop();
gradStop.Color = color;
gradStop.Offset = stop;
return gradStop;
}
///
/// Creates and returns a linear gradient brush
///
/// Angle
/// Start point
/// End point
/// List of color
/// List of Double
/// Brush
public static Brush CreateLinearGradientBrush(Double angle, Point start, Point end, List colors, List stops)
{
LinearGradientBrush brush = new LinearGradientBrush();
if (colors.Count != stops.Count)
throw new Exception("Colors and Stops arrays don't match");
brush.StartPoint = start;
brush.EndPoint = end;
brush.GradientStops = new GradientStopCollection();
for (Int32 i = 0; i < colors.Count; i++)
{
brush.GradientStops.Add(GetGradientStop(colors[i],stops[i]));
}
RotateTransform rt = new RotateTransform();
rt.Angle = angle;
rt.CenterX = 0.5;
rt.CenterY = 0.5;
brush.RelativeTransform = rt;
return brush;
}
///
/// Creates and returns bevel top brush
///
/// Brush
/// Angle
/// Brush
internal static Brush GetBevelTopBrush(Brush brush, Double angle)
{
if (brush != null)
{
if (typeof(SolidColorBrush).Equals(brush.GetType()))
{
SolidColorBrush solidBrush = brush as SolidColorBrush;
Double r, g, b;
List colors = new List();
List stops = new List();
r = ((double)solidBrush.Color.R / (double)255) * 0.9999;
g = ((double)solidBrush.Color.G / (double)255) * 0.9999;
b = ((double)solidBrush.Color.B / (double)255) * 0.9999;
colors.Add(Graphics.GetLighterColor(solidBrush.Color, 0.99));
stops.Add(0);
colors.Add(Graphics.GetLighterColor(solidBrush.Color, 1 - r, 1 - g, 1 - b));
stops.Add(0.2);
colors.Add(Graphics.GetLighterColor(solidBrush.Color, 1 - r, 1 - g, 1 - b));
stops.Add(0.6);
colors.Add(Graphics.GetLighterColor(solidBrush.Color, 0.99));
stops.Add(1);
return Graphics.CreateLinearGradientBrush(angle, new Point(0, 0.5), new Point(1, 0.5), colors, stops);
}
else
{
return brush;
}
}
else
return null;
}
///
/// Creates and returns bevel side brush
///
/// Angle
/// Brush
/// Brush
internal static Brush GetBevelSideBrush(Double angle, Brush brush)
{
return Graphics.GetLightingEnabledBrush(brush, angle, "Linear", new Double[] { 0.75, 0.97});
}
///
/// Creates and returns a bevel top brush
///
/// Brush
/// Brush
internal static Brush GetBevelTopBrush(Brush brush)
{
return GetBevelTopBrush(brush, 90);
}
///
/// Creates and returns a lighting brush
///
/// Brush
/// Brush
internal static Brush GetLightingEnabledBrush3D(Brush brush)
{
return GetLightingEnabledBrush(brush, "Linear", new Double[] { 0.65, 0.55 });
}
///
/// Creates and returns a lighting brush
///
/// Brush
/// Angle
/// Type as String
/// Array of Double
/// Brush
internal static Brush GetLightingEnabledBrush(Brush brush, Double angle, String type, Double[] colorIntensies)
{
if (brush != null)
{
if (typeof(SolidColorBrush).Equals(brush.GetType()))
{
if (colorIntensies == null)
colorIntensies = new Double[] { 0.745, 0.99 };
SolidColorBrush solidBrush = brush as SolidColorBrush;
List colors = new List();
List stops = new List();
colors.Add(Graphics.GetDarkerColor(solidBrush.Color, colorIntensies[0]));
stops.Add(0);
colors.Add(Graphics.GetDarkerColor(solidBrush.Color, colorIntensies[1]));
stops.Add(1);
if (type == "Radial")
return Graphics.CreateRadialGradientBrush(colors, stops);
else
return Graphics.CreateLinearGradientBrush(angle, new Point(0, 0.5), new Point(1, 0.5), colors, stops);
}
else
{
return brush;
}
}
else
{
return brush;
}
}
///
/// Creates and returns a lighting brush
///
/// Brush
/// Type as String
/// Array of Double
/// Brush
internal static Brush GetLightingEnabledBrush(Brush brush, String type, Double[] colorIntensies)
{
return GetLightingEnabledBrush(brush, -90, type, colorIntensies);
}
///
/// Creates and returns a radial gradient brush
///
/// List of color
/// List of Double
/// Brush
public static Brush CreateRadialGradientBrush(List colors, List stops)
{
RadialGradientBrush brush = new RadialGradientBrush();
if (colors.Count != stops.Count)
throw new Exception("Colors and Stops arrays don't match");
brush.GradientStops = new GradientStopCollection();
for (Int32 i = 0; i < colors.Count; i++)
{
brush.GradientStops.Add(GetGradientStop(colors[i], stops[i]));
}
return brush;
}
///
/// Creates and returns a brush intensity
///
/// Brush
/// Double
public static Double GetBrushIntensity(Brush brush)
{
Color color = new Color();
Double intensity = 0;
if (brush == null) return 1;
if (brush.GetType().Name == "SolidColorBrush")
{
color = (brush as SolidColorBrush).Color;
intensity = (Double)(color.R + color.G + color.B) / (3 * 255);
}
else if (brush.GetType().Name == "LinearGradientBrush" || brush.GetType().Name == "RadialGradientBrush")
{
foreach (GradientStop grad in (brush as GradientBrush).GradientStops)
{
color = grad.Color;
intensity += (Double)(color.R + color.G + color.B) / (3 * 255);
}
intensity /= (brush as GradientBrush).GradientStops.Count;
}
else
{
intensity = 1;
}
return intensity;
}
///
/// Creates and returns a default font color based on intensity
///
/// Intensity as Double
/// Brush
public static Brush GetDefaultFontColor(Double intensity)
{
Brush brush = null;
if (intensity < 0.6)
{
//brush = ParseSolidColor("#EFEFEF");
brush = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239));
}
else
{
//brush = ParseSolidColor("#000000");
brush = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
}
return brush;
}
///
/// Compare brushes
///
/// First brush
/// Second brush
///
internal static bool AreBrushesEqual(Brush first, Brush second)
{
// If the default comparison is true, that's good enough.
if (object.Equals(first, second))
{
return true;
}
// Do a field by field comparison if they're not the same reference
SolidColorBrush firstSolidColorBrush = first as SolidColorBrush;
if (firstSolidColorBrush != null)
{
SolidColorBrush secondSolidColorBrush = second as SolidColorBrush;
if (secondSolidColorBrush != null)
{
return object.Equals(firstSolidColorBrush.Color, secondSolidColorBrush.Color);
}
}
return false;
}
///
/// Converts a color in String form to Solid Color Brush
///
/// ColorCode as String
/// Brush
public static Brush ParseSolidColor(String colorCode)
{
#if WPF
return (Brush)XamlReader.Load(new XmlTextReader(new System.IO.StringReader(String.Format(System.Globalization.CultureInfo.InvariantCulture, @"", colorCode))));
#else
return (Brush)XamlReader.Load(String.Format(System.Globalization.CultureInfo.InvariantCulture, @"", colorCode));
#endif
}
///
/// Returns a darker shade of the color by decreasing the brightness by the given intensity value
///
/// Color as Color
/// Intensity as Double
///
public static Color GetDarkerColor(Color color, Double intensity)
{
Color darkerShade = new Color();
intensity = (intensity < 0 || intensity > 1) ? 1 : intensity;
darkerShade.R = (Byte)(color.R * intensity);
darkerShade.G = (Byte)(color.G * intensity);
darkerShade.B = (Byte)(color.B * intensity);
darkerShade.A = color.A;
return darkerShade;
}
///
/// Returns a lighter shade of the color by increasing the brightness by the given intensity value
///
/// Color as Color
/// Intensity as Double
///
public static Color GetLighterColor(Color color, Double intensity)
{
Color lighterShade = new Color();
intensity = (intensity < 0 || intensity > 1) ? 1 : intensity;
lighterShade.R = (Byte)(256 - ((256 - color.R) * intensity));
lighterShade.G = (Byte)(256 - ((256 - color.G) * intensity));
lighterShade.B = (Byte)(256 - ((256 - color.B) * intensity));
lighterShade.A = color.A;
return lighterShade;
}
///
/// Creates and returns a lighter color based on the parameters
///
/// Color as Color
/// IntensityR as Double
/// IntensityG as Double
/// IntensityB as Double
/// Color
public static Color GetLighterColor(Color color, Double intensityR, Double intensityG, Double intensityB)
{
Color lighterShade = new Color();
intensityR = (intensityR < 0 || intensityR > 1) ? 1 : intensityR;
intensityG = (intensityG < 0 || intensityG > 1) ? 1 : intensityG;
intensityB = (intensityB < 0 || intensityB > 1) ? 1 : intensityB;
lighterShade.R = (Byte)(256 - ((256 - color.R) * intensityR));
lighterShade.G = (Byte)(256 - ((256 - color.G) * intensityG));
lighterShade.B = (Byte)(256 - ((256 - color.B) * intensityB));
lighterShade.A = color.A;
return lighterShade;
}
///
/// Converts LineStyle to StrokeDashArray
///
/// lineStyle as String
/// DashArray as DoubleCollection
public static DoubleCollection LineStyleToStrokeDashArray(String lineStyle)
{
DoubleCollection retVal = null;
switch (lineStyle)
{
case "Solid":
retVal = null;
break;
case "Dashed":
retVal = new DoubleCollection() { 4, 4, 4, 4 };
break;
case "Dotted":
retVal = new DoubleCollection() { 1, 2, 1, 2 };
break;
}
return retVal;
}
///
/// Creates and returns a lighting brush
///
/// Whether lighting is enabled
/// Brush
public static Brush LightingBrush(Boolean lightingEnabled)
{
Brush brush;
if (lightingEnabled)
{
String xaml = String.Format(@"
");
#if WPF
brush = (Brush)XamlReader.Load(new XmlTextReader(new System.IO.StringReader(xaml)));
#else
brush = System.Windows.Markup.XamlReader.Load(xaml) as Brush;
#endif
}
else
brush = new SolidColorBrush(Colors.Transparent);
return brush;
}
#endregion
#region Constants
public static Brush BLACK_BRUSH = new SolidColorBrush(Colors.Black);
public static Brush RED_BRUSH = new SolidColorBrush(Colors.Red);
///
/// Array of font sizes
///
public static Double[] DefaultFontSizes = { 8, 10, 12, 14, 16, 18, 20, 24, 28, 32, 36, 40 };
#endregion
}
}