|
|
@@ -1,5 +1,6 @@
|
|
|
using System;
|
|
|
using Avalonia.Media;
|
|
|
+using Avalonia.Media.Immutable;
|
|
|
|
|
|
namespace InABox.Avalonia.Converters
|
|
|
{
|
|
|
@@ -9,13 +10,84 @@ namespace InABox.Avalonia.Converters
|
|
|
public IBrush False{ get; set; }
|
|
|
|
|
|
public IBrush True { get; set; }
|
|
|
+
|
|
|
+ public double Opacity { get; set; } = 1.0;
|
|
|
|
|
|
protected override IBrush Convert(bool value, object parameter = null)
|
|
|
{
|
|
|
return value
|
|
|
- ? True
|
|
|
- : False;
|
|
|
+ ? CloneBrushWithOpacity(True,Opacity)
|
|
|
+ : CloneBrushWithOpacity(False,Opacity);
|
|
|
+ }
|
|
|
+
|
|
|
+ private IBrush CloneBrushWithOpacity(IBrush value, double opacity)
|
|
|
+ {
|
|
|
+ opacity = Math.Clamp(opacity, 0.0, 1.0);
|
|
|
+
|
|
|
+ switch (value)
|
|
|
+ {
|
|
|
+
|
|
|
+ case ImmutableSolidColorBrush scb:
|
|
|
+ return new ImmutableSolidColorBrush(scb.Color, scb.Opacity * opacity);
|
|
|
+ case SolidColorBrush scb:
|
|
|
+ return new SolidColorBrush(scb.Color, scb.Opacity * opacity);
|
|
|
+
|
|
|
+ case LinearGradientBrush lgb:
|
|
|
+ var linear = new LinearGradientBrush
|
|
|
+ {
|
|
|
+ StartPoint = lgb.StartPoint,
|
|
|
+ EndPoint = lgb.EndPoint,
|
|
|
+ SpreadMethod = lgb.SpreadMethod,
|
|
|
+ GradientStops = CloneGradientStops(lgb.GradientStops, opacity)
|
|
|
+ };
|
|
|
+ linear.Transform = lgb.Transform;
|
|
|
+ return linear;
|
|
|
+
|
|
|
+ case RadialGradientBrush rgb:
|
|
|
+ var radial = new RadialGradientBrush
|
|
|
+ {
|
|
|
+ Center = rgb.Center,
|
|
|
+ GradientOrigin = rgb.GradientOrigin,
|
|
|
+ Radius = rgb.Radius,
|
|
|
+ SpreadMethod = rgb.SpreadMethod,
|
|
|
+ GradientStops = CloneGradientStops(rgb.GradientStops, opacity)
|
|
|
+ };
|
|
|
+ radial.Transform = rgb.Transform;
|
|
|
+ return radial;
|
|
|
+
|
|
|
+ case ImageBrush ib:
|
|
|
+ return new ImageBrush
|
|
|
+ {
|
|
|
+ Source = ib.Source,
|
|
|
+ AlignmentX = ib.AlignmentX,
|
|
|
+ AlignmentY = ib.AlignmentY,
|
|
|
+ Stretch = ib.Stretch,
|
|
|
+ TileMode = ib.TileMode,
|
|
|
+ DestinationRect = ib.DestinationRect,
|
|
|
+ SourceRect = ib.SourceRect,
|
|
|
+ Opacity = ib.Opacity * opacity
|
|
|
+ };
|
|
|
+
|
|
|
+ default:
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private GradientStops CloneGradientStops(GradientStops stops, double opacity)
|
|
|
+ {
|
|
|
+ var gs = new GradientStops();
|
|
|
+
|
|
|
+ foreach (var stop in stops)
|
|
|
+ {
|
|
|
+ gs.Add(new GradientStop(
|
|
|
+ stop.Color,
|
|
|
+ stop.Offset)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ return gs;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
}
|