[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Color.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using Tizen.NUI.Binding;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24
25     /// <summary>
26     /// The Color class.
27     /// </summary>
28     [Tizen.NUI.Binding.TypeConverter(typeof(ColorTypeConverter))]
29     public class Color : Disposable
30     {
31         /// <summary>
32         /// Gets the black colored Color class.
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         public static readonly Color Black = new Color(0.0f, 0.0f, 0.0f, 1.0f);
36
37         /// <summary>
38         /// Gets the white colored Color class.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         public static readonly Color White = new Color(1.0f, 1.0f, 1.0f, 1.0f);
42
43         /// <summary>
44         /// Gets the red colored Color class.
45         /// </summary>
46         /// <since_tizen> 3 </since_tizen>
47         public static readonly Color Red = new Color(1.0f, 0.0f, 0.0f, 1.0f);
48
49         /// <summary>
50         /// Gets the green colored Color class.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         public static readonly Color Green = new Color(0.0f, 1.0f, 0.0f, 1.0f);
54
55         /// <summary>
56         /// Gets the blue colored Color class.
57         /// </summary>
58         /// <since_tizen> 3 </since_tizen>
59         public static readonly Color Blue = new Color(0.0f, 0.0f, 1.0f, 1.0f);
60
61         /// <summary>
62         /// Gets the yellow colored Color class.
63         /// </summary>
64         /// <since_tizen> 3 </since_tizen>
65         public static readonly Color Yellow = new Color(1.0f, 1.0f, 0.0f, 1.0f);
66
67         /// <summary>
68         /// Gets the magenta colored Color class.
69         /// </summary>
70         /// <since_tizen> 3 </since_tizen>
71         public static readonly Color Magenta = new Color(1.0f, 0.0f, 1.0f, 1.0f);
72
73         /// <summary>
74         /// Gets the cyan colored Color class.
75         /// </summary>
76         /// <since_tizen> 3 </since_tizen>
77         public static readonly Color Cyan = new Color(0.0f, 1.0f, 1.0f, 1.0f);
78
79         /// <summary>
80         /// Gets the  transparent colored Color class.
81         /// </summary>
82         /// <since_tizen> 3 </since_tizen>
83         public static readonly Color Transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
84
85         /// <summary>
86         /// swigCMemOwn
87         /// </summary>
88         /// <since_tizen> 3 </since_tizen>
89         protected bool swigCMemOwn;
90
91         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
92         private readonly bool hashDummy;
93
94         /// <summary>
95         /// Default constructor
96         /// </summary>
97         /// <since_tizen> 3 </since_tizen>
98         public Color() : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
99         {
100             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101         }
102
103
104         /// <summary>
105         /// The constructor.
106         /// </summary>
107         /// <param name="r">The red component.</param>
108         /// <param name="g">The green component.</param>
109         /// <param name="b">The blue component.</param>
110         /// <param name="a">The alpha component.</param>
111         /// <since_tizen> 3 </since_tizen>
112         public Color(float r, float g, float b, float a) : this(Interop.Vector4.new_Vector4__SWIG_1(ValueCheck(r), ValueCheck(g), ValueCheck(b), ValueCheck(a)), true)
113         {
114             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
115         }
116
117         /// <summary>
118         /// The conversion constructor from an array of four floats.
119         /// </summary>
120         /// <param name="array">array Array of R,G,B,A.</param>
121         /// <since_tizen> 3 </since_tizen>
122         public Color(float[] array) : this(Interop.Vector4.new_Vector4__SWIG_2(ValueCheck(array)), true)
123         {
124             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
125         }
126
127         internal Color(global::System.IntPtr cPtr, bool cMemoryOwn)
128         {
129             swigCMemOwn = cMemoryOwn;
130             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
131                         hashDummy = false;
132         }
133
134         /// <summary>
135         /// The red component.
136         /// </summary>
137         /// <since_tizen> 3 </since_tizen>
138         public float R
139         {
140             set
141             {
142                 Interop.Vector4.Vector4_r_set(swigCPtr, ValueCheck(value));
143                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
144             }
145             get
146             {
147                 float ret = Interop.Vector4.Vector4_r_get(swigCPtr);
148                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149                 return ret;
150             }
151         }
152
153         /// <summary>
154         /// The green component.
155         /// </summary>
156         /// <since_tizen> 3 </since_tizen>
157         public float G
158         {
159             set
160             {
161                 Interop.Vector4.Vector4_g_set(swigCPtr, ValueCheck(value));
162                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163             }
164             get
165             {
166                 float ret = Interop.Vector4.Vector4_g_get(swigCPtr);
167                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168                 return ret;
169             }
170         }
171
172         /// <summary>
173         /// The blue component.
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         public float B
177         {
178             set
179             {
180                 Interop.Vector4.Vector4_b_set(swigCPtr, ValueCheck(value));
181                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182             }
183             get
184             {
185                 float ret = Interop.Vector4.Vector4_b_get(swigCPtr);
186                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187                 return ret;
188             }
189         }
190
191         /// <summary>
192         /// The alpha component.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         public float A
196         {
197             set
198             {
199                 Interop.Vector4.Vector4_a_set(swigCPtr, ValueCheck(value));
200                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201             }
202             get
203             {
204                 float ret = Interop.Vector4.Vector4_a_get(swigCPtr);
205                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206                 return ret;
207             }
208         }
209
210         /// <summary>
211         /// The array subscript operator overload.
212         /// </summary>
213         /// <param name="index">The subscript index.</param>
214         /// <returns>The float at the given index.</returns>
215         /// <since_tizen> 3 </since_tizen>
216         public float this[uint index]
217         {
218             get
219             {
220                 return ValueOfIndex(index);
221             }
222         }
223
224         /// <summary>
225         /// Converts the Color class to Vector4 class implicitly.
226         /// </summary>
227         /// <param name="color">A color to be converted to Vector4</param>
228         /// <since_tizen> 3 </since_tizen>
229         public static implicit operator Vector4(Color color)
230         {
231             return new Vector4(color.R, color.G, color.B, color.A);
232         }
233
234         /// <summary>
235         /// Converts Vector4 class to Color class implicitly.
236         /// </summary>
237         /// <param name="vec">A Vector4 to be converted to color.</param>
238         /// <since_tizen> 3 </since_tizen>
239         public static implicit operator Color(Vector4 vec)
240         {
241             return new Color(vec.R, vec.G, vec.B, vec.A);
242         }
243
244         /// <summary>
245         /// The addition operator.
246         /// </summary>
247         /// <param name="arg1">The first value.</param>
248         /// <param name="arg2">The second value.</param>
249         /// <returns>The color containing the result of the addition.</returns>
250         /// <since_tizen> 3 </since_tizen>
251         public static Color operator +(Color arg1, Color arg2)
252         {
253             Color result = arg1.Add(arg2);
254             return ValueCheck(result);
255         }
256
257         /// <summary>
258         /// The subtraction operator.
259         /// </summary>
260         /// <param name="arg1">The first value.</param>
261         /// <param name="arg2">The second value.</param>
262         /// <returns>The color containing the result of the subtraction.</returns>
263         /// <since_tizen> 3 </since_tizen>
264         public static Color operator -(Color arg1, Color arg2)
265         {
266             Color result = arg1.Subtract(arg2);
267             return ValueCheck(result);
268         }
269
270         /// <summary>
271         /// The unary negation operator.
272         /// </summary>
273         /// <param name="arg1">The target value.</param>
274         /// <returns>The color containg the negation.</returns>
275         /// <since_tizen> 3 </since_tizen>
276         public static Color operator -(Color arg1)
277         {
278             Color result = arg1.Subtract();
279             return ValueCheck(result);
280         }
281
282         /// <summary>
283         /// The multiplication operator.
284         /// </summary>
285         /// <param name="arg1">The first value.</param>
286         /// <param name="arg2">The second value.</param>
287         /// <returns>The color containing the result of the multiplication.</returns>
288         /// <since_tizen> 3 </since_tizen>
289         public static Color operator *(Color arg1, Color arg2)
290         {
291             Color result = arg1.Multiply(arg2);
292             return ValueCheck(result);
293         }
294
295         /// <summary>
296         /// The multiplication operator.
297         /// </summary>
298         /// <param name="arg1">The first value.</param>
299         /// <param name="arg2">The second value.</param>
300         /// <returns>The color containing the result of the multiplication.</returns>
301         /// <since_tizen> 3 </since_tizen>
302         public static Color operator *(Color arg1, float arg2)
303         {
304             Color result = arg1.Multiply(arg2);
305             return ValueCheck(result);
306         }
307
308         /// <summary>
309         /// The division operator.
310         /// </summary>
311         /// <param name="arg1">The first value.</param>
312         /// <param name="arg2">The second value.</param>
313         /// <returns>The color containing the result of the division.</returns>
314         /// <since_tizen> 3 </since_tizen>
315         public static Color operator /(Color arg1, Color arg2)
316         {
317             Color result = arg1.Divide(arg2);
318             return ValueCheck(result);
319         }
320
321         /// <summary>
322         /// The division operator.
323         /// </summary>
324         /// <param name="arg1">The first value.</param>
325         /// <param name="arg2">The second value.</param>
326         /// <returns>The color containing the result of the division.</returns>
327         /// <since_tizen> 3 </since_tizen>
328         public static Color operator /(Color arg1, float arg2)
329         {
330             Color result = arg1.Divide(arg2);
331             return ValueCheck(result);
332         }
333
334         /// <summary>
335         /// Checks if two color classes are same.
336         /// </summary>
337         /// <param name="rhs">A color to be compared.</param>
338         /// <returns>If two colors are are same, then true.</returns>
339         /// <since_tizen> 3 </since_tizen>
340         public bool EqualTo(Color rhs)
341         {
342             bool ret = Interop.Vector4.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
343
344             if (rhs == null) return false;
345
346             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
347             return ret;
348         }
349
350         /// <summary>
351         /// Checks if two color classes are different.
352         /// </summary>
353         /// <param name="rhs">A color to be compared.</param>
354         /// <returns>If two colors are are different, then true.</returns>
355         /// <since_tizen> 3 </since_tizen>
356         public bool NotEqualTo(Color rhs)
357         {
358             bool ret = Interop.Vector4.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
359             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
360             return ret;
361         }
362
363         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
364         {
365             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
366         }
367
368         internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
369         {
370             Color ret = new Color(cPtr, false);
371             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
372             return ret;
373         }
374
375         internal static Color ValueCheck(Color color)
376         {
377             if (color.R < 0.0f)
378             {
379                 color.R = 0.0f;
380                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
381             }
382             else if (color.R > 1.0f)
383             {
384                 color.R = 1.0f;
385                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
386             }
387             if (color.G < 0.0f)
388             {
389                 color.G = 0.0f;
390                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
391             }
392             else if (color.G > 1.0f)
393             {
394                 color.G = 1.0f;
395                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
396             }
397             if (color.B < 0.0f)
398             {
399                 color.B = 0.0f;
400                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
401             }
402             else if (color.B > 1.0f)
403             {
404                 color.B = 1.0f;
405                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
406             }
407             if (color.A < 0.0f)
408             {
409                 color.A = 0.0f;
410                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
411             }
412             else if (color.A > 1.0f)
413             {
414                 color.A = 1.0f;
415                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
416             }
417             return color;
418         }
419
420         internal static float ValueCheck(float value)
421         {
422             if (value < 0.0f)
423             {
424                 value = 0.0f;
425                 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
426             }
427             else if (value > 1.0f)
428             {
429                 value = 1.0f;
430                 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
431             }
432             return value;
433         }
434
435         internal static float[] ValueCheck(float[] arr)
436         {
437             for (int i = 0; i < arr.Length; i++)
438             {
439                 if (arr[i] < 0.0f)
440                 {
441                     arr[i] = 0.0f;
442                     NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
443                 }
444                 else if (arr[i] > 1.0f)
445                 {
446                     arr[i] = 1.0f;
447                     NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
448                 }
449             }
450             return arr;
451         }
452
453         /// <summary>
454         /// Dispose.
455         /// </summary>
456         /// <since_tizen> 3 </since_tizen>
457         protected override void Dispose(DisposeTypes type)
458         {
459             if (disposed)
460             {
461                 return;
462             }
463
464             //Release your own unmanaged resources here.
465             //You should not access any managed member here except static instance.
466             //because the execution order of Finalizes is non-deterministic.
467
468             if (swigCPtr.Handle != global::System.IntPtr.Zero)
469             {
470                 if (swigCMemOwn)
471                 {
472                     swigCMemOwn = false;
473                     Interop.Vector4.delete_Vector4(swigCPtr);
474                 }
475                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
476             }
477             base.Dispose(type);
478         }
479
480         private Color Add(Color rhs)
481         {
482             Color ret = new Color(Interop.Vector4.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
483             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
484             return ret;
485         }
486
487         private Color AddAssign(Vector4 rhs)
488         {
489             Color ret = new Color(Interop.Vector4.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
490             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
491             return ret;
492         }
493
494         private Color Subtract(Color rhs)
495         {
496             Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
497             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
498             return ret;
499         }
500
501         private Color SubtractAssign(Color rhs)
502         {
503             Color ret = new Color(Interop.Vector4.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
504             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
505             return ret;
506         }
507
508         private Color Multiply(Color rhs)
509         {
510             Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
511             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
512             return ret;
513         }
514
515         private Color Multiply(float rhs)
516         {
517             Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
518             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
519             return ret;
520         }
521
522         private Color MultiplyAssign(Color rhs)
523         {
524             Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
525             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
526             return ret;
527         }
528
529         private Color MultiplyAssign(float rhs)
530         {
531             Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
532             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
533             return ret;
534         }
535
536         private Color Divide(Vector4 rhs)
537         {
538             Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
539             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
540             return ret;
541         }
542
543         private Color Divide(float rhs)
544         {
545             Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
546             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
547             return ret;
548         }
549
550         private Color DivideAssign(Color rhs)
551         {
552             Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
553             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
554             return ret;
555         }
556
557         private Color DivideAssign(float rhs)
558         {
559             Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
560             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
561             return ret;
562         }
563
564         private Color Subtract()
565         {
566             Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_1(swigCPtr), true);
567             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
568             return ret;
569         }
570
571         private static bool EqualsColorValue(float f1, float f2)
572         {
573             float EPS = (float)Math.Abs(f1 * .00001);
574             if(Math.Abs(f1 - f2) <= EPS)
575             {
576                 return true;
577             }
578             else
579             {
580                 return false;
581             }
582         }
583
584         private static bool EqualsColor(Color c1, Color c2)
585         {
586             return EqualsColorValue(c1.R, c2.R) && EqualsColorValue(c1.G, c2.G)
587                 && EqualsColorValue(c1.B, c2.B) && EqualsColorValue(c1.A, c2.A);
588         }
589
590         /// <summary>
591         /// Determines whether the specified object is equal to the current object.
592         /// </summary>
593         /// <param name="obj">The object to compare with the current object.</param>
594         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
595         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
596         [EditorBrowsable(EditorBrowsableState.Never)]
597         public override bool Equals(System.Object obj)
598         {
599             Color color = obj as Color;
600             bool equal = false;
601             if (color == null)
602             {
603                 return equal;
604             }
605
606             if (EqualsColor(this, color))
607             {
608                 equal = true;
609             }
610             return equal;
611         }
612
613         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
614         [EditorBrowsable(EditorBrowsableState.Never)]
615         public override int GetHashCode()
616         {
617             return hashDummy.GetHashCode();
618         }
619
620         private float ValueOfIndex(uint index)
621         {
622             float ret = Interop.Vector4.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
623             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
624             return ret;
625         }
626
627     }
628
629 }
630
631