Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / WPF

Stacked Geometry Brush Factory

Rate me:
Please Sign up or sign in to vote.
4.95/5 (68 votes)
16 Jul 2010CPOL13 min read 80K   2.6K   103  
A geometric visual brush producer with plug-in architecture and customized XAML.
using System;
using StackedGeometry;
using System.Windows;
using System.Windows.Media;
using System.ComponentModel;

namespace EffectFactories
{
    [EffectFactory][CustomXaml]
    public class NeonFactory : DependencyObject, IEffectFactory, INotifyPropertyChanged
    {

  

        public NeonFactory()
        {
            SetToDefault();

        }

 public void SetToDefault()
        {
            StartingBrightness = 1;
            StartingSaturation = .7;
            BrightnessMultiplier = new double[2] { 1, 1 };
            SaurationMultiplier = new double[2] { .5, .5 };
        }
 #region ComonPropertyCallbacks
 
 private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     NeonFactory nf = (NeonFactory)d;
     if (nf.Changed != null)
     {
         nf.Changed(nf, new EventArgs());
     }

 }

 private static object CoercePositiveDouble(DependencyObject d, object value)
 {
     double raw  = (double)value;
     return Math.Max(raw, 0);
 }


 #endregion

 #region Hue

 public static DependencyProperty HueProperty = DependencyProperty.Register("Hue",typeof(double), typeof(NeonFactory),
new PropertyMetadata(0.0,new PropertyChangedCallback(NeonFactory.OnHueChanged), new CoerceValueCallback(NeonFactory.CoerceHue)));

  public double Hue
        {

get { return (double)GetValue(HueProperty); }

set {  SetValue(HueProperty, value); }

}

private static object  CoerceHue(DependencyObject d, object value)
{
    double rawHue = (double) value;
    return rawHue % 360;
}
private static void OnHueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

{
   NeonFactory nf = (NeonFactory)d;
     if (nf.Changed != null)
        {
          nf.Changed(nf, new EventArgs());
        }
     }
 

       

#endregion  

#region StartingBrightness
        public static DependencyProperty StartingBrightnessProperty = DependencyProperty.Register("StartingBrightness", typeof(double), typeof(NeonFactory),
new PropertyMetadata(1.0, new PropertyChangedCallback(NeonFactory.OnStartingBrightnessChanged), new CoerceValueCallback(NeonFactory.CoerceStartingBrightness)));

        public double StartingBrightness
        {
            get { return (double)GetValue(StartingBrightnessProperty); }
            set { SetValue(StartingBrightnessProperty, value); }
        }

private static void OnStartingBrightnessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    NeonFactory nf = (NeonFactory)d;
    if (nf.Changed != null)
    {
        nf.Changed(nf, new EventArgs());
    }

}
private static object  CoerceStartingBrightness(DependencyObject d, object value)
{
    
    double rawStartingBrightness = (double)value;
    rawStartingBrightness = Math.Min(rawStartingBrightness,1);
    rawStartingBrightness= Math.Max(rawStartingBrightness, 0);
    return rawStartingBrightness;

}
#endregion

#region StartingSaturation
public static DependencyProperty StartingSaturationProperty = DependencyProperty.Register("StartingSaturation", typeof(double), typeof(NeonFactory),
new PropertyMetadata(.7, new PropertyChangedCallback(NeonFactory.OnStartingSaturationChanged), new CoerceValueCallback(NeonFactory.CoerceStartingSaturation)));

public double StartingSaturation
{

    get { return (double)GetValue(StartingSaturationProperty); }

    set { SetValue(StartingSaturationProperty, value); }

}


private static void OnStartingSaturationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

{
    NeonFactory nf = (NeonFactory)d;
                if (nf.Changed != null)
                {
                    nf.Changed(nf, new EventArgs());
                }
}

private static object CoerceStartingSaturation(DependencyObject d, object value)
{

    double rawStartingSaturation = (double)value;
    rawStartingSaturation = Math.Min(rawStartingSaturation, 1);
    rawStartingSaturation = Math.Max(rawStartingSaturation, 0);
    return rawStartingSaturation;

}

#endregion

