Skip to content

Commit

Permalink
Removed duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
ben_singer committed Nov 11, 2024
1 parent c4cfae2 commit ff82a60
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 137 deletions.
112 changes: 2 additions & 110 deletions LoFiEffects.WPF/Effects/DegradeEffect.cs
Original file line number Diff line number Diff line change
@@ -1,112 +1,18 @@
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Effects;

namespace LoFiEffects.WPF.Effects
{
/// <summary>
/// Represents a degrade shader effect.
/// </summary>
public class DegradeEffect : ShaderEffect
public class DegradeEffect : SpecklingEffect
{
#region StaticFields

private static readonly PixelShader pixelShader = new() { UriSource = UriHelper.FromResource(@"Effects/Shaders/Degrade.ps") };

#endregion

#region Properties

/// <summary>
/// Get or set the input. This is a dependency property.
/// </summary>
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}

/// <summary>
/// Get or set the density. Higher values will produce more speckling. This is a dependency property.
/// </summary>
public double Density
{
get { return (double)GetValue(DensityProperty); }
set { SetValue(DensityProperty, value); }
}

/// <summary>
/// Get or set the intensity. Higher values will produce more intense speckling. This is a dependency property.
/// </summary>
public double Intensity
{
get { return (double)GetValue(IntensityProperty); }
set { SetValue(IntensityProperty, value); }
}

/// <summary>
/// Get or set the offset. This value can modified to produce different speckling patterns. This is a dependency property.
/// </summary>
public double Offset
{
get { return (double)GetValue(OffsetProperty); }
set { SetValue(OffsetProperty, value); }
}

/// <summary>
/// Get or set if the effect should be rendered over transparency. This is a dependency property.
/// </summary>
public bool RenderOverTransparent
{
get { return (bool)GetValue(RenderOverTransparentProperty); }
set { SetValue(RenderOverTransparentProperty, value); }
}

/// <summary>
/// Get or set if the effect should be rendered over transparency. This is a dependency property.
/// </summary>
protected double RenderOverTransparentDouble
{
get { return (double)GetValue(RenderOverTransparentDoubleProperty); }
set { SetValue(RenderOverTransparentDoubleProperty, value); }
}

#endregion

#region DependencyProperties

/// <summary>
/// Identifies the DegradeEffect.Input property.
/// </summary>
public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(DegradeEffect), 0);

/// <summary>
/// Identifies the DegradeEffect.Density property.
/// </summary>
public static readonly DependencyProperty DensityProperty = DependencyProperty.Register("Density", typeof(double), typeof(DegradeEffect), new UIPropertyMetadata(0.3, PixelShaderConstantCallback(0)));

/// <summary>
/// Identifies the DegradeEffect.Intensity property.
/// </summary>
public static readonly DependencyProperty IntensityProperty = DependencyProperty.Register("Intensity", typeof(double), typeof(DegradeEffect), new UIPropertyMetadata(0.8, PixelShaderConstantCallback(1)));

/// <summary>
/// Identifies the DegradeEffect.Offset property.
/// </summary>
public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("Offset", typeof(double), typeof(DegradeEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2)));

/// <summary>
/// Identifies the DegradeEffect.RenderOverTransparent property.
/// </summary>
public static readonly DependencyProperty RenderOverTransparentProperty = DependencyProperty.Register("RenderOverTransparent", typeof(bool), typeof(DegradeEffect), new PropertyMetadata(false, new PropertyChangedCallback(OnRenderOverTransparentPropertyChanged)));

/// <summary>
/// Identifies the DegradeEffect.RenderOverTransparentDouble property.
/// </summary>
public static readonly DependencyProperty RenderOverTransparentDoubleProperty = DependencyProperty.Register("RenderOverTransparentDouble", typeof(double), typeof(DegradeEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(3)));

#endregion

#region Constructors

/// <summary>
Expand All @@ -124,19 +30,5 @@ public DegradeEffect()
}

#endregion

#region StaticMethods

private static void OnRenderOverTransparentPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var degrade = obj as DegradeEffect;

if (degrade == null)
return;

degrade.RenderOverTransparentDouble = (bool)args.NewValue ? 1.0 : 0.0;
}

#endregion
}
}
30 changes: 3 additions & 27 deletions LoFiEffects.WPF/Effects/NegativeEffect.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Effects;

