-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
160 lines (139 loc) · 4.59 KB
/
MainWindow.xaml.cs
File metadata and controls
160 lines (139 loc) · 4.59 KB
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
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace RedLight;
public partial class MainWindow : Window
{
private DispatcherTimer? _sliderTimer;
public MainWindow()
{
InitializeComponent();
InitializeSliderTimer();
Deactivated += (_, _) => HidePopup();
ApplyCurrentFilter();
}
public void ShowPopup()
{
var workArea = SystemParameters.WorkArea;
Left = workArea.Right - Width - 12;
Top = workArea.Bottom - Height - 24;
Opacity = 0;
Show();
Activate();
var fadeIn = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(150));
var slideUp = new DoubleAnimation(Top + 10, Top, TimeSpan.FromMilliseconds(150))
{
EasingFunction = new QuadraticEase()
};
BeginAnimation(OpacityProperty, fadeIn);
BeginAnimation(TopProperty, slideUp);
}
public void HidePopup()
{
Hide();
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
HidePopup();
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
private void InitializeSliderTimer()
{
_sliderTimer = new DispatcherTimer
{
Interval = TimeSpan.FromMilliseconds(100)
};
_sliderTimer.Tick += (s, e) =>
{
ApplyCurrentFilter();
_sliderTimer.Stop();
};
}
private void IntensitySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
if (_sliderTimer == null) return;
_sliderTimer.Stop();
_sliderTimer.Start();
if (IntensityValueText != null)
IntensityValueText.Text = $"{(int)(IntensitySlider.Value * 100)}%";
}
private void ApplyFilter(object? sender, RoutedEventArgs? e) => ApplyCurrentFilter();
private void ApplyCurrentFilter()
{
if (EnableFilterCheckBox == null || FilterTypeComboBox == null || IntensitySlider == null)
return;
if (EnableFilterCheckBox.IsChecked != true)
{
NativeMethods.ResetColorEffect();
return;
}
float intensity = (float)IntensitySlider.Value;
string filterType = (FilterTypeComboBox.SelectedItem as ComboBoxItem)?.Content as string ?? "Red";
float[] matrix = GetMatrixForFilter(filterType, intensity);
var effect = new NativeMethods.MagColorEffect(matrix);
NativeMethods.MagSetFullscreenColorEffect(ref effect);
}
private void QuitButton_Click(object sender, RoutedEventArgs e)
{
System.Windows.Application.Current.Shutdown();
}
private static float[] GetMatrixForFilter(string filterType, float intensity)
{
float[] matrix = filterType switch
{
"Grayscale" =>
[
0.2126f, 0.7152f, 0.0722f, 0, 0,
0.2126f, 0.7152f, 0.0722f, 0, 0,
0.2126f, 0.7152f, 0.0722f, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
],
"Red/Green (Protanopia)" =>
[
0.1121f, 0.8853f, -0.0005f, 0, 0,
0.1127f, 0.8897f, -0.0001f, 0, 0,
0.0045f, 0.0000f, 1.0019f, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
],
"Green/Red (Deuteranopia)" =>
[
0.2920f, 0.7054f, -0.0003f, 0, 0,
0.2934f, 0.7089f, 0.0000f, 0, 0,
-0.0210f,0.0256f, 1.0019f, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
],
"Blue/Yellow (Tritanopia)" =>
[
1.0160f, 0.1351f, -0.1488f, 0, 0,
-0.0154f,0.8683f, 0.1448f, 0, 0,
0.1002f, 0.8168f, 0.1169f, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
],
_ =>
[
1, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1, 0,
0, 0, 0, 0, 1
]
};
var identity = NativeMethods.IdentityMatrix;
for (int i = 0; i < 25; i++)
{
matrix[i] = (identity[i] * (1 - intensity)) + (matrix[i] * intensity);
}
return matrix;
}
}