public double[] SaurationMultiplier { get; set; }
        public double[] BrightnessMultiplier { get; set; }



 #region StartingThickness
        public static DependencyProperty StartingThicknessProperty = DependencyProperty.Register("StartingThickness", typeof(double), typeof(NeonFactory),
        new PropertyMetadata(2.0, new PropertyChangedCallback(NeonFactory.OnStartingThicknessChanged), new CoerceValueCallback(NeonFactory.CoerceStartingThickness)));

        public double StartingThickness
        {

            get { return (double)GetValue(StartingThicknessProperty); }

            set { SetValue(StartingThicknessProperty, value); }

        }


        private static void OnStartingThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            NeonFactory nf = (NeonFactory)d;
            if (nf.Changed != null)
            {
                nf.Changed(nf, new EventArgs());
            }

        }

        private static object CoerceStartingThickness(DependencyObject d, object value)
        {
            double rawStartingThickness = (double)value;
            return Math.Max(rawStartingThickness, 0);
        }
        #endregion


 #region GlowMultiplier
        public static DependencyProperty GlowMultiplierProperty = DependencyProperty.Register("GlowMultiplier", typeof(double), typeof(NeonFactory),
        new PropertyMetadata(1.0, OnPropertyChanged, CoercePositiveDouble));

        public double GlowMultiplier
        {

            get { return (double)GetValue(GlowMultiplierProperty); }

            set { SetValue(GlowMultiplierProperty, value); }

        }
 
 
        #endregion




#region IEffectFactory Members
       bool mIsFrozen;
       public bool IsFrozen
       {
           get
           {
               return mIsFrozen;
           }
           set
           {
               mIsFrozen = value;
           }
       }

        public event EventHandler Changed;

        public ShapeVisualPropsCollection CreateLayerCollection()
        {
            ShapeVisualPropsCollection svps = new ShapeVisualPropsCollection();
            ShapeVisualProps svp = new ShapeVisualProps();
            double currentSaturation = StartingSaturation;
            double currentBrightness = StartingBrightness;

            //Glow
            svp.Stroke = new SolidColorBrush(MediaColor(DevCorpColor.ColorSpaceHelper.HSBtoColor(Hue, currentSaturation, currentBrightness)));
            svp.StrokeThickness = StartingThickness * 4 * GlowMultiplier;
            svp.StrokeLineJoin = PenLineJoin.Round;
            System.Windows.Media.Effects.BlurEffect blur = new System.Windows.Media.Effects.BlurEffect();
            blur.Radius = 12;
            svp.Effect=blur ;
            svps.Add(svp);

            //glass edge
            svp = new ShapeVisualProps();
            currentSaturation *= SaurationMultiplier[0];
            currentBrightness *= BrightnessMultiplier[0];
            svp.Stroke = new SolidColorBrush(MediaColor(DevCorpColor.ColorSpaceHelper.HSBtoColor(Hue, currentSaturation, currentBrightness)));
            svp.StrokeThickness = StartingThickness * 2;
            svp.StrokeLineJoin = PenLineJoin.Round;
            svps.Add(svp);

             // Gas  Column
            svp = new ShapeVisualProps(); 
            currentSaturation *= SaurationMultiplier[1];
            currentBrightness *= BrightnessMultiplier[1];
            svp.Stroke = new SolidColorBrush(MediaColor(DevCorpColor.ColorSpaceHelper.HSBtoColor(Hue, currentSaturation, currentBrightness)));
            svp.StrokeThickness = StartingThickness ;
            svp.StrokeLineJoin = PenLineJoin.Round;
            svps.Add(svp);
 
            return svps;
        }


        private Color MediaColor(System.Drawing.Color drawColor)
        {

            return Color.FromRgb(drawColor.R, drawColor.G, drawColor.B);
        }
  

        public string DisplayName
        {
            get { return "Neon"; }
        }

        public DataTemplate DataTemplate
        {
            get { 
                      ResourceDictionary rd = new ResourceDictionary();
                        rd.Source = new Uri("EffectFactories;component/NeonResources.xaml", UriKind.Relative);
                        DataTemplate dt = (DataTemplate)rd["NeonFactoryTemplate"];
                        return dt;
                 }
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        #region IEffectFactory Members


        private StackedGeometry.StackedGeometry mStackedGeometry;


        public void SetStackedGeometry(StackedGeometry.StackedGeometry stackedGeometry)
        {
            mStackedGeometry = stackedGeometry;
        }

        public StackedGeometry.StackedGeometry GetStackedGeometry()
        {
            return mStackedGeometry;
        }


        private bool mDesignMode;
        [XamlIgnore]
        public bool DesignMode
        {
            get
            {
                return mDesignMode ;
            }
            set
            {
                mDesignMode = value;
            }
        }


        ShapeVisualPropsCollection mShapeVisualProps;

        [XamlIgnore("IsFrozen", false)]
        public ShapeVisualPropsCollection ShapeVisualProps
        {
            get
            {
                return mShapeVisualProps;
                
            }
            set
            {
                mShapeVisualProps = value;
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

Comments and Discussions