From ac34ffe211bfa0d811f33a9e6141c0da97c55abe Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 14 Dec 2017 15:35:39 +0200 Subject: Implemented DAL Observables auto enumerations generation. --- .../Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj') diff --git a/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj b/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj index e9f1fd586..e2cc941e7 100644 --- a/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj +++ b/Software/Visual_Studio/Tango.CodeGeneration/Tango.CodeGeneration.csproj @@ -65,6 +65,8 @@ + + @@ -93,6 +95,7 @@ + + + + + + + + + + + diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorBlendConverter.cs b/Software/Visual_Studio/Tango.ColorPicker/ColorBlendConverter.cs new file mode 100644 index 000000000..3e61fea36 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorBlendConverter.cs @@ -0,0 +1,82 @@ +/************************************************************************************* + + Extended WPF Toolkit + + Copyright (C) 2007-2013 Xceed Software Inc. + + This program is provided to you under the terms of the Microsoft Public + License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license + + For more features, controls, and fast professional support, + pick up the Plus Edition at http://xceed.com/wpf_toolkit + + Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids + + ***********************************************************************************/ + +using System; +using System.Windows.Data; +using System.Windows.Media; + +namespace Tango.ColorPicker.Core.Converters +{ + /// + /// This converter allow to blend two colors into one based on a specified ratio + /// + internal class ColorBlendConverter : IValueConverter + { + private double _blendedColorRatio = 0; + + /// + /// The ratio of the blended color. Must be between 0 and 1. + /// + public double BlendedColorRatio + { + get { return _blendedColorRatio; } + + set + { + if (value < 0d || value > 1d) + throw new ArgumentException("BlendedColorRatio must greater than or equal to 0 and lower than or equal to 1 "); + + _blendedColorRatio = value; + } + } + + /// + /// The color to blend with the source color + /// + public Color BlendedColor { get; set; } + + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + if (value == null || value.GetType() != typeof(Color)) + return null; + + Color color = (Color)value; + return new Color() + { + A = this.BlendValue(color.A, this.BlendedColor.A), + R = this.BlendValue(color.R, this.BlendedColor.R), + G = this.BlendValue(color.G, this.BlendedColor.G), + B = this.BlendValue(color.B, this.BlendedColor.B) + }; + } + + private byte BlendValue(byte original, byte blend) + { + double blendRatio = this.BlendedColorRatio; + double sourceRatio = 1 - blendRatio; + + double result = (((double)original) * sourceRatio) + (((double)blend) * blendRatio); + result = Math.Round(result); + result = Math.Min(255d, Math.Max(0d, result)); + return System.Convert.ToByte(result); + } + + public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Implementation/ColorCanvas.cs b/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Implementation/ColorCanvas.cs new file mode 100644 index 000000000..da7176f61 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Implementation/ColorCanvas.cs @@ -0,0 +1,602 @@ +/************************************************************************************* + + Extended WPF Toolkit + + Copyright (C) 2007-2013 Xceed Software Inc. + + This program is provided to you under the terms of the Microsoft Public + License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license + + For more features, controls, and fast professional support, + pick up the Plus Edition at http://xceed.com/wpf_toolkit + + Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids + + ***********************************************************************************/ + +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Input; +using System.Windows.Media; +using Tango.ColorPicker.Core.Utilities; +using Tango.ColorPicker.Primitives; +using System.IO; +using System; + +namespace Tango +{ + [TemplatePart( Name = PART_ColorShadingCanvas, Type = typeof( Canvas ) )] + [TemplatePart( Name = PART_ColorShadeSelector, Type = typeof( Canvas ) )] + [TemplatePart( Name = PART_SpectrumSlider, Type = typeof( ColorSpectrumSlider ) )] + [TemplatePart( Name = PART_HexadecimalTextBox, Type = typeof( TextBox ) )] + internal class ColorCanvas : Control + { + private const string PART_ColorShadingCanvas = "PART_ColorShadingCanvas"; + private const string PART_ColorShadeSelector = "PART_ColorShadeSelector"; + private const string PART_SpectrumSlider = "PART_SpectrumSlider"; + private const string PART_HexadecimalTextBox = "PART_HexadecimalTextBox"; + + #region Private Members + + private TranslateTransform _colorShadeSelectorTransform = new TranslateTransform(); + private Canvas _colorShadingCanvas; + private Canvas _colorShadeSelector; + private ColorSpectrumSlider _spectrumSlider; + private TextBox _hexadecimalTextBox; + private Point? _currentColorPosition; + private bool _surpressPropertyChanged; + private bool _updateSpectrumSliderValue = true; + + #endregion //Private Members + + #region Properties + + #region SelectedColor + + public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register( "SelectedColor", typeof( Color? ), typeof( ColorCanvas ), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedColorChanged ) ); + public Color? SelectedColor + { + get + { + return ( Color? )GetValue( SelectedColorProperty ); + } + set + { + SetValue( SelectedColorProperty, value ); + } + } + + private static void OnSelectedColorChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnSelectedColorChanged( ( Color? )e.OldValue, ( Color? )e.NewValue ); + } + + protected virtual void OnSelectedColorChanged( Color? oldValue, Color? newValue ) + { + SetHexadecimalStringProperty( GetFormatedColorString( newValue ), false ); + UpdateRGBValues( newValue ); + UpdateColorShadeSelectorPosition( newValue ); + + RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs( oldValue, newValue ); + args.RoutedEvent = SelectedColorChangedEvent; + RaiseEvent( args ); + } + + #endregion //SelectedColor + + #region RGB + + #region A + + public static readonly DependencyProperty AProperty = DependencyProperty.Register( "A", typeof( byte ), typeof( ColorCanvas ), new UIPropertyMetadata( ( byte )255, OnAChanged ) ); + public byte A + { + get + { + return ( byte )GetValue( AProperty ); + } + set + { + SetValue( AProperty, value ); + } + } + + private static void OnAChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnAChanged( ( byte )e.OldValue, ( byte )e.NewValue ); + } + + protected virtual void OnAChanged( byte oldValue, byte newValue ) + { + if( !_surpressPropertyChanged ) + UpdateSelectedColor(); + } + + #endregion //A + + #region R + + public static readonly DependencyProperty RProperty = DependencyProperty.Register( "R", typeof( byte ), typeof( ColorCanvas ), new UIPropertyMetadata( ( byte )0, OnRChanged ) ); + public byte R + { + get + { + return ( byte )GetValue( RProperty ); + } + set + { + SetValue( RProperty, value ); + } + } + + private static void OnRChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnRChanged( ( byte )e.OldValue, ( byte )e.NewValue ); + } + + protected virtual void OnRChanged( byte oldValue, byte newValue ) + { + if( !_surpressPropertyChanged ) + UpdateSelectedColor(); + } + + #endregion //R + + #region G + + public static readonly DependencyProperty GProperty = DependencyProperty.Register( "G", typeof( byte ), typeof( ColorCanvas ), new UIPropertyMetadata( ( byte )0, OnGChanged ) ); + public byte G + { + get + { + return ( byte )GetValue( GProperty ); + } + set + { + SetValue( GProperty, value ); + } + } + + private static void OnGChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnGChanged( ( byte )e.OldValue, ( byte )e.NewValue ); + } + + protected virtual void OnGChanged( byte oldValue, byte newValue ) + { + if( !_surpressPropertyChanged ) + UpdateSelectedColor(); + } + + #endregion //G + + #region B + + public static readonly DependencyProperty BProperty = DependencyProperty.Register( "B", typeof( byte ), typeof( ColorCanvas ), new UIPropertyMetadata( ( byte )0, OnBChanged ) ); + public byte B + { + get + { + return ( byte )GetValue( BProperty ); + } + set + { + SetValue( BProperty, value ); + } + } + + private static void OnBChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnBChanged( ( byte )e.OldValue, ( byte )e.NewValue ); + } + + protected virtual void OnBChanged( byte oldValue, byte newValue ) + { + if( !_surpressPropertyChanged ) + UpdateSelectedColor(); + } + + #endregion //B + + #endregion //RGB + + #region HexadecimalString + + public static readonly DependencyProperty HexadecimalStringProperty = DependencyProperty.Register( "HexadecimalString", typeof( string ), typeof( ColorCanvas ), new UIPropertyMetadata( "", OnHexadecimalStringChanged, OnCoerceHexadecimalString ) ); + public string HexadecimalString + { + get + { + return ( string )GetValue( HexadecimalStringProperty ); + } + set + { + SetValue( HexadecimalStringProperty, value ); + } + } + + private static void OnHexadecimalStringChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnHexadecimalStringChanged( ( string )e.OldValue, ( string )e.NewValue ); + } + + protected virtual void OnHexadecimalStringChanged( string oldValue, string newValue ) + { + string newColorString = GetFormatedColorString( newValue ); + string currentColorString = GetFormatedColorString( SelectedColor ); + if( !currentColorString.Equals( newColorString ) ) + { + Color? col = null; + if( !string.IsNullOrEmpty( newColorString ) ) + { + col = ( Color )ColorConverter.ConvertFromString( newColorString ); + } + UpdateSelectedColor( col ); + } + + SetHexadecimalTextBoxTextProperty( newValue ); + } + + private static object OnCoerceHexadecimalString( DependencyObject d, object basevalue ) + { + var colorCanvas = ( ColorCanvas )d; + if( colorCanvas == null ) + return basevalue; + + return colorCanvas.OnCoerceHexadecimalString( basevalue ); + } + + private object OnCoerceHexadecimalString( object newValue ) + { + var value = newValue as string; + string retValue = value; + + try + { + if( !string.IsNullOrEmpty( retValue ) ) + { + ColorConverter.ConvertFromString( value ); + } + } + catch + { + //When HexadecimalString is changed via Code-Behind and hexadecimal format is bad, throw. + throw new InvalidDataException( "Color provided is not in the correct format." ); + } + + return retValue; + } + + #endregion //HexadecimalString + + #region UsingAlphaChannel + + public static readonly DependencyProperty UsingAlphaChannelProperty = DependencyProperty.Register( "UsingAlphaChannel", typeof( bool ), typeof( ColorCanvas ), new FrameworkPropertyMetadata( true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback( OnUsingAlphaChannelPropertyChanged ) ) ); + public bool UsingAlphaChannel + { + get + { + return ( bool )GetValue( UsingAlphaChannelProperty ); + } + set + { + SetValue( UsingAlphaChannelProperty, value ); + } + } + + private static void OnUsingAlphaChannelPropertyChanged( DependencyObject o, DependencyPropertyChangedEventArgs e ) + { + ColorCanvas colorCanvas = o as ColorCanvas; + if( colorCanvas != null ) + colorCanvas.OnUsingAlphaChannelChanged(); + } + + protected virtual void OnUsingAlphaChannelChanged() + { + SetHexadecimalStringProperty( GetFormatedColorString( SelectedColor ), false ); + } + + #endregion //UsingAlphaChannel + + #endregion //Properties + + #region Constructors + + static ColorCanvas() + { + DefaultStyleKeyProperty.OverrideMetadata( typeof( ColorCanvas ), new FrameworkPropertyMetadata( typeof( ColorCanvas ) ) ); + } + + #endregion //Constructors + + #region Base Class Overrides + + public override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + if( _colorShadingCanvas != null ) + { + _colorShadingCanvas.MouseLeftButtonDown -= ColorShadingCanvas_MouseLeftButtonDown; + _colorShadingCanvas.MouseLeftButtonUp -= ColorShadingCanvas_MouseLeftButtonUp; + _colorShadingCanvas.MouseMove -= ColorShadingCanvas_MouseMove; + _colorShadingCanvas.SizeChanged -= ColorShadingCanvas_SizeChanged; + } + + _colorShadingCanvas = GetTemplateChild( PART_ColorShadingCanvas ) as Canvas; + + if( _colorShadingCanvas != null ) + { + _colorShadingCanvas.MouseLeftButtonDown += ColorShadingCanvas_MouseLeftButtonDown; + _colorShadingCanvas.MouseLeftButtonUp += ColorShadingCanvas_MouseLeftButtonUp; + _colorShadingCanvas.MouseMove += ColorShadingCanvas_MouseMove; + _colorShadingCanvas.SizeChanged += ColorShadingCanvas_SizeChanged; + } + + _colorShadeSelector = GetTemplateChild( PART_ColorShadeSelector ) as Canvas; + + if( _colorShadeSelector != null ) + _colorShadeSelector.RenderTransform = _colorShadeSelectorTransform; + + if( _spectrumSlider != null ) + _spectrumSlider.ValueChanged -= SpectrumSlider_ValueChanged; + + _spectrumSlider = GetTemplateChild( PART_SpectrumSlider ) as ColorSpectrumSlider; + + if( _spectrumSlider != null ) + _spectrumSlider.ValueChanged += SpectrumSlider_ValueChanged; + + if( _hexadecimalTextBox != null ) + _hexadecimalTextBox.LostFocus -= new RoutedEventHandler( HexadecimalTextBox_LostFocus ); + + _hexadecimalTextBox = GetTemplateChild( PART_HexadecimalTextBox ) as TextBox; + + if( _hexadecimalTextBox != null ) + _hexadecimalTextBox.LostFocus += new RoutedEventHandler( HexadecimalTextBox_LostFocus ); + + UpdateRGBValues( SelectedColor ); + UpdateColorShadeSelectorPosition( SelectedColor ); + + // When changing theme, HexadecimalString needs to be set since it is not binded. + SetHexadecimalTextBoxTextProperty( GetFormatedColorString( SelectedColor ) ); + } + + protected override void OnKeyDown( KeyEventArgs e ) + { + base.OnKeyDown( e ); + + //hitting enter on textbox will update Hexadecimal string + if( e.Key == Key.Enter && e.OriginalSource is TextBox ) + { + TextBox textBox = ( TextBox )e.OriginalSource; + if( textBox.Name == PART_HexadecimalTextBox ) + SetHexadecimalStringProperty( textBox.Text, true ); + } + } + + #endregion //Base Class Overrides + + #region Event Handlers + + void ColorShadingCanvas_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) + { + Point p = e.GetPosition( _colorShadingCanvas ); + UpdateColorShadeSelectorPositionAndCalculateColor( p, true ); + _colorShadingCanvas.CaptureMouse(); + //Prevent from closing ColorCanvas after mouseDown in ListView + e.Handled = true; + } + + void ColorShadingCanvas_MouseLeftButtonUp( object sender, MouseButtonEventArgs e ) + { + _colorShadingCanvas.ReleaseMouseCapture(); + } + + void ColorShadingCanvas_MouseMove( object sender, MouseEventArgs e ) + { + if( e.LeftButton == MouseButtonState.Pressed ) + { + Point p = e.GetPosition( _colorShadingCanvas ); + UpdateColorShadeSelectorPositionAndCalculateColor( p, true ); + Mouse.Synchronize(); + } + } + + void ColorShadingCanvas_SizeChanged( object sender, SizeChangedEventArgs e ) + { + if( _currentColorPosition != null ) + { + Point _newPoint = new Point + { + X = ( ( Point )_currentColorPosition ).X * e.NewSize.Width, + Y = ( ( Point )_currentColorPosition ).Y * e.NewSize.Height + }; + + UpdateColorShadeSelectorPositionAndCalculateColor( _newPoint, false ); + } + } + + void SpectrumSlider_ValueChanged( object sender, RoutedPropertyChangedEventArgs e ) + { + if( (_currentColorPosition != null) && (this.SelectedColor != null) ) + { + CalculateColor( ( Point )_currentColorPosition ); + } + } + + void HexadecimalTextBox_LostFocus( object sender, RoutedEventArgs e ) + { + TextBox textbox = sender as TextBox; + SetHexadecimalStringProperty( textbox.Text, true ); + } + + #endregion //Event Handlers + + #region Events + + public static readonly RoutedEvent SelectedColorChangedEvent = EventManager.RegisterRoutedEvent( "SelectedColorChanged", RoutingStrategy.Bubble, typeof( RoutedPropertyChangedEventHandler ), typeof( ColorCanvas ) ); + public event RoutedPropertyChangedEventHandler SelectedColorChanged + { + add + { + AddHandler( SelectedColorChangedEvent, value ); + } + remove + { + RemoveHandler( SelectedColorChangedEvent, value ); + } + } + + #endregion //Events + + #region Methods + + private void UpdateSelectedColor() + { + SelectedColor = Color.FromArgb( A, R, G, B ); + } + + private void UpdateSelectedColor( Color? color ) + { + SelectedColor = ( ( color != null ) && color.HasValue ) + ? (Color?)Color.FromArgb( color.Value.A, color.Value.R, color.Value.G, color.Value.B ) + : null; + } + + private void UpdateRGBValues( Color? color ) + { + if( ( color == null ) || !color.HasValue ) + return; + + _surpressPropertyChanged = true; + + A = color.Value.A; + R = color.Value.R; + G = color.Value.G; + B = color.Value.B; + + _surpressPropertyChanged = false; + } + + private void UpdateColorShadeSelectorPositionAndCalculateColor( Point p, bool calculateColor ) + { + if( p.Y < 0 ) + p.Y = 0; + + if( p.X < 0 ) + p.X = 0; + + if( p.X > _colorShadingCanvas.ActualWidth ) + p.X = _colorShadingCanvas.ActualWidth; + + if( p.Y > _colorShadingCanvas.ActualHeight ) + p.Y = _colorShadingCanvas.ActualHeight; + + _colorShadeSelectorTransform.X = p.X - ( _colorShadeSelector.Width / 2 ); + _colorShadeSelectorTransform.Y = p.Y - ( _colorShadeSelector.Height / 2 ); + + p.X = p.X / _colorShadingCanvas.ActualWidth; + p.Y = p.Y / _colorShadingCanvas.ActualHeight; + + _currentColorPosition = p; + + if( calculateColor ) + CalculateColor( p ); + } + + private void UpdateColorShadeSelectorPosition( Color? color ) + { + if( (_spectrumSlider == null) || (_colorShadingCanvas == null) || (color == null) || !color.HasValue) + return; + + _currentColorPosition = null; + + HsvColor hsv = ColorUtilities.ConvertRgbToHsv( color.Value.R, color.Value.G, color.Value.B ); + + if( _updateSpectrumSliderValue ) + { + _spectrumSlider.Value = hsv.H; + } + + Point p = new Point( hsv.S, 1 - hsv.V ); + + _currentColorPosition = p; + + _colorShadeSelectorTransform.X = ( p.X * _colorShadingCanvas.Width ) - 5; + _colorShadeSelectorTransform.Y = ( p.Y * _colorShadingCanvas.Height ) - 5; + } + + private void CalculateColor( Point p ) + { + HsvColor hsv = new HsvColor( 360 - _spectrumSlider.Value, 1, 1 ) + { + S = p.X, + V = 1 - p.Y + }; + var currentColor = ColorUtilities.ConvertHsvToRgb( hsv.H, hsv.S, hsv.V ); + currentColor.A = A; + _updateSpectrumSliderValue = false; + SelectedColor = currentColor; + _updateSpectrumSliderValue = true; + SetHexadecimalStringProperty( GetFormatedColorString( SelectedColor ), false ); + } + + private string GetFormatedColorString( Color? colorToFormat ) + { + if( ( colorToFormat == null ) || !colorToFormat.HasValue ) + return string.Empty; + return ColorUtilities.FormatColorString( colorToFormat.ToString(), UsingAlphaChannel ); + } + + private string GetFormatedColorString( string stringToFormat ) + { + return ColorUtilities.FormatColorString( stringToFormat, UsingAlphaChannel ); + } + + private void SetHexadecimalStringProperty( string newValue, bool modifyFromUI ) + { + if( modifyFromUI ) + { + try + { + if( !string.IsNullOrEmpty( newValue ) ) + { + ColorConverter.ConvertFromString( newValue ); + } + HexadecimalString = newValue; + } + catch + { + //When HexadecimalString is changed via UI and hexadecimal format is bad, keep the previous HexadecimalString. + SetHexadecimalTextBoxTextProperty( HexadecimalString ); + } + } + else + { + //When HexadecimalString is changed via Code-Behind, hexadecimal format will be evaluated in OnCoerceHexadecimalString() + HexadecimalString = newValue; + } + } + + private void SetHexadecimalTextBoxTextProperty( string newValue ) + { + if( _hexadecimalTextBox != null ) + _hexadecimalTextBox.Text = newValue; + } + + #endregion //Methods + } +} diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Implementation/ColorSpectrumSlider.cs b/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Implementation/ColorSpectrumSlider.cs new file mode 100644 index 000000000..06233ba35 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Implementation/ColorSpectrumSlider.cs @@ -0,0 +1,111 @@ +/************************************************************************************* + + Extended WPF Toolkit + + Copyright (C) 2007-2013 Xceed Software Inc. + + This program is provided to you under the terms of the Microsoft Public + License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license + + For more features, controls, and fast professional support, + pick up the Plus Edition at http://xceed.com/wpf_toolkit + + Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids + + ***********************************************************************************/ + +using System; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Media; +using System.Windows.Shapes; +using Tango.ColorPicker.Core.Utilities; + +namespace Tango +{ + [TemplatePart( Name = PART_SpectrumDisplay, Type = typeof( Rectangle ) )] + internal class ColorSpectrumSlider : Slider + { + private const string PART_SpectrumDisplay = "PART_SpectrumDisplay"; + + #region Private Members + + private Rectangle _spectrumDisplay; + private LinearGradientBrush _pickerBrush; + + #endregion //Private Members + + #region Constructors + + static ColorSpectrumSlider() + { + DefaultStyleKeyProperty.OverrideMetadata( typeof( ColorSpectrumSlider ), new FrameworkPropertyMetadata( typeof( ColorSpectrumSlider ) ) ); + } + + #endregion //Constructors + + #region Dependency Properties + + public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register( "SelectedColor", typeof( Color ), typeof( ColorSpectrumSlider ), new PropertyMetadata( System.Windows.Media.Colors.Transparent ) ); + public Color SelectedColor + { + get + { + return ( Color )GetValue( SelectedColorProperty ); + } + set + { + SetValue( SelectedColorProperty, value ); + } + } + + #endregion //Dependency Properties + + #region Base Class Overrides + + public override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + _spectrumDisplay = ( Rectangle )GetTemplateChild( PART_SpectrumDisplay ); + CreateSpectrum(); + OnValueChanged( Double.NaN, Value ); + } + + protected override void OnValueChanged( double oldValue, double newValue ) + { + base.OnValueChanged( oldValue, newValue ); + + Color color = ColorUtilities.ConvertHsvToRgb( 360 - newValue, 1, 1 ); + SelectedColor = color; + } + + #endregion //Base Class Overrides + + #region Methods + + private void CreateSpectrum() + { + _pickerBrush = new LinearGradientBrush(); + _pickerBrush.StartPoint = new Point( 0.5, 0 ); + _pickerBrush.EndPoint = new Point( 0.5, 1 ); + _pickerBrush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; + + List colorsList = ColorUtilities.GenerateHsvSpectrum(); + + double stopIncrement = ( double )1 / colorsList.Count; + + int i; + for( i = 0; i < colorsList.Count; i++ ) + { + _pickerBrush.GradientStops.Add( new GradientStop( colorsList[ i ], i * stopIncrement ) ); + } + + _pickerBrush.GradientStops[ i - 1 ].Offset = 1.0; + _spectrumDisplay.Fill = _pickerBrush; + } + + #endregion //Methods + } +} diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Themes/Generic.xaml b/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Themes/Generic.xaml new file mode 100644 index 000000000..a54e2f6a5 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorCanvas/Themes/Generic.xaml @@ -0,0 +1,602 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorItem.cs b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorItem.cs new file mode 100644 index 000000000..dbf1860f7 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorItem.cs @@ -0,0 +1,53 @@ +/************************************************************************************* + + Extended WPF Toolkit + + Copyright (C) 2007-2013 Xceed Software Inc. + + This program is provided to you under the terms of the Microsoft Public + License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license + + For more features, controls, and fast professional support, + pick up the Plus Edition at http://xceed.com/wpf_toolkit + + Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids + + ***********************************************************************************/ + +using System.Windows.Media; + +namespace Tango +{ + internal class ColorItem + { + public Color? Color + { + get; + set; + } + public string Name + { + get; + set; + } + + public ColorItem(Color? color, string name) + { + Color = color; + Name = name; + } + + public override bool Equals(object obj) + { + var ci = obj as ColorItem; + if (ci == null) + return false; + return (ci.Color.Equals(Color) && ci.Name.Equals(Name)); + } + + public override int GetHashCode() + { + return this.Color.GetHashCode() ^ this.Name.GetHashCode(); + } + } +} diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorPicker.cs b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorPicker.cs new file mode 100644 index 000000000..aff0ae661 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorPicker.cs @@ -0,0 +1,770 @@ +/************************************************************************************* + + Extended WPF Toolkit + + Copyright (C) 2007-2013 Xceed Software Inc. + + This program is provided to you under the terms of the Microsoft Public + License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license + + For more features, controls, and fast professional support, + pick up the Plus Edition at http://xceed.com/wpf_toolkit + + Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids + + ***********************************************************************************/ + +using System; +using System.Collections.ObjectModel; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Media; +using Tango.ColorPicker.Core.Utilities; +using System.Windows.Controls.Primitives; +using System.Linq; +using System.Collections.Generic; +using System.Windows.Data; +using Tango.ColorPicker; + +namespace Tango +{ + internal enum ColorMode + { + ColorPalette, + ColorCanvas + } + + internal enum ColorSortingMode + { + Alphabetical, + HueSaturationBrightness + } + + [TemplatePart(Name = PART_AvailableColors, Type = typeof(ListBox))] + [TemplatePart(Name = PART_StandardColors, Type = typeof(ListBox))] + [TemplatePart(Name = PART_RecentColors, Type = typeof(ListBox))] + [TemplatePart(Name = PART_ColorPickerToggleButton, Type = typeof(ToggleButton))] + [TemplatePart(Name = PART_ColorPickerPalettePopup, Type = typeof(Popup))] + [TemplatePart(Name = PART_ColorModeButton, Type = typeof(Button))] + public class ColorPickerCombo : Control + { + private const string PART_AvailableColors = "PART_AvailableColors"; + private const string PART_StandardColors = "PART_StandardColors"; + private const string PART_RecentColors = "PART_RecentColors"; + private const string PART_ColorPickerToggleButton = "PART_ColorPickerToggleButton"; + private const string PART_ColorPickerPalettePopup = "PART_ColorPickerPalettePopup"; + private const string PART_ColorModeButton = "PART_ColorModeButton"; + + #region Members + + private ListBox _availableColors; + private ListBox _standardColors; + private ListBox _recentColors; + private ToggleButton _toggleButton; + private Popup _popup; + private Button _colorModeButton; + private Color? _initialColor; + private bool _selectionChanged; + + #endregion //Members + + #region Properties + + #region AdvancedButtonHeader + + public static readonly DependencyProperty AdvancedButtonHeaderProperty = DependencyProperty.Register("AdvancedButtonHeader", typeof(string), typeof(ColorPickerCombo), new UIPropertyMetadata("Advanced")); + public string AdvancedButtonHeader + { + get + { + return (string)GetValue(AdvancedButtonHeaderProperty); + } + set + { + SetValue(AdvancedButtonHeaderProperty, value); + } + } + + #endregion //AdvancedButtonHeader + + #region AvailableColors + + internal static readonly DependencyProperty AvailableColorsProperty = DependencyProperty.Register("AvailableColors", typeof(ObservableCollection), typeof(ColorPickerCombo), new UIPropertyMetadata(CreateAvailableColors())); + internal ObservableCollection AvailableColors + { + get + { + return (ObservableCollection)GetValue(AvailableColorsProperty); + } + set + { + SetValue(AvailableColorsProperty, value); + } + } + + #endregion //AvailableColors + + #region AvailableColorsSortingMode + + internal static readonly DependencyProperty AvailableColorsSortingModeProperty = DependencyProperty.Register("AvailableColorsSortingMode", typeof(ColorSortingMode), typeof(ColorPickerCombo), new UIPropertyMetadata(ColorSortingMode.Alphabetical, OnAvailableColorsSortingModeChanged)); + internal ColorSortingMode AvailableColorsSortingMode + { + get + { + return (ColorSortingMode)GetValue(AvailableColorsSortingModeProperty); + } + set + { + SetValue(AvailableColorsSortingModeProperty, value); + } + } + + private static void OnAvailableColorsSortingModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ColorPickerCombo colorPicker = (ColorPickerCombo)d; + if (colorPicker != null) + colorPicker.OnAvailableColorsSortingModeChanged((ColorSortingMode)e.OldValue, (ColorSortingMode)e.NewValue); + } + + private void OnAvailableColorsSortingModeChanged(ColorSortingMode oldValue, ColorSortingMode newValue) + { + ListCollectionView lcv = (ListCollectionView)(CollectionViewSource.GetDefaultView(this.AvailableColors)); + if (lcv != null) + { + lcv.CustomSort = (AvailableColorsSortingMode == ColorSortingMode.HueSaturationBrightness) + ? new ColorSorter() + : null; + } + } + + #endregion //AvailableColorsSortingMode + + #region AvailableColorsHeader + + public static readonly DependencyProperty AvailableColorsHeaderProperty = DependencyProperty.Register("AvailableColorsHeader", typeof(string), typeof(ColorPickerCombo), new UIPropertyMetadata("Available Colors")); + public string AvailableColorsHeader + { + get + { + return (string)GetValue(AvailableColorsHeaderProperty); + } + set + { + SetValue(AvailableColorsHeaderProperty, value); + } + } + + #endregion //AvailableColorsHeader + + #region ButtonStyle + + public static readonly DependencyProperty ButtonStyleProperty = DependencyProperty.Register("ButtonStyle", typeof(Style), typeof(ColorPickerCombo)); + public Style ButtonStyle + { + get + { + return (Style)GetValue(ButtonStyleProperty); + } + set + { + SetValue(ButtonStyleProperty, value); + } + } + + #endregion //ButtonStyle + + #region DisplayColorAndName + + public static readonly DependencyProperty DisplayColorAndNameProperty = DependencyProperty.Register("DisplayColorAndName", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(false)); + public bool DisplayColorAndName + { + get + { + return (bool)GetValue(DisplayColorAndNameProperty); + } + set + { + SetValue(DisplayColorAndNameProperty, value); + } + } + + #endregion //DisplayColorAndName + + #region ColorMode + + internal static readonly DependencyProperty ColorModeProperty = DependencyProperty.Register("ColorMode", typeof(ColorMode), typeof(ColorPickerCombo), new UIPropertyMetadata(ColorMode.ColorPalette)); + internal ColorMode ColorMode + { + get + { + return (ColorMode)GetValue(ColorModeProperty); + } + set + { + SetValue(ColorModeProperty, value); + } + } + + #endregion //ColorMode + + #region IsOpen + + public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(false, OnIsOpenChanged)); + public bool IsOpen + { + get + { + return (bool)GetValue(IsOpenProperty); + } + set + { + SetValue(IsOpenProperty, value); + } + } + + private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ColorPickerCombo colorPicker = (ColorPickerCombo)d; + if (colorPicker != null) + colorPicker.OnIsOpenChanged((bool)e.OldValue, (bool)e.NewValue); + } + + private void OnIsOpenChanged(bool oldValue, bool newValue) + { + if (newValue) + { + _initialColor = this.SelectedColor; + } + RoutedEventArgs args = new RoutedEventArgs(newValue ? OpenedEvent : ClosedEvent, this); + this.RaiseEvent(args); + } + + #endregion //IsOpen + + #region RecentColors + + internal static readonly DependencyProperty RecentColorsProperty = DependencyProperty.Register("RecentColors", typeof(ObservableCollection), typeof(ColorPickerCombo), new UIPropertyMetadata(null)); + internal ObservableCollection RecentColors + { + get + { + return (ObservableCollection)GetValue(RecentColorsProperty); + } + set + { + SetValue(RecentColorsProperty, value); + } + } + + #endregion //RecentColors + + #region RecentColorsHeader + + public static readonly DependencyProperty RecentColorsHeaderProperty = DependencyProperty.Register("RecentColorsHeader", typeof(string), typeof(ColorPickerCombo), new UIPropertyMetadata("Recent Colors")); + public string RecentColorsHeader + { + get + { + return (string)GetValue(RecentColorsHeaderProperty); + } + set + { + SetValue(RecentColorsHeaderProperty, value); + } + } + + #endregion //RecentColorsHeader + + #region SelectedColor + + public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register("SelectedColor", typeof(Color?), typeof(ColorPickerCombo), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnSelectedColorPropertyChanged))); + public Color? SelectedColor + { + get + { + return (Color?)GetValue(SelectedColorProperty); + } + set + { + SetValue(SelectedColorProperty, value); + } + } + + private static void OnSelectedColorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ColorPickerCombo colorPicker = (ColorPickerCombo)d; + if (colorPicker != null) + colorPicker.OnSelectedColorChanged((Color?)e.OldValue, (Color?)e.NewValue); + } + + private void OnSelectedColorChanged(Color? oldValue, Color? newValue) + { + SelectedColorText = GetFormatedColorString(newValue); + + RoutedPropertyChangedEventArgs args = new RoutedPropertyChangedEventArgs(oldValue, newValue); + args.RoutedEvent = ColorPickerCombo.SelectedColorChangedEvent; + RaiseEvent(args); + } + + #endregion //SelectedColor + + #region SelectedColorText + + public static readonly DependencyProperty SelectedColorTextProperty = DependencyProperty.Register("SelectedColorText", typeof(string), typeof(ColorPickerCombo), new UIPropertyMetadata("")); + public string SelectedColorText + { + get + { + return (string)GetValue(SelectedColorTextProperty); + } + protected set + { + SetValue(SelectedColorTextProperty, value); + } + } + + #endregion //SelectedColorText + + #region ShowAdvancedButton + + public static readonly DependencyProperty ShowAdvancedButtonProperty = DependencyProperty.Register("ShowAdvancedButton", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(true)); + public bool ShowAdvancedButton + { + get + { + return (bool)GetValue(ShowAdvancedButtonProperty); + } + set + { + SetValue(ShowAdvancedButtonProperty, value); + } + } + + #endregion //ShowAdvancedButton + + #region ShowAvailableColors + + public static readonly DependencyProperty ShowAvailableColorsProperty = DependencyProperty.Register("ShowAvailableColors", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(true)); + public bool ShowAvailableColors + { + get + { + return (bool)GetValue(ShowAvailableColorsProperty); + } + set + { + SetValue(ShowAvailableColorsProperty, value); + } + } + + #endregion //ShowAvailableColors + + #region ShowRecentColors + + public static readonly DependencyProperty ShowRecentColorsProperty = DependencyProperty.Register("ShowRecentColors", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(false)); + public bool ShowRecentColors + { + get + { + return (bool)GetValue(ShowRecentColorsProperty); + } + set + { + SetValue(ShowRecentColorsProperty, value); + } + } + + #endregion //DisplayRecentColors + + #region ShowStandardColors + + public static readonly DependencyProperty ShowStandardColorsProperty = DependencyProperty.Register("ShowStandardColors", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(true)); + public bool ShowStandardColors + { + get + { + return (bool)GetValue(ShowStandardColorsProperty); + } + set + { + SetValue(ShowStandardColorsProperty, value); + } + } + + #endregion //DisplayStandardColors + + #region ShowDropDownButton + + public static readonly DependencyProperty ShowDropDownButtonProperty = DependencyProperty.Register("ShowDropDownButton", typeof(bool), typeof(ColorPickerCombo), new UIPropertyMetadata(true)); + public bool ShowDropDownButton + { + get + { + return (bool)GetValue(ShowDropDownButtonProperty); + } + set + { + SetValue(ShowDropDownButtonProperty, value); + } + } + + #endregion //ShowDropDownButton + + #region StandardButtonHeader + + public static readonly DependencyProperty StandardButtonHeaderProperty = DependencyProperty.Register("StandardButtonHeader", typeof(string), typeof(ColorPickerCombo), new UIPropertyMetadata("Standard")); + public string StandardButtonHeader + { + get + { + return (string)GetValue(StandardButtonHeaderProperty); + } + set + { + SetValue(StandardButtonHeaderProperty, value); + } + } + + #endregion //StandardButtonHeader + + #region StandardColors + + internal static readonly DependencyProperty StandardColorsProperty = DependencyProperty.Register("StandardColors", typeof(ObservableCollection), typeof(ColorPickerCombo), new UIPropertyMetadata(CreateStandardColors())); + internal ObservableCollection StandardColors + { + get + { + return (ObservableCollection)GetValue(StandardColorsProperty); + } + set + { + SetValue(StandardColorsProperty, value); + } + } + + #endregion //StandardColors + + #region StandardColorsHeader + + public static readonly DependencyProperty StandardColorsHeaderProperty = DependencyProperty.Register("StandardColorsHeader", typeof(string), typeof(ColorPickerCombo), new UIPropertyMetadata("Standard Colors")); + public string StandardColorsHeader + { + get + { + return (string)GetValue(StandardColorsHeaderProperty); + } + set + { + SetValue(StandardColorsHeaderProperty, value); + } + } + + #endregion //StandardColorsHeader + + #region UsingAlphaChannel + + public static readonly DependencyProperty UsingAlphaChannelProperty = DependencyProperty.Register("UsingAlphaChannel", typeof(bool), typeof(ColorPickerCombo), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnUsingAlphaChannelPropertyChanged))); + public bool UsingAlphaChannel + { + get + { + return (bool)GetValue(UsingAlphaChannelProperty); + } + set + { + SetValue(UsingAlphaChannelProperty, value); + } + } + + private static void OnUsingAlphaChannelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ColorPickerCombo colorPicker = (ColorPickerCombo)d; + if (colorPicker != null) + colorPicker.OnUsingAlphaChannelChanged(); + } + + private void OnUsingAlphaChannelChanged() + { + SelectedColorText = GetFormatedColorString(SelectedColor); + } + + #endregion //UsingAlphaChannel + + #endregion //Properties + + #region Constructors + + static ColorPickerCombo() + { + DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPickerCombo), new FrameworkPropertyMetadata(typeof(ColorPickerCombo))); + } + + public ColorPickerCombo() + { +#if VS2008 + this.RecentColors = new ObservableCollection(); +#else + this.SetCurrentValue(ColorPickerCombo.RecentColorsProperty, new ObservableCollection()); +#endif + + Keyboard.AddKeyDownHandler(this, OnKeyDown); + Mouse.AddPreviewMouseDownOutsideCapturedElementHandler(this, OnMouseDownOutsideCapturedElement); + } + + #endregion //Constructors + + #region Base Class Overrides + + public override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + if (_availableColors != null) + _availableColors.SelectionChanged -= Color_SelectionChanged; + + _availableColors = GetTemplateChild(PART_AvailableColors) as ListBox; + if (_availableColors != null) + _availableColors.SelectionChanged += Color_SelectionChanged; + + if (_standardColors != null) + _standardColors.SelectionChanged -= Color_SelectionChanged; + + _standardColors = GetTemplateChild(PART_StandardColors) as ListBox; + if (_standardColors != null) + _standardColors.SelectionChanged += Color_SelectionChanged; + + if (_recentColors != null) + _recentColors.SelectionChanged -= Color_SelectionChanged; + + _recentColors = GetTemplateChild(PART_RecentColors) as ListBox; + if (_recentColors != null) + _recentColors.SelectionChanged += Color_SelectionChanged; + + if (_popup != null) + _popup.Opened -= Popup_Opened; + + _popup = GetTemplateChild(PART_ColorPickerPalettePopup) as Popup; + if (_popup != null) + _popup.Opened += Popup_Opened; + + _toggleButton = this.Template.FindName(PART_ColorPickerToggleButton, this) as ToggleButton; + + if (_colorModeButton != null) + _colorModeButton.Click -= new RoutedEventHandler(this.ColorModeButton_Clicked); + + _colorModeButton = this.Template.FindName(PART_ColorModeButton, this) as Button; + + if (_colorModeButton != null) + _colorModeButton.Click += new RoutedEventHandler(this.ColorModeButton_Clicked); + } + + protected override void OnMouseUp(MouseButtonEventArgs e) + { + base.OnMouseUp(e); + + // Close ColorPicker on MouseUp to prevent action of mouseUp on controls behind the ColorPicker. + if (_selectionChanged) + { + CloseColorPicker(true); + _selectionChanged = false; + } + } + + #endregion //Base Class Overrides + + #region Event Handlers + + private void OnKeyDown(object sender, KeyEventArgs e) + { + if (!IsOpen) + { + if (KeyboardUtilities.IsKeyModifyingPopupState(e)) + { + IsOpen = true; + // Focus will be on ListBoxItem in Popup_Opened(). + e.Handled = true; + } + } + else + { + if (KeyboardUtilities.IsKeyModifyingPopupState(e)) + { + CloseColorPicker(true); + e.Handled = true; + } + else if (e.Key == Key.Escape) + { + this.SelectedColor = _initialColor; + CloseColorPicker(true); + e.Handled = true; + } + } + } + + private void OnMouseDownOutsideCapturedElement(object sender, MouseButtonEventArgs e) + { + CloseColorPicker(true); + } + + private void Color_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + ListBox lb = (ListBox)sender; + + if (e.AddedItems.Count > 0) + { + var colorItem = (ColorItem)e.AddedItems[0]; + SelectedColor = colorItem.Color; + UpdateRecentColors(colorItem); + _selectionChanged = true; + lb.SelectedIndex = -1; //for now I don't care about keeping track of the selected color + } + } + + private void Popup_Opened(object sender, EventArgs e) + { + if ((_availableColors != null) && ShowAvailableColors) + FocusOnListBoxItem(_availableColors); + else if ((_standardColors != null) && ShowStandardColors) + FocusOnListBoxItem(_standardColors); + else if ((_recentColors != null) && ShowRecentColors) + FocusOnListBoxItem(_recentColors); + } + + private void FocusOnListBoxItem(ListBox listBox) + { + ListBoxItem listBoxItem = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(listBox.SelectedItem); + if ((listBoxItem == null) && (listBox.Items.Count > 0)) + listBoxItem = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromItem(listBox.Items[0]); + if (listBoxItem != null) + listBoxItem.Focus(); + } + + private void ColorModeButton_Clicked(object sender, RoutedEventArgs e) + { + this.ColorMode = (this.ColorMode == ColorMode.ColorPalette) ? ColorMode.ColorCanvas : ColorMode.ColorPalette; + } + + #endregion //Event Handlers + + #region Events + + #region SelectedColorChangedEvent + + public static readonly RoutedEvent SelectedColorChangedEvent = EventManager.RegisterRoutedEvent("SelectedColorChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler), typeof(ColorPickerCombo)); + public event RoutedPropertyChangedEventHandler SelectedColorChanged + { + add + { + AddHandler(SelectedColorChangedEvent, value); + } + remove + { + RemoveHandler(SelectedColorChangedEvent, value); + } + } + + #endregion + + #region OpenedEvent + + public static readonly RoutedEvent OpenedEvent = EventManager.RegisterRoutedEvent("OpenedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ColorPickerCombo)); + public event RoutedEventHandler Opened + { + add + { + AddHandler(OpenedEvent, value); + } + remove + { + RemoveHandler(OpenedEvent, value); + } + } + + #endregion //OpenedEvent + + #region ClosedEvent + + public static readonly RoutedEvent ClosedEvent = EventManager.RegisterRoutedEvent("ClosedEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ColorPickerCombo)); + public event RoutedEventHandler Closed + { + add + { + AddHandler(ClosedEvent, value); + } + remove + { + RemoveHandler(ClosedEvent, value); + } + } + + #endregion //ClosedEvent + + #endregion //Events + + #region Methods + + private void CloseColorPicker(bool isFocusOnColorPicker) + { + if (IsOpen) + IsOpen = false; + ReleaseMouseCapture(); + + if (isFocusOnColorPicker && (_toggleButton != null)) + _toggleButton.Focus(); + this.UpdateRecentColors(new ColorItem(SelectedColor, SelectedColorText)); + } + + private void UpdateRecentColors(ColorItem colorItem) + { + if (!RecentColors.Contains(colorItem)) + RecentColors.Add(colorItem); + + if (RecentColors.Count > 10) //don't allow more than ten, maybe make a property that can be set by the user. + RecentColors.RemoveAt(0); + } + + private string GetFormatedColorString(Color? colorToFormat) + { + if ((colorToFormat == null) || !colorToFormat.HasValue) + return string.Empty; + + return ColorUtilities.FormatColorString(colorToFormat.Value.GetColorName(), UsingAlphaChannel); + } + + private static ObservableCollection CreateStandardColors() + { + ObservableCollection _standardColors = new ObservableCollection(); + _standardColors.Add(new ColorItem(Colors.Transparent, "Transparent")); + _standardColors.Add(new ColorItem(Colors.White, "White")); + _standardColors.Add(new ColorItem(Colors.Gray, "Gray")); + _standardColors.Add(new ColorItem(Colors.Black, "Black")); + _standardColors.Add(new ColorItem(Colors.Red, "Red")); + _standardColors.Add(new ColorItem(Colors.Green, "Green")); + _standardColors.Add(new ColorItem(Colors.Blue, "Blue")); + _standardColors.Add(new ColorItem(Colors.Yellow, "Yellow")); + _standardColors.Add(new ColorItem(Colors.Orange, "Orange")); + _standardColors.Add(new ColorItem(Colors.Purple, "Purple")); + return _standardColors; + } + + private static ObservableCollection CreateAvailableColors() + { + ObservableCollection _standardColors = new ObservableCollection(); + + foreach (var item in ColorUtilities.KnownColors) + { + if (!String.Equals(item.Key, "Transparent")) + { + var colorItem = new ColorItem(item.Value, item.Key); + if (!_standardColors.Contains(colorItem)) + _standardColors.Add(colorItem); + } + } + + return _standardColors; + } + + #endregion //Methods + } +} diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorSorter.cs b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorSorter.cs new file mode 100644 index 000000000..fa33faa55 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Implementation/ColorSorter.cs @@ -0,0 +1,77 @@ +/************************************************************************************* + + Extended WPF Toolkit + + Copyright (C) 2007-2013 Xceed Software Inc. + + This program is provided to you under the terms of the Microsoft Public + License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license + + For more features, controls, and fast professional support, + pick up the Plus Edition at http://xceed.com/wpf_toolkit + + Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids + + ***********************************************************************************/ + +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Media; + +namespace Tango.ColorPicker +{ + internal class ColorSorter : IComparer + { + public int Compare( object firstItem, object secondItem ) + { + if( firstItem == null || secondItem == null ) + return -1; + + ColorItem colorItem1 = ( ColorItem )firstItem; + ColorItem colorItem2 = ( ColorItem )secondItem; + + if( (colorItem1.Color == null) || !colorItem1.Color.HasValue || + (colorItem2.Color == null) || !colorItem2.Color.HasValue ) + return -1; + + System.Drawing.Color drawingColor1 = System.Drawing.Color.FromArgb( colorItem1.Color.Value.A, colorItem1.Color.Value.R, colorItem1.Color.Value.G, colorItem1.Color.Value.B ); + System.Drawing.Color drawingColor2 = System.Drawing.Color.FromArgb( colorItem2.Color.Value.A, colorItem2.Color.Value.R, colorItem2.Color.Value.G, colorItem2.Color.Value.B ); + + // Compare Hue + double hueColor1 = Math.Round( ( double )drawingColor1.GetHue(), 3 ); + double hueColor2 = Math.Round( ( double )drawingColor2.GetHue(), 3 ); + + if( hueColor1 > hueColor2 ) + return 1; + else if( hueColor1 < hueColor2 ) + return -1; + else + { + // Hue is equal, compare Saturation + double satColor1 = Math.Round( ( double )drawingColor1.GetSaturation(), 3 ); + double satColor2 = Math.Round( ( double )drawingColor2.GetSaturation(), 3 ); + + if( satColor1 > satColor2 ) + return 1; + else if( satColor1 < satColor2 ) + return -1; + else + { + // Saturation is equal, compare Brightness + double brightColor1 = Math.Round( ( double )drawingColor1.GetBrightness(), 3 ); + double brightColor2 = Math.Round( ( double )drawingColor2.GetBrightness(), 3 ); + + if( brightColor1 > brightColor2 ) + return 1; + else if( brightColor1 < brightColor2 ) + return -1; + } + } + + return 0; + } + } +} diff --git a/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Themes/Generic.xaml b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Themes/Generic.xaml new file mode 100644 index 000000000..996da3308 --- /dev/null +++ b/Software/Visual_Studio/Tango.ColorPicker/ColorPicker/Themes/Generic.xaml @@ -0,0 +1,356 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +