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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
|
/***************** 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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Data;
//Tango.BrushPicker
namespace Tango.BrushPicker
{
[TemplatePart(Name = PART_CurrentColor, Type = typeof(TextBox))]
public class BrushPicker : Control
{
internal const string PART_CurrentColor = "PART_CurrentColor";
//internal bool _GradientStopSetInternally = false;
internal bool _HSBSetInternally = false;
internal bool _RGBSetInternally = false;
internal bool _BrushSetInternally = false;
internal bool _BrushTypeSetInternally = false;
internal bool _UpdateBrush = true;
internal TextBox CurrentColorTextBox
{
get;
private set;
}
static BrushPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BrushPicker), new FrameworkPropertyMetadata(typeof(BrushPicker)));
}
public static RoutedCommand RemoveGradientStop = new RoutedCommand();
public static RoutedCommand ReverseGradientStop = new RoutedCommand();
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
CurrentColorTextBox = GetTemplateChild(PART_CurrentColor) as TextBox;
if (CurrentColorTextBox != null)
{
CurrentColorTextBox.PreviewKeyDown += CurrentColorTextBox_PreviewKeyDown;
}
this.CommandBindings.Add(new CommandBinding(BrushPicker.RemoveGradientStop, RemoveGradientStop_Executed));
this.CommandBindings.Add(new CommandBinding(BrushPicker.ReverseGradientStop, ReverseGradientStop_Executed));
BrushType = BrushTypes.Solid;
}
void CurrentColorTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression be = CurrentColorTextBox.GetBindingExpression(TextBox.TextProperty);
if (be != null)
{
be.UpdateSource();
}
}
}
private void RemoveGradientStop_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (this.Gradients != null && this.Gradients.Count > 2)
{
this.Gradients.Remove(this.SelectedGradient);
this.SetBrush();
}
}
private void ReverseGradientStop_Executed(object sender, ExecutedRoutedEventArgs e)
{
this._UpdateBrush = false;
this._BrushSetInternally = true;
foreach (GradientStop gs in Gradients)
{
gs.Offset = 1.0 - gs.Offset;
}
this._UpdateBrush = true;
this._BrushSetInternally = false;
this.SetBrush();
}
void InitTransform()
{
if (this.Brush.Transform == null || this.Brush.Transform.Value.IsIdentity)
{
this._BrushSetInternally = true;
TransformGroup _tg = new TransformGroup();
_tg.Children.Add(new RotateTransform());
_tg.Children.Add(new ScaleTransform());
_tg.Children.Add(new SkewTransform());
_tg.Children.Add(new TranslateTransform());
this.Brush.Transform = _tg;
this._BrushSetInternally = false;
}
}
#region Private Properties
double StartX
{
get { return (double)GetValue(StartXProperty); }
set { SetValue(StartXProperty, value); }
}
static readonly DependencyProperty StartXProperty =
DependencyProperty.Register("StartX", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(StartXChanged)));
static void StartXChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is LinearGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as LinearGradientBrush).StartPoint = new Point((double)args.NewValue, (cp.Brush as LinearGradientBrush).StartPoint.Y);
cp._BrushSetInternally = false;
}
}
double StartY
{
get { return (double)GetValue(StartYProperty); }
set { SetValue(StartYProperty, value); }
}
static readonly DependencyProperty StartYProperty =
DependencyProperty.Register("StartY", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.0, new PropertyChangedCallback(StartYChanged)));
static void StartYChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is LinearGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as LinearGradientBrush).StartPoint = new Point((cp.Brush as LinearGradientBrush).StartPoint.X, (double)args.NewValue);
cp._BrushSetInternally = false;
}
}
double EndX
{
get { return (double)GetValue(EndXProperty); }
set { SetValue(EndXProperty, value); }
}
static readonly DependencyProperty EndXProperty =
DependencyProperty.Register("EndX", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(EndXChanged)));
static void EndXChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is LinearGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as LinearGradientBrush).EndPoint = new Point((double)args.NewValue, (cp.Brush as LinearGradientBrush).EndPoint.Y);
cp._BrushSetInternally = false;
}
}
double EndY
{
get { return (double)GetValue(EndYProperty); }
set { SetValue(EndYProperty, value); }
}
static readonly DependencyProperty EndYProperty =
DependencyProperty.Register("EndY", typeof(double), typeof(BrushPicker), new PropertyMetadata(1.0, new PropertyChangedCallback(EndYChanged)));
static void EndYChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is LinearGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as LinearGradientBrush).EndPoint = new Point((cp.Brush as LinearGradientBrush).EndPoint.X, (double)args.NewValue);
cp._BrushSetInternally = false;
}
}
double GradientOriginX
{
get { return (double)GetValue(GradientOriginXProperty); }
set { SetValue(GradientOriginXProperty, value); }
}
static readonly DependencyProperty GradientOriginXProperty =
DependencyProperty.Register("GradientOriginX", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(GradientOriginXChanged)));
static void GradientOriginXChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is RadialGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as RadialGradientBrush).GradientOrigin = new Point((double)args.NewValue, (cp.Brush as RadialGradientBrush).GradientOrigin.Y);
cp._BrushSetInternally = false;
}
}
double GradientOriginY
{
get { return (double)GetValue(GradientOriginYProperty); }
set { SetValue(GradientOriginYProperty, value); }
}
static readonly DependencyProperty GradientOriginYProperty =
DependencyProperty.Register("GradientOriginY", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(GradientOriginYChanged)));
static void GradientOriginYChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is RadialGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as RadialGradientBrush).GradientOrigin = new Point((cp.Brush as RadialGradientBrush).GradientOrigin.X, (double)args.NewValue);
cp._BrushSetInternally = false;
}
}
double CenterX
{
get { return (double)GetValue(CenterXProperty); }
set { SetValue(CenterXProperty, value); }
}
static readonly DependencyProperty CenterXProperty =
DependencyProperty.Register("CenterX", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(CenterXChanged)));
static void CenterXChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is RadialGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as RadialGradientBrush).Center = new Point((double)args.NewValue, (cp.Brush as RadialGradientBrush).Center.Y);
cp._BrushSetInternally = false;
}
}
double CenterY
{
get { return (double)GetValue(CenterYProperty); }
set { SetValue(CenterYProperty, value); }
}
static readonly DependencyProperty CenterYProperty =
DependencyProperty.Register("CenterY", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(CenterYChanged)));
static void CenterYChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is RadialGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as RadialGradientBrush).Center = new Point((cp.Brush as RadialGradientBrush).Center.X, (double)args.NewValue);
cp._BrushSetInternally = false;
}
}
double RadiusX
{
get { return (double)GetValue(RadiusXProperty); }
set { SetValue(RadiusXProperty, value); }
}
static readonly DependencyProperty RadiusXProperty =
DependencyProperty.Register("RadiusX", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(RadiusXChanged)));
static void RadiusXChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is RadialGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as RadialGradientBrush).RadiusX = (double)args.NewValue;
cp._BrushSetInternally = false;
}
}
double RadiusY
{
get { return (double)GetValue(RadiusYProperty); }
set { SetValue(RadiusYProperty, value); }
}
static readonly DependencyProperty RadiusYProperty =
DependencyProperty.Register("RadiusY", typeof(double), typeof(BrushPicker), new PropertyMetadata(0.5, new PropertyChangedCallback(RadiusYChanged)));
static void RadiusYChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is RadialGradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as RadialGradientBrush).RadiusY = (double)args.NewValue;
cp._BrushSetInternally = false;
}
}
double BrushOpacity
{
get { return (double)GetValue(BrushOpacityProperty); }
set { SetValue(BrushOpacityProperty, value); }
}
static readonly DependencyProperty BrushOpacityProperty =
DependencyProperty.Register("BrushOpacity", typeof(double), typeof(BrushPicker), new PropertyMetadata(1.0));
//static void BrushOpacityChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
//{
// ColorBox cp = property as ColorBox;
// cp._BrushSetInternally = true;
// cp.Brush.Opacity = (double)args.NewValue;
// cp._BrushSetInternally = false;
//}
GradientSpreadMethod SpreadMethod
{
get { return (GradientSpreadMethod)GetValue(SpreadMethodProperty); }
set { SetValue(SpreadMethodProperty, value); }
}
static readonly DependencyProperty SpreadMethodProperty =
DependencyProperty.Register("SpreadMethod", typeof(GradientSpreadMethod), typeof(BrushPicker), new PropertyMetadata(GradientSpreadMethod.Pad, new PropertyChangedCallback(SpreadMethodChanged)));
static void SpreadMethodChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is GradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as GradientBrush).SpreadMethod = (GradientSpreadMethod)args.NewValue;
cp._BrushSetInternally = false;
}
}
BrushMappingMode MappingMode
{
get { return (BrushMappingMode)GetValue(MappingModeProperty); }
set { SetValue(MappingModeProperty, value); }
}
static readonly DependencyProperty MappingModeProperty =
DependencyProperty.Register("MappingMode", typeof(BrushMappingMode), typeof(BrushPicker), new PropertyMetadata(BrushMappingMode.RelativeToBoundingBox, new PropertyChangedCallback(MappingModeChanged)));
static void MappingModeChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker cp = property as BrushPicker;
if (cp.Brush is GradientBrush)
{
cp._BrushSetInternally = true;
(cp.Brush as GradientBrush).MappingMode = (BrushMappingMode)args.NewValue;
cp._BrushSetInternally = false;
}
}
#endregion
#region Internal Properties
internal ObservableCollection<GradientStop> Gradients
{
get { return (ObservableCollection<GradientStop>)GetValue(GradientsProperty); }
set { SetValue(GradientsProperty, value); }
}
internal static readonly DependencyProperty GradientsProperty =
DependencyProperty.Register("Gradients", typeof(ObservableCollection<GradientStop>), typeof(BrushPicker));
internal GradientStop SelectedGradient
{
get { return (GradientStop)GetValue(SelectedGradientProperty); }
set { SetValue(SelectedGradientProperty, value); }
}
internal static readonly DependencyProperty SelectedGradientProperty =
DependencyProperty.Register("SelectedGradient", typeof(GradientStop), typeof(BrushPicker));
internal BrushTypes BrushType
{
get { return (BrushTypes)GetValue(BrushTypeProperty); }
set { SetValue(BrushTypeProperty, value); }
}
internal static readonly DependencyProperty BrushTypeProperty =
DependencyProperty.Register("BrushType", typeof(BrushTypes), typeof(BrushPicker),
new FrameworkPropertyMetadata(BrushTypes.None, new PropertyChangedCallback(BrushTypeChanged)));
static void BrushTypeChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker c = property as BrushPicker;
if (!c._BrushTypeSetInternally)
{
if (c.Gradients == null)
{
c.Gradients = new ObservableCollection<GradientStop>();
c.Gradients.Add(new GradientStop(Colors.Black, 0));
c.Gradients.Add(new GradientStop(Colors.White, 1));
}
c.SetBrush();
}
}
#endregion
#region Public Properties
public Visibility BrushTypeVisibility
{
get { return (Visibility)GetValue(BrushTypeVisibilityProperty); }
set { SetValue(BrushTypeVisibilityProperty, value); }
}
public static readonly DependencyProperty BrushTypeVisibilityProperty =
DependencyProperty.Register("BrushTypeVisibility", typeof(Visibility), typeof(BrushPicker), new PropertyMetadata(Visibility.Visible));
public IEnumerable<Enum> SpreadMethodTypes
{
get
{
GradientSpreadMethod temp = GradientSpreadMethod.Pad | GradientSpreadMethod.Reflect | GradientSpreadMethod.Repeat;
foreach (Enum value in Enum.GetValues(temp.GetType()))
if (temp.HasFlag(value))
yield return value;
}
}
public IEnumerable<Enum> MappingModeTypes
{
get
{
BrushMappingMode temp = BrushMappingMode.Absolute | BrushMappingMode.RelativeToBoundingBox;
foreach (Enum value in Enum.GetValues(temp.GetType()))
if (temp.HasFlag(value))
yield return value;
}
}
public IEnumerable<Enum> AvailableBrushTypes
{
get
{
List<Enum> brushTypes = new List<Enum>();
brushTypes.Add(BrushTypes.Solid);
brushTypes.Add(BrushTypes.Linear);
return brushTypes;
}
}
public Brush Brush
{
get { return (Brush)GetValue(BrushProperty); }
set { SetValue(BrushProperty, value); }
}
public static readonly DependencyProperty BrushProperty =
DependencyProperty.Register("Brush", typeof(Brush), typeof(BrushPicker)
, new FrameworkPropertyMetadata(null, new PropertyChangedCallback(BrushChanged)));
static void BrushChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
BrushPicker c = property as BrushPicker;
Brush brush = args.NewValue as Brush;
if (!c._BrushSetInternally)
{
c._BrushTypeSetInternally = true;
if (brush == null)
{
c.BrushType = BrushTypes.None;
}
else if (brush is SolidColorBrush)
{
c.BrushType = BrushTypes.Solid;
c.Color = (brush as SolidColorBrush).Color;
}
else if (brush is LinearGradientBrush)
{
LinearGradientBrush lgb = brush as LinearGradientBrush;
//c.Opacity = lgb.Opacity;
c.StartX = lgb.StartPoint.X;
c.StartY = lgb.StartPoint.Y;
c.EndX = lgb.EndPoint.X;
c.EndY = lgb.EndPoint.Y;
c.MappingMode = lgb.MappingMode;
c.SpreadMethod = lgb.SpreadMethod;
c.Gradients = new ObservableCollection<GradientStop>(lgb.GradientStops);
c.BrushType = BrushTypes.Linear;
//c.Color = lgb.GradientStops.OrderBy(x => x.Offset).Last().Color;
//c.SelectedGradient = lgb.GradientStops.OrderBy(x => x.Offset).Last();
}
else
{
RadialGradientBrush rgb = brush as RadialGradientBrush;
c.GradientOriginX = rgb.GradientOrigin.X;
c.GradientOriginY = rgb.GradientOrigin.Y;
c.RadiusX = rgb.RadiusX;
c.RadiusY = rgb.RadiusY;
c.CenterX = rgb.Center.X;
c.CenterY = rgb.Center.Y;
c.MappingMode = rgb.MappingMode;
c.SpreadMethod = rgb.SpreadMethod;
c.Gradients = new ObservableCollection<GradientStop>(rgb.GradientStops);
c.BrushType = BrushTypes.Radial;
//c.Color = rgb.GradientStops.OrderBy(x => x.Offset).Last().Color;
//c.SelectedGradient = rgb.GradientStops.OrderBy(x => x.Offset).Last();
}
c._BrushTypeSetInternally = false;
}
}
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register("Color", typeof(Color), typeof(BrushPicker), new UIPropertyMetadata(Colors.Black, OnColorChanged));
public static void OnColorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
BrushPicker c = (BrushPicker)o;
if (e.NewValue is Color)
{
Color color = (Color)e.NewValue;
if (!c._HSBSetInternally)
{
// update HSB value based on new value of color
double H = 0;
double S = 0;
double B = 0;
ColorHelper.HSBFromColor(color, ref H, ref S, ref B);
c._HSBSetInternally = true;
c.Alpha = (double)(color.A / 255d);
c.Hue = H;
c.Saturation = S;
c.Brightness = B;
c._HSBSetInternally = false;
}
if (!c._RGBSetInternally)
{
// update RGB value based on new value of color
c._RGBSetInternally = true;
c.A = color.A;
c.R = color.R;
c.G = color.G;
c.B = color.B;
if (e.OldValue != e.NewValue)
{
c.RaiseColorChangedEvent((Color)e.NewValue);
}
c._RGBSetInternally = false;
}
}
}
#endregion
#region Color Specific Properties
private double Hue
{
get { return (double)GetValue(HueProperty); }
set { SetValue(HueProperty, value); }
}
private static readonly DependencyProperty HueProperty =
DependencyProperty.Register("Hue", typeof(double), typeof(BrushPicker),
new FrameworkPropertyMetadata(1.0, new PropertyChangedCallback(UpdateColorHSB), new CoerceValueCallback(HueCoerce)));
private static object HueCoerce(DependencyObject d, object Hue)
{
double v = (double)Hue;
if (v < 0) return 0.0;
if (v > 1) return 1.0;
return v;
}
private double Brightness
{
get { return (double)GetValue(BrightnessProperty); }
set { SetValue(BrightnessProperty, value); }
}
private static readonly DependencyProperty BrightnessProperty =
DependencyProperty.Register("Brightness", typeof(double), typeof(BrushPicker),
new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(UpdateColorHSB), new CoerceValueCallback(BrightnessCoerce)));
private 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;
}
private double Saturation
{
get { return (double)GetValue(SaturationProperty); }
set { SetValue(SaturationProperty, value); }
}
private static readonly DependencyProperty SaturationProperty =
DependencyProperty.Register("Saturation", typeof(double), typeof(BrushPicker),
new FrameworkPropertyMetadata(0.0, new PropertyChangedCallback(UpdateColorHSB), new CoerceValueCallback(SaturationCoerce)));
private static object SaturationCoerce(DependencyObject d, object Saturation)
{
double v = (double)Saturation;
if (v < 0) return 0.0;
if (v > 1) return 1.0;
return v;
}
private double Alpha
{
get { return (double)GetValue(AlphaProperty); }
set { SetValue(AlphaProperty, value); }
}
private static readonly DependencyProperty AlphaProperty =
DependencyProperty.Register("Alpha", typeof(double), typeof(BrushPicker),
new FrameworkPropertyMetadata(1.0, new PropertyChangedCallback(UpdateColorHSB), new CoerceValueCallback(AlphaCoerce)));
private static object AlphaCoerce(DependencyObject d, object Alpha)
{
double v = (double)Alpha;
if (v < 0) return 0.0;
if (v > 1) return 1.0;
return v;
}
private int A
{
get { return (int)GetValue(AProperty); }
set { SetValue(AProperty, value); }
}
private static readonly DependencyProperty AProperty =
DependencyProperty.Register("A", typeof(int), typeof(BrushPicker),
new FrameworkPropertyMetadata(255, new PropertyChangedCallback(UpdateColorRGB), new CoerceValueCallback(RGBCoerce)));
private int R
{
get { return (int)GetValue(RProperty); }
set { SetValue(RProperty, value); }
}
private static readonly DependencyProperty RProperty =
DependencyProperty.Register("R", typeof(int), typeof(BrushPicker),
new FrameworkPropertyMetadata(default(int), new PropertyChangedCallback(UpdateColorRGB), new CoerceValueCallback(RGBCoerce)));
private int G
{
get { return (int)GetValue(GProperty); }
set { SetValue(GProperty, value); }
}
private static readonly DependencyProperty GProperty =
DependencyProperty.Register("G", typeof(int), typeof(BrushPicker),
new FrameworkPropertyMetadata(default(int), new PropertyChangedCallback(UpdateColorRGB), new CoerceValueCallback(RGBCoerce)));
private int B
{
get { return (int)GetValue(BProperty); }
set { SetValue(BProperty, value); }
}
private static readonly DependencyProperty BProperty =
DependencyProperty.Register("B", typeof(int), typeof(BrushPicker),
new FrameworkPropertyMetadata(default(int), new PropertyChangedCallback(UpdateColorRGB), new CoerceValueCallback(RGBCoerce)));
private static object RGBCoerce(DependencyObject d, object value)
{
int v = (int)value;
if (v < 0) return 0;
if (v > 255) return 255;
return v;
}
#endregion
/// <summary>
/// Shared property changed callback to update the Color property
/// </summary>
public static void UpdateColorHSB(object o, DependencyPropertyChangedEventArgs e)
{
BrushPicker c = (BrushPicker)o;
Color n = ColorHelper.ColorFromAHSB(c.Alpha, c.Hue, c.Saturation, c.Brightness);
c._HSBSetInternally = true;
c.Color = n;
if (c.SelectedGradient != null)
c.SelectedGradient.Color = n;
c.SetBrush();
c._HSBSetInternally = false;
}
/// <summary>
/// Shared property changed callback to update the Color property
/// </summary>
public static void UpdateColorRGB(object o, DependencyPropertyChangedEventArgs e)
{
BrushPicker c = (BrushPicker)o;
Color n = Color.FromArgb((byte)c.A, (byte)c.R, (byte)c.G, (byte)c.B);
c._RGBSetInternally = true;
c.Color = n;
if (c.SelectedGradient != null)
c.SelectedGradient.Color = n;
c.SetBrush();
c._RGBSetInternally = false;
}
#region ColorChanged Event
public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs e);
public static readonly RoutedEvent ColorChangedEvent =
EventManager.RegisterRoutedEvent("ColorChanged", RoutingStrategy.Bubble, typeof(ColorChangedEventHandler), typeof(BrushPicker));
public event ColorChangedEventHandler ColorChanged
{
add { AddHandler(ColorChangedEvent, value); }
remove { RemoveHandler(ColorChangedEvent, value); }
}
void RaiseColorChangedEvent(Color color)
{
ColorChangedEventArgs newEventArgs = new ColorChangedEventArgs(BrushPicker.ColorChangedEvent, color);
RaiseEvent(newEventArgs);
}
#endregion
internal void SetBrush()
{
if (!_UpdateBrush)
return;
this._BrushSetInternally = true;
// retain old opacity
double opacity = 1;
TransformGroup tempTG = null;
if (this.Brush != null)
{
opacity = this.Brush.Opacity;
tempTG = Brush.Transform as TransformGroup;
}
switch (BrushType)
{
case BrushTypes.None: Brush = null; break;
case BrushTypes.Solid:
Brush = new SolidColorBrush(this.Color);
break;
case BrushTypes.Linear:
var brush = new LinearGradientBrush();
foreach (GradientStop g in Gradients)
{
brush.GradientStops.Add(new GradientStop(g.Color, g.Offset));
}
brush.StartPoint = new Point(this.StartX, this.StartY);
brush.EndPoint = new Point(this.EndX, this.EndY);
brush.MappingMode = this.MappingMode;
brush.SpreadMethod = this.SpreadMethod;
Brush = brush;
break;
case BrushTypes.Radial:
var brush1 = new RadialGradientBrush();
foreach (GradientStop g in Gradients)
{
brush1.GradientStops.Add(new GradientStop(g.Color, g.Offset));
}
brush1.GradientOrigin = new Point(this.GradientOriginX, this.GradientOriginY);
brush1.Center = new Point(this.CenterX, this.CenterY);
brush1.RadiusX = this.RadiusX;
brush1.RadiusY = this.RadiusY;
brush1.MappingMode = this.MappingMode;
brush1.SpreadMethod = this.SpreadMethod;
Brush = brush1;
break;
}
if (this.BrushType != BrushTypes.None)
{
this.Brush.Opacity = opacity; // retain old opacity
if (tempTG != null)
this.Brush.Transform = tempTG;
}
this._BrushSetInternally = false;
}
}
}
|