aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BrushPicker/Implementation/AlphaSelector.cs
blob: 8a7f097963b3154010c10e31b6347438a3ceb5d7 (plain)
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
using System;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Tango.BrushPicker
{
    class AlphaSelector : BaseSelector
    {
        public double Alpha
        {
            get { return (double)GetValue(AlphaProperty); }
            set { SetValue(AlphaProperty, value); }
        }
        public static readonly DependencyProperty AlphaProperty =
            DependencyProperty.Register("Alpha", typeof(double), typeof(AlphaSelector),
            new FrameworkPropertyMetadata(1.0, new PropertyChangedCallback(AlphaChanged), new CoerceValueCallback(AlphaCoerce)));
        public static void AlphaChanged(object o, DependencyPropertyChangedEventArgs e)
        {
            AlphaSelector h = (AlphaSelector)o;
            h.SetAlphaOffset();
            h.SetColor();
        }
        public static object AlphaCoerce(DependencyObject d, object Brightness)
        {
            double v = (double)Brightness;
            if (v < 0) return 0.0;
            if (v > 1) return 1.0;
            return v;
        }


        public double AlphaOffset
        {
            get { return (double)GetValue(AlphaOffsetProperty); }
            private set { SetValue(AlphaOffsetProperty, value); }
        }
        public static readonly DependencyProperty AlphaOffsetProperty =
            DependencyProperty.Register("AlphaOffset", typeof(double), typeof(AlphaSelector), new UIPropertyMetadata(0.0));        


        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point p = e.GetPosition(this);

                if (Orientation == Orientation.Vertical)
                {
                    Alpha = 1 - (p.Y / this.ActualHeight);
                }
                else
                {
                    Alpha = 1 - (p.X / this.ActualWidth);
                }
            }
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Point p = e.GetPosition(this);

                if (Orientation == Orientation.Vertical)
                {
                    Alpha = 1 - (p.Y / this.ActualHeight);
                }
                else
                {
                    Alpha = 1 - (p.X / this.ActualWidth);
                }
            }
            Mouse.Capture(this);
            base.OnMouseMove(e);
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            this.ReleaseMouseCapture();
            base.OnMouseUp(e);
        }

        protected override void OnRender(DrawingContext dc)
        {
            LinearGradientBrush lb = new LinearGradientBrush();

            lb.StartPoint = new Point(0, 0);

            if (Orientation == Orientation.Vertical)
                lb.EndPoint = new Point(0, 1);
            else
                lb.EndPoint = new Point(1, 0);

            lb.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0x00, 0x00, 0x00), 0.00));
            lb.GradientStops.Add(new GradientStop(Color.FromArgb(0x00, 0x00, 0x00, 0x00), 1.00));

            dc.DrawRectangle(lb, null, new Rect(0, 0, ActualWidth, ActualHeight));

            SetAlphaOffset();

        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            SetAlphaOffset();
            return base.ArrangeOverride(finalSize);
        }


        private void SetAlphaOffset()
        {
            double length = ActualHeight;
            if (Orientation == Orientation.Horizontal)
                length = ActualWidth;
            AlphaOffset = length - (length * Alpha);
        }

        private void SetColor()
        {
            Color = Color.FromArgb((byte)Math.Round(Alpha * 255), 0, 0, 0);            
            //Brush = new SolidColorBrush(Color);
        }
    }
}