namespace LoFiEffects.WPF.Effects
{
/// <summary>
/// Represents a Negative shader effect.
/// Represents a negative shader effect.
/// </summary>
public class NegativeEffect : ShaderEffect
public class NegativeEffect : SpecklingEffect
{
#region StaticFields

private static readonly PixelShader pixelShader = new() { UriSource = UriHelper.FromResource(@"Effects/Shaders/Negative.ps") };

#endregion

#region Properties

/// <summary>
/// Get or set the input. This is a dependency property.
/// </summary>
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}

#endregion

#region DependencyProperties

/// <summary>
/// Identifies the NegativeEffect.Input property.
/// </summary>
public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(NegativeEffect), 0);

#endregion

#region Constructors

/// <summary>
Expand Down
118 changes: 118 additions & 0 deletions LoFiEffects.WPF/Effects/SpecklingEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace LoFiEffects.WPF.Effects
{
/// <summary>
/// Represents a speckling shader effect.
/// </summary>
public abstract class SpecklingEffect : ShaderEffect
{
#region Properties

/// <summary>
/// Get or set the input. This is a dependency property.
/// </summary>
public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}

/// <summary>
/// Get or set the density. Higher values will produce more speckling. This is a dependency property.
/// </summary>
public double Density
{
get { return (double)GetValue(DensityProperty); }
set { SetValue(DensityProperty, value); }
}

/// <summary>
/// Get or set the intensity. Higher values will produce more intense speckling. This is a dependency property.
/// </summary>
public double Intensity
{
get { return (double)GetValue(IntensityProperty); }
set { SetValue(IntensityProperty, value); }
}

/// <summary>
/// Get or set the offset. This value can modified to produce different speckling patterns. This is a dependency property.
/// </summary>
public double Offset
{
get { return (double)GetValue(OffsetProperty); }
set { SetValue(OffsetProperty, value); }
}

/// <summary>
/// Get or set if the effect should be rendered over transparency. This is a dependency property.
/// </summary>
public bool RenderOverTransparent
{
get { return (bool)GetValue(RenderOverTransparentProperty); }
set { SetValue(RenderOverTransparentProperty, value); }
}

/// <summary>
/// Get or set if the effect should be rendered over transparency. This is a dependency property.
/// </summary>
protected double RenderOverTransparentDouble
{
get { return (double)GetValue(RenderOverTransparentDoubleProperty); }
set { SetValue(RenderOverTransparentDoubleProperty, value); }
}

#endregion

#region DependencyProperties

/// <summary>
/// Identifies the SpecklingEffect.Input property.
/// </summary>
public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(SpecklingEffect), 0);

/// <summary>
/// Identifies the SpecklingEffect.Density property.
/// </summary>
public static readonly DependencyProperty DensityProperty = DependencyProperty.Register("Density", typeof(double), typeof(SpecklingEffect), new UIPropertyMetadata(0.3, PixelShaderConstantCallback(0)));

/// <summary>
/// Identifies the SpecklingEffect.Intensity property.
/// </summary>
public static readonly DependencyProperty IntensityProperty = DependencyProperty.Register("Intensity", typeof(double), typeof(SpecklingEffect), new UIPropertyMetadata(0.8, PixelShaderConstantCallback(1)));

/// <summary>
/// Identifies the SpecklingEffect.Offset property.
/// </summary>
public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("Offset", typeof(double), typeof(SpecklingEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2)));

/// <summary>
/// Identifies the SpecklingEffect.RenderOverTransparent property.
/// </summary>
public static readonly DependencyProperty RenderOverTransparentProperty = DependencyProperty.Register("RenderOverTransparent", typeof(bool), typeof(SpecklingEffect), new PropertyMetadata(false, new PropertyChangedCallback(OnRenderOverTransparentPropertyChanged)));

/// <summary>
/// Identifies the SpecklingEffect.RenderOverTransparentDouble property.
/// </summary>
public static readonly DependencyProperty RenderOverTransparentDoubleProperty = DependencyProperty.Register("RenderOverTransparentDouble", typeof(double), typeof(SpecklingEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(3)));

#endregion

#region StaticMethods

private static void OnRenderOverTransparentPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
var degrade = obj as SpecklingEffect;

if (degrade == null)
return;

degrade.RenderOverTransparentDouble = (bool)args.NewValue ? 1.0 : 0.0;
}

#endregion
}
}

0 comments on commit ff82a60

Please sign in to comment.