Hi,
I have a Custom Color Picker control, I'm able to changed the color of the selected object.
But after close the control and launch, the Position of the selected control in ColorPicker is not retaining.
it is setting to default position only.
Is there any way to set the position on the CustomColorPicker control based on the color.
here is the code of the custom Color picker control.
What I have tried:
public partial class ColorPickerControl : UserControl
{
#region Properties
private byte[] _pixels;
public event CurrentColorChanged SelectedColorChanged;
public Color CurrentColor = Colors.Purple;
public SolidColorBrush SelectedCurrentColor
{
get { return (SolidColorBrush)GetValue(SelectedCurrentColorProperty); }
set { SetValue(SelectedCurrentColorProperty, value); }
}
public static DependencyProperty SelectedCurrentColorProperty = DependencyProperty.Register("SelectedCurrentColor", typeof(SolidColorBrush), typeof(ColorPickerControl), new PropertyMetadata(Brushes.White));
#endregion Properties
public ColorPickerControl()
{
DataContext = this;
InitializeComponent();
brightnessSlider.ValueChanged += BrightnessSliderValueChanged;
_markerTransform.X = 99;
_markerTransform.Y = -20;
colorMarker.RenderTransform = _markerTransform;
colorMarker.RenderTransformOrigin = new Point(1, 1);
brightnessSlider.Value = 0.5;
}
#region Color Picker Members
public readonly TranslateTransform _markerTransform = new TranslateTransform();
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
GetSelectedColor(e);
}
private void Image_PreviewTouchDown(object sender, TouchEventArgs e)
{
GetSelectedColor(e);
}
private void Image_PreviewStylusDown(object sender, StylusDownEventArgs e)
{
GetSelectedColor(e);
}
private void GetSelectedColor(RoutedEventArgs sender)
{
try
{
var cb = new CroppedBitmap((BitmapSource)(((Image)sender.Source).Source), new Int32Rect((int)Mouse.GetPosition(sender.Source as Image).X, (int)Mouse.GetPosition(sender.Source as Image).Y, 1, 1));
_pixels = new byte[4];
try
{
cb.CopyPixels(_pixels, 4, 0);
UpdateCurrentColor();
SelectedColorChanged(sender, new RoutedEventArgs());
UpdateMarkerPosition();
}
catch
{
}
}
catch (Exception)
{
}
}
private void UpdateCurrentColor()
{
CurrentColor = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
brightnessSlider.Value = 0.5;
SelectedCurrentColor = new SolidColorBrush(CurrentColor);
}
private void UpdateMarkerPosition()
{
_markerTransform.X = Mouse.GetPosition(borderColorChart).X - (borderColorChart.ActualWidth / 2);
_markerTransform.Y = Mouse.GetPosition(borderColorChart).Y - (borderColorChart.ActualHeight / 2);
}
private void BrightnessSliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (_pixels == null)
{
_pixels = new byte[3];
_pixels[2] = CurrentColor.R;
_pixels[1] = CurrentColor.G;
_pixels[0] = CurrentColor.B;
}
var nc = Color.FromRgb(_pixels[2], _pixels[1], _pixels[0]);
var f = (float)e.NewValue;
float r, g, b;
const float a = 1;
if (f >= 0.5f)
{
r = nc.ScR + (1 - nc.ScR) * (f - 0.5f) * 2;
g = nc.ScG + (1 - nc.ScG) * (f - 0.5f) * 2;
b = nc.ScB + (1 - nc.ScB) * (f - 0.5f) * 2;
}
else
{
r = nc.ScR * f * 2;
g = nc.ScG * f * 2;
b = nc.ScB * f * 2;
}
CurrentColor = Color.FromScRgb(a, r, g, b);
SelectedCurrentColor = new SolidColorBrush(CurrentColor);
if (brightnessSlider.IsLoaded)
SelectedColorChanged(sender, e);
}
private void Image_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
if (e.Source.GetType().Equals(typeof(Image)))
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)Mouse.GetPosition(e.Source as Image).X, (int)Mouse.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
try
{
cb.CopyPixels(_pixels, 4, 0);
UpdateMarkerPosition();
UpdateCurrentColor();
SelectedColorChanged(sender, e);
Mouse.Synchronize();
}
catch
{
}
}
}
}
#endregion Color Picker Members
private void Image_PreviewStylusMove(object sender, StylusEventArgs e)
{
if (Stylus.GetIsPressAndHoldEnabled(sender as Image))
{
if (e.Source.GetType().Equals(typeof(Image)))
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)e.GetPosition(e.Source as Image).X, (int)e.GetPosition(e.Source as Image).Y, 1, 1));
_pixels = new byte[4];
try
{
cb.CopyPixels(_pixels, 4, 0);
UpdateMarkerPosition();
UpdateCurrentColor();
SelectedColorChanged(sender, e);
}
catch
{
}
}
}
}
private void Image_PreviewTouchMove(object sender, TouchEventArgs e)
{
if (e.Source.GetType().Equals(typeof(Image)))
{
var cb = new CroppedBitmap((BitmapSource)(((Image)e.Source).Source), new Int32Rect((int)e.GetTouchPoint(e.Source as Image).Position.X, (int)e.GetTouchPoint(e.Source as Image).Position.Y, 1, 1));
_pixels = new byte[4];
try
{
cb.CopyPixels(_pixels, 4, 0);
UpdateMarkerPosition();
UpdateCurrentColor();
SelectedColorChanged(sender, e);
}
catch
{
}
}
}
private void Image_PreviewTouchUp(object sender, TouchEventArgs e)
{
GetSelectedColor(e);
}
private void Image_PreviewStylusUp(object sender, StylusEventArgs e)
{
GetSelectedColor(e);
}
private void Image_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
GetSelectedColor(e);
}
}