Skip to content

Commit

Permalink
prevent same key exception with brush creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Nov 22, 2024
1 parent 15fd353 commit bddb528
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions Project-Aurora/Project-Aurora/Utils/BrushUtils.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using D = System.Drawing;
using M = System.Windows.Media;
Expand Down Expand Up @@ -35,7 +37,7 @@ public static D.Brush MediaBrushToDrawingBrush(M.Brush in_brush)
(float)lgb.EndPoint.Y
);

D.Drawing2D.LinearGradientBrush brush = new D.Drawing2D.LinearGradientBrush(
LinearGradientBrush brush = new LinearGradientBrush(
starting_point,
ending_point,
D.Color.Red,
Expand Down Expand Up @@ -74,7 +76,7 @@ public static D.Brush MediaBrushToDrawingBrush(M.Brush in_brush)
brush_positions.Add(kvp.Key);
}

D.Drawing2D.ColorBlend color_blend = new D.Drawing2D.ColorBlend();
ColorBlend color_blend = new ColorBlend();
color_blend.Colors = brush_colors.ToArray();
color_blend.Positions = brush_positions.ToArray();
brush.InterpolationColors = color_blend;
Expand All @@ -97,10 +99,10 @@ public static D.Brush MediaBrushToDrawingBrush(M.Brush in_brush)
(float)rgb.Center.Y
);

D.Drawing2D.GraphicsPath g_path = new D.Drawing2D.GraphicsPath();
GraphicsPath g_path = new GraphicsPath();
g_path.AddEllipse(brush_region);

D.Drawing2D.PathGradientBrush brush = new D.Drawing2D.PathGradientBrush(g_path);
PathGradientBrush brush = new PathGradientBrush(g_path);

brush.CenterPoint = center_point;

Expand Down Expand Up @@ -136,7 +138,7 @@ public static D.Brush MediaBrushToDrawingBrush(M.Brush in_brush)
brush_positions.Add(kvp.Key);
}

D.Drawing2D.ColorBlend color_blend = new D.Drawing2D.ColorBlend();
ColorBlend color_blend = new ColorBlend();
color_blend.Colors = brush_colors.ToArray();
color_blend.Positions = brush_positions.ToArray();
brush.InterpolationColors = color_blend;
Expand All @@ -145,7 +147,7 @@ public static D.Brush MediaBrushToDrawingBrush(M.Brush in_brush)
}
else
{
return new D.SolidBrush(System.Drawing.Color.Red); //Return error color
return new D.SolidBrush(D.Color.Red); //Return error color
}
}

Expand All @@ -159,16 +161,16 @@ public static M.Brush DrawingBrushToMediaBrush(D.Brush in_brush)

return brush;
}
else if (in_brush is D.Drawing2D.LinearGradientBrush)
else if (in_brush is LinearGradientBrush)
{
D.Drawing2D.LinearGradientBrush lgb = (in_brush as D.Drawing2D.LinearGradientBrush);
LinearGradientBrush lgb = (in_brush as LinearGradientBrush);

System.Windows.Point starting_point = new System.Windows.Point(
Point starting_point = new Point(
lgb.Rectangle.X,
lgb.Rectangle.Y
);

System.Windows.Point ending_point = new System.Windows.Point(
Point ending_point = new Point(
lgb.Rectangle.Right,
lgb.Rectangle.Bottom
);
Expand Down Expand Up @@ -211,11 +213,11 @@ public static M.Brush DrawingBrushToMediaBrush(D.Brush in_brush)

return brush;
}
else if (in_brush is D.Drawing2D.PathGradientBrush)
else if (in_brush is PathGradientBrush)
{
D.Drawing2D.PathGradientBrush pgb = (in_brush as D.Drawing2D.PathGradientBrush);
PathGradientBrush pgb = (in_brush as PathGradientBrush);

System.Windows.Point starting_point = new System.Windows.Point(
Point starting_point = new Point(
pgb.CenterPoint.X,
pgb.CenterPoint.Y
);
Expand Down Expand Up @@ -245,7 +247,7 @@ public static M.Brush DrawingBrushToMediaBrush(D.Brush in_brush)
}
else
{
return new M.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 0, 0)); //Return error color
return new M.SolidColorBrush(M.Color.FromArgb(255, 255, 0, 0)); //Return error color
}
}
}
Expand Down Expand Up @@ -361,12 +363,26 @@ public M.LinearGradientBrush ToMediaBrush() {
/// <summary>
/// Creates a new stop collection from the given media brush.
/// </summary>
public static ColorStopCollection FromMediaBrush(M.Brush brush) {
if (brush is M.GradientBrush gb)
return new ColorStopCollection(gb.GradientStops.GroupBy(gs => gs.Offset).ToDictionary(gs => (float)gs.First().Offset, gs => gs.First().Color.ToDrawingColor()));
else if (brush is M.SolidColorBrush sb)
return new ColorStopCollection { { 0f, sb.Color.ToDrawingColor() } };
throw new InvalidOperationException($"Brush of type '{brush.GetType().Name} could not be converted to a ColorStopCollection.");
public static ColorStopCollection FromMediaBrush(M.Brush brush)
{
switch (brush)
{
case M.GradientBrush gb:
{
var colorPositions = gb.GradientStops
.GroupBy(gs => gs.Offset)
.Distinct()
.ToDictionary(
gs => (float)gs.First().Offset,
gs => gs.First().Color.ToDrawingColor()
);
return new ColorStopCollection(colorPositions);
}
case M.SolidColorBrush sb:
return new ColorStopCollection { { 0f, sb.Color.ToDrawingColor() } };
default:
throw new InvalidOperationException($"Brush of type '{brush.GetType().Name} could not be converted to a ColorStopCollection.");
}
}

/// <summary>
Expand All @@ -388,7 +404,7 @@ public static ColorStopCollection FromMediaBrush(M.Brush brush) {
/// Does not support converting back.
/// </summary>
public class ColorToBrushConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => new M.SolidColorBrush((value as M.Color?) ?? System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => new M.SolidColorBrush((value as M.Color?) ?? M.Color.FromArgb(0, 0, 0, 0));
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException();
}
}

0 comments on commit bddb528

Please sign in to comment.