aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.JobsV2/Models/VisualOffsetModel.cs
blob: 74651992e1878b05193e7a04ecd7f90a6ba4996e (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
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
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimit
using ColorMine.ColorSpaces;
using Colourful;
using Colourful.Conversion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using Tango.Core.Commands;
using Tango.SharedUI;

namespace Tango.PPC.Jobs.Models
{
    
    public class VisualOffsetModel : ViewModel
    {
        public enum TypeColorCorrection
        {
            Lightness = 1,
            Chroma = 2,
            Hue = 3,
        }

        public class VisualLCHOffset : ViewModel
        {
            public TypeColorCorrection ColorType { get; set; }

            public int Offset { get; set; }
            
            public double L { get; set; }
            public double C { get; set; }

            private double _h;

            public double H
            {
                get { return _h; }
                set { _h = value;
                    RaisePropertyChangedAuto();
                }
            }
            
            public SolidColorBrush ColorBrush
            {
                get
                {
                    Lch lch = new Lch() { L = L, C = C, H = H };
                    IRgb RGB = lch.ToRgb();
                    return new SolidColorBrush() { Color = Color.FromRgb(System.Convert.ToByte(RGB.R), System.Convert.ToByte(RGB.G), System.Convert.ToByte(RGB.B)) };
                }
            }

            private bool _isVisible;

            public bool IsVisible
            {
                get { return _isVisible; }
                set
                {
                    _isVisible = value;
                    RaisePropertyChangedAuto();
                }
            }
            

            public VisualLCHOffset(TypeColorCorrection colorType, int offset)
            {
                ColorType = colorType;
                Offset = offset;
            }

            public void Init(Lch lch)
            {
                L = lch.L;
                C = lch.C;
                H = lch.H;
                RaisePropertyChanged( nameof(ColorBrush));
            }
        }

        #region Property

       
        private double _l;
        public double L
        {
            get { return _l; }
            set
            {
                if (_l != value)
                {
                    _l = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        private double _a;
        public double A
        {
            get { return _a; }
            set
            {
                if (_a != value)
                {
                    _a = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        private double _b;
        public double B
        {
            get { return _b; }
            set
            {
                if (_b != value)
                {
                    _b = value;
                    RaisePropertyChangedAuto();
                }
            }
        }
        
        public double C { get; set; }
        public double H { get; set; }

        private double _sourcel;
        public double SourceL
        {
            get { return _sourcel; }
            set {
                if(_sourcel != value)
                {
                    _sourcel = value;
                    RaisePropertyChangedAuto();
                    //ManualCorrection(LightnessOffset, ChromaOffset, HueOffset);
                }
            }
        }
        
        private double _sourcea;
        public double SourceA
        {
            get { return _sourcea; }
            set
            {
                if (_sourcea != value)
                {
                    _sourcea = value;
                    RaisePropertyChangedAuto();
                    //ManualCorrection(LightnessOffset, ChromaOffset, HueOffset);
                }
            }
        }

        private double _sourceb;
        public double SourceB
        {
            get { return _sourceb; }
            set
            {
                if (_sourceb != value)
                {
                    _sourceb = value;
                    RaisePropertyChangedAuto();
                    //ManualCorrection(LightnessOffset, ChromaOffset, HueOffset);
                }
            }
        }

        private double _sourceh;
        public double SourceH
        {
            get { return _sourceh; }
            set
            {
                if (_sourceh != value)
                {
                    _sourceh = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        private double _sourcec;
        public double SourceC
        {
            get { return _sourcec; }
            set
            {
                if (_sourcec != value)
                {
                    _sourcec = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        public double LightnessOffset { get; set; }
        public double ChromaOffset { get; set; }
        public double HueOffset { get; set; }



        public VisualLCHOffset MinLightness { get; set; }
        public VisualLCHOffset MaxLightness { get; set; }
        public VisualLCHOffset MinChroma { get; set; }
        public VisualLCHOffset MaxChroma { get; set; }
        public VisualLCHOffset MinHue { get; set; }
        public VisualLCHOffset MaxHue { get; set; }
        
        public SolidColorBrush SourceColorBrush
        {
            get
            {
                //Lch lch = new Lch() { L = L, C = C, H = H };
                //IRgb RGB = lch.ToRgb();
                Lab lab = new Lab(SourceL, SourceA, SourceB);
                Rgb rgb = new Rgb(lab.ToRgb());
                return new SolidColorBrush() { Color = Color.FromRgb((byte)rgb.R, (byte)rgb.G, (byte)rgb.B) };
            }
        } 

        public SolidColorBrush ManualColorBrush
        {
            get
            {
                //Lch lch = new Lch() { L = L, C = C, H = H };
                //IRgb rgb = lch.ToRgb();
                if(LightnessOffset == 0 && ChromaOffset == 0 && HueOffset  == 0)
                {
                    return SourceColorBrush;
                }
                Lab lab = new Lab(L, A, B);
                Rgb rgb = new Rgb(lab.ToRgb());
                return new SolidColorBrush() { Color = Color.FromRgb((byte)rgb.R, (byte)rgb.G, (byte)rgb.B) };
            }
        }

        private bool _isOutOfGamut;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is out of gamut.
        /// </summary>
        public bool IsOutOfGamut
        {
            get { return _isOutOfGamut; }
            set
            {
                if (_isOutOfGamut != value)
                {
                    _isOutOfGamut = value;
                    RaisePropertyChangedAuto();
                }
            }
        }

        #endregion

        public VisualOffsetModel()
        {
            MinLightness = new VisualLCHOffset(TypeColorCorrection.Lightness, -1);
            MaxLightness = new VisualLCHOffset(TypeColorCorrection.Lightness, 1);
            MinChroma = new VisualLCHOffset(TypeColorCorrection.Chroma, -1);
            MaxChroma = new VisualLCHOffset(TypeColorCorrection.Chroma, 1);
            MinHue = new VisualLCHOffset(TypeColorCorrection.Hue, -1);
            MaxHue = new VisualLCHOffset(TypeColorCorrection.Hue, 1);
            IsOutOfGamut = false;


        }
        
        public void InitLAB(double l, double a, double b, double c, double h)
        {
            L = l;
            A = a;
            B = b;
            C = c;
            H = h;
            LightnessOffset = ChromaOffset = HueOffset = 0;

            _sourcel = l;
            RaisePropertyChanged(nameof(SourceL));
            _sourcea = a;
            RaisePropertyChanged(nameof(SourceA));
            _sourceb = b;
            RaisePropertyChanged(nameof(SourceB));
            _sourcec = c;
            RaisePropertyChanged(nameof(SourceC));
            _sourceh = h;
            RaisePropertyChanged(nameof(SourceH));

            UpdateManualColors();
        }

        public void ClearAll(double l, double a, double b)
        {
            LightnessOffset = 0;
            ChromaOffset = 0;
            HueOffset = 0;
            LabColor labColor = new LabColor(l, a, b);
            var converter = new ColourfulConverter { WhitePoint = Illuminants.D65 };
            var last_LCH = converter.ToLChab(labColor);
            SourceL = l;
            SourceA = a;
            SourceB = b;
            SourceC = last_LCH.C;
            SourceH = last_LCH.h;
            L = l;
            A = a;
            B = b;
            C = last_LCH.C;
            H = last_LCH.h;
            IsOutOfGamut = false;
        }


        public void UpdateSourceLAB(double l, double a, double b)
        {
            LabColor labColor = new LabColor(l, a, b);
            var converter = new ColourfulConverter { WhitePoint = Illuminants.D65 };
            var last_LCH = converter.ToLChab(labColor);
            SourceL =  l;
            SourceA =  a;
            SourceB =  b;
            SourceC = last_LCH.C;
            SourceH = last_LCH.h;
        }

        public void UpdateLAB(double l, double a, double b)
        {
            LabColor labColor = new LabColor(l, a, b);
            var converter = new ColourfulConverter { WhitePoint = Illuminants.D65 };
            var last_LCH = converter.ToLChab(labColor);
            L = l;
            A = a;
            B = b;
            C = last_LCH.C;
            H = last_LCH.h;
        }

        public void UpdateManualColors()
        {
            Lch lch = Correction(SourceL, SourceC, SourceH,-6, 0, 0);
            MinLightness.Init(lch);
            lch = Correction(SourceL, SourceC, SourceH, 6, 0, 0);
            MaxLightness.Init(lch);
            lch = Correction(SourceL, SourceC, SourceH, 0, -6, 0);
            MinChroma.Init(lch);
            lch = Correction(SourceL, SourceC, SourceH, 0, 6, 0);
            MaxChroma.Init(lch);
            lch = Correction(SourceL, SourceC, SourceH, 0, 0, -6);
            MinHue.Init(lch);
            lch = Correction(SourceL, SourceC, SourceH, 0, 0, 6);
            MaxHue.Init(lch);
            
            RaisePropertyChanged(nameof(SourceColorBrush));
            RaisePropertyChanged(nameof(ManualColorBrush));
        }

        /// <summary>
        /// Manuals the correction. During change lightnessOffset, chromaOffset or hueOffset in app caused to change LAB of visual correction.
        /// </summary>
        /// <param name="lightnessOffset">The lightness offset.</param>
        /// <param name="chromaOffset">The chroma offset.</param>
        /// <param name="hueOffset">The hue offset.</param>
        public void ManualCorrection( double lightnessOffset, double chromaOffset, double hueOffset)
        {
            Lch lch = Correction(SourceL, SourceC, SourceH, lightnessOffset, chromaOffset, hueOffset);

            L = lch.L;
            A = lch.C * Math.Cos(lch.H * (Math.PI / 180));
            B = lch.C * Math.Sin(lch.H * (Math.PI / 180));
            
            RaisePropertyChanged(nameof(SourceColorBrush));
            RaisePropertyChanged(nameof(ManualColorBrush));
        }

        /// <summary>
        /// Calculate Lch on the specified lightness offset.
        /// </summary>
        /// <param name="lightnessOffset">The lightness offset.</param>
        /// <param name="chromaOffset">The chroma offset.</param>
        /// <param name="hueOffset">The hue offset.</param>
        /// <returns></returns>
        public Lch Correction(double lch_l, double lch_c, double lch_h, double lightnessOffset, double chromaOffset, double hueOffset)
        {
            double L1 = lch_l;
            double C1 = lch_c;
            double H1 = lch_h;
            if (H1 < 0)
                H1 += 360;
            double refX_C = C1;
            ///lightness
            double SL = 0;
            if (L1 < 16)
                SL = 0.511;
            else
                SL = L1 * 0.040975 / (1 + 0.01765 * L1);

            L1 += lightnessOffset * 2 * SL;
            if (L1 > 100)
                L1 = 100;
            if (L1 < 0)
                L1 = 0;

            double SC = (0.638 + 0.0638 * refX_C / (1 + 0.0131 * refX_C));

            C1 = C1 + (chromaOffset * SC);
            //  if (C1 > 128)
            //       C1 = 128;
            if (C1 < 0)
                C1 = 0;

            //double h1 = H1;
            //if (h1 < 0)
            //    h1 = h1 + 360;
            double refX_CQ = Math.Pow(refX_C, 4);
            double refX_H = H1;
            double refX_F = Math.Sqrt(refX_CQ / (refX_CQ + 1900));
            double refX_T = 0;
            if ((refX_H > 164) & (refX_H < 345))
                refX_T = 0.56 + Math.Abs(0.2 * Math.Cos(Math.PI * (refX_H + 168) / 180));
            else if ((refX_H >= 345) | (refX_H <= 164))
                refX_T = 0.36 + Math.Abs(0.4 * Math.Cos(Math.PI * (refX_H + 35) / 180));

            //double refX_SC = (0.638 + 0.0638 * refX_C / (1 + 0.0131 * refX_C));
            double refX_SH = SC * (refX_T * refX_F + 1 - refX_F);
            if (C1 != 0)
            {
                var argum = 1 - (refX_SH * refX_SH * hueOffset * hueOffset / (2 * C1 * C1));
                if (argum < -1)
                    argum = -1;
                if (argum > 1)
                    argum = 1;

                double tmp = (180 / Math.PI) * Math.Acos(argum);  //degrees
                if (hueOffset < 0)//?
                    tmp *= -1;
                H1 += tmp;
            }
            Lch lch = new Lch() { L = L1, C = C1, H = H1 };
            return lch;

        }


        #region Converter


        #endregion
    }
}