aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RgbVM.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RgbVM.cs')
-rw-r--r--Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RgbVM.cs148
1 files changed, 148 insertions, 0 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RgbVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RgbVM.cs
new file mode 100644
index 000000000..e64592aae
--- /dev/null
+++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/RgbVM.cs
@@ -0,0 +1,148 @@
+using ColorMine.ColorSpaces;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Media;
+using System.Windows.Threading;
+using Tango.Core;
+using Tango.SharedUI;
+
+namespace Tango.MachineStudio.RML.ViewModels
+{
+ public class RgbVM : ExtendedObject
+ {
+ private DispatcherTimer _color_change_timer;
+
+ public event EventHandler<Color> ColorChanged;
+
+ public RgbVM()
+ {
+ _color_change_timer = new DispatcherTimer();
+ _color_change_timer.Interval = TimeSpan.FromMilliseconds(30);
+ _color_change_timer.Tick += _color_change_timer_Tick;
+ _color_change_timer.Stop();
+
+ Color = Colors.DimGray;
+ }
+
+ private void _color_change_timer_Tick(object sender, EventArgs e)
+ {
+ _color_change_timer.Stop();
+ ColorChanged?.Invoke(this, Color);
+ }
+
+ private double _red;
+ public double Red
+ {
+ get { return _red; }
+ set { _red = value; SynchronizeColor(); RaisePropertyChangedAuto(); }
+ }
+
+ private double _green;
+ public double Green
+ {
+ get { return _green; }
+ set { _green = value; SynchronizeColor(); RaisePropertyChangedAuto(); }
+ }
+
+ private double _blue;
+ public double Blue
+ {
+ get { return _blue; }
+ set { _blue = value; SynchronizeColor(); RaisePropertyChangedAuto(); }
+ }
+
+ private double _l;
+ public double L
+ {
+ get { return _l; }
+ set { _l = value; SynchronizeColor(); RaisePropertyChangedAuto(); }
+ }
+
+ private double _a;
+ public double A
+ {
+ get { return _a; }
+ set { _a = value; SynchronizeColor(); RaisePropertyChangedAuto(); }
+ }
+
+ private double _b;
+ public double B
+ {
+ get { return _b; }
+ set { _b = value; SynchronizeColor(); RaisePropertyChangedAuto(); }
+ }
+
+ private Color _color;
+ public Color Color
+ {
+ get { return _color; }
+ set { _color = value; RaisePropertyChanged(nameof(Color)); SynchronizeComponents(); }
+ }
+
+ private bool _isLab;
+ public bool IsLab
+ {
+ get { return _isLab; }
+ set { _isLab = value; RaisePropertyChangedAuto(); }
+ }
+
+
+ private void SynchronizeColor()
+ {
+ _color_change_timer.Stop();
+
+ if (!IsLab)
+ {
+ Rgb rgb = new Rgb(Red, Green, Blue);
+ Lab lab = rgb.To<Lab>();
+ _l = lab.L;
+ _a = lab.A;
+ _b = lab.B;
+ }
+ else
+ {
+ Lab lab = new Lab(L, A, B);
+ Rgb rgb = lab.To<Rgb>();
+ _red = rgb.R;
+ _green = rgb.G;
+ _blue = rgb.B;
+ }
+
+ _color = Color.FromRgb((byte)Red, (byte)Green, (byte)Blue);
+ RaisePropertyChanged(nameof(Color));
+
+ _color_change_timer.Start();
+ }
+
+ private void SynchronizeComponents()
+ {
+ _color_change_timer.Stop();
+
+ _red = Color.R;
+ _green = Color.G;
+ _blue = Color.B;
+
+ if (IsLab)
+ {
+ Rgb rgb = new Rgb(Red, Green, Blue);
+ Lab lab = rgb.To<Lab>();
+ _l = lab.L;
+ _a = lab.A;
+ _b = lab.B;
+ }
+
+ RaisePropertyChanged(nameof(Red));
+ RaisePropertyChanged(nameof(Green));
+ RaisePropertyChanged(nameof(Blue));
+ RaisePropertyChanged(nameof(L));
+ RaisePropertyChanged(nameof(A));
+ RaisePropertyChanged(nameof(B));
+ RaisePropertyChanged(nameof(Color));
+
+ _color_change_timer.Start();
+ }
+ }
+}