1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
/***************** NCore Softwares Pvt. Ltd., India **************************
ColorBox
Copyright (C) 2013 NCore Softwares Pvt. Ltd.
This program is provided to you under the terms of the Microsoft Public
License (Ms-PL) as published at http://ColorBox.codeplex.com/license
***********************************************************************************/
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace Tango.BrushPicker
{
class SaturationBrightnessSelector : BaseSelector
{
public Thickness OffsetPadding
{
get { return (Thickness)GetValue(OffsetPaddingProperty); }
set { SetValue(OffsetPaddingProperty, value); }
}
public static readonly DependencyProperty OffsetPaddingProperty =
DependencyProperty.Register("OffsetPadding", typeof(Thickness), typeof(SaturationBrightnessSelector), new UIPropertyMetadata(new Thickness(0.0)));
public double Hue
{
private get { return (double)GetValue(HueProperty); }
set { SetValue(HueProperty, value); }
}
public static readonly DependencyProperty HueProperty =
DependencyProperty.Register("Hue", typeof(double), typeof(SaturationBrightnessSelector), new
FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(HueChanged)));
public static void HueChanged(object o, DependencyPropertyChangedEventArgs e)
{
SaturationBrightnessSelector h = (SaturationBrightnessSelector)o;
h.SetColor();
}
public double SaturationOffset
{
get { return (double)GetValue(SaturationOffsetProperty); }
set { SetValue(SaturationOffsetProperty, value); }
}
public static readonly DependencyProperty SaturationOffsetProperty =
DependencyProperty.Register("SaturationOffset", typeof(double), typeof(SaturationBrightnessSelector), new UIPropertyMetadata(0.0));
public double Saturation
{
get { return (double)GetValue(SaturationProperty); }
set { SetValue(SaturationProperty, value); }
}
public static readonly DependencyProperty SaturationProperty =
DependencyProperty.Register("Saturation", typeof(double), typeof(SaturationBrightnessSelector),
new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(SaturationChanged), new CoerceValueCallback(SaturationCoerce)));
public static void SaturationChanged(object o, DependencyPropertyChangedEventArgs e)
{
SaturationBrightnessSelector h = (SaturationBrightnessSelector)o;
h.SetSaturationOffset();
}
public static object SaturationCoerce(DependencyObject d, object Brightness)
{
double v = (double)Brightness;
if (v < 0) return 0.0;
if (v > 1) return 1.0;
return v;
}
public double BrightnessOffset
{
get { return (double)GetValue(BrightnessOffsetProperty); }
set { SetValue(BrightnessOffsetProperty, value); }
}
public static readonly DependencyProperty BrightnessOffsetProperty =
DependencyProperty.Register("BrightnessOffset", typeof(double), typeof(SaturationBrightnessSelector), new UIPropertyMetadata(0.0));
public double Brightness
{
get { return (double)GetValue(BrightnessProperty); }
set { SetValue(BrightnessProperty, value); }
}
public static readonly DependencyProperty BrightnessProperty =
DependencyProperty.Register("Brightness", typeof(double), typeof(SaturationBrightnessSelector),
new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(BrightnessChanged), new CoerceValueCallback(BrightnessCoerce)));
public static void BrightnessChanged(object o, DependencyPropertyChangedEventArgs e)
{
SaturationBrightnessSelector h = (SaturationBrightnessSelector)o;
h.SetBrightnessOffset();
}
public static object BrightnessCoerce(DependencyObject d, object Brightness)
{
double v = (double)Brightness;
if (v < 0) return 0.0;
if (v > 1) return 1.0;
return v;
}
protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point p = e.GetPosition(this);
Saturation = (p.X / (this.ActualWidth - OffsetPadding.Right));
Brightness = (((this.ActualHeight - OffsetPadding.Bottom) - p.Y) / (this.ActualHeight - OffsetPadding.Bottom));
SetColor();
}
base.OnMouseMove(e);
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
Point p = e.GetPosition(this);
Saturation = (p.X / (this.ActualWidth - OffsetPadding.Right));
Brightness = (((this.ActualHeight - OffsetPadding.Bottom) - p.Y) / (this.ActualHeight - OffsetPadding.Bottom));
SetColor();
Mouse.Capture(this);
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
this.ReleaseMouseCapture();
base.OnMouseUp(e);
}
protected override void OnRender(DrawingContext dc)
{
LinearGradientBrush h = new LinearGradientBrush();
h.StartPoint = new Point(0, 0);
h.EndPoint = new Point(1, 0);
h.GradientStops.Add(new GradientStop(Colors.White, 0.00));
h.GradientStops.Add(new GradientStop(ColorHelper.ColorFromHSB(Hue, 1, 1), 1.0));
dc.DrawRectangle(h, null, new Rect(0, 0, ActualWidth, ActualHeight));
LinearGradientBrush v = new LinearGradientBrush();
v.StartPoint = new Point(0, 0);
v.EndPoint = new Point(0, 1);
v.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0, 0, 0), 1.00));
v.GradientStops.Add(new GradientStop(Color.FromArgb(0x80, 0, 0, 0), 0.50));
v.GradientStops.Add(new GradientStop(Color.FromArgb(0x00, 0, 0, 0), 0.00));
dc.DrawRectangle(v, null, new Rect(0, 0, ActualWidth, ActualHeight));
SetSaturationOffset();
SetBrightnessOffset();
}
private void SetSaturationOffset()
{
SaturationOffset = OffsetPadding.Left + ((ActualWidth - (OffsetPadding.Right + OffsetPadding.Left)) * Saturation);
}
private void SetBrightnessOffset()
{
BrightnessOffset = OffsetPadding.Top + ((ActualHeight - (OffsetPadding.Bottom + OffsetPadding.Top)) - ((ActualHeight - (OffsetPadding.Bottom + OffsetPadding.Top)) * Brightness));
}
public void SetColor()
{
Color = ColorHelper.ColorFromHSB(Hue, Saturation, Brightness);
//Brush = new SolidColorBrush(Color);
}
}
}
|