[NUI] Sync with dalihub & API5 branch (#631)
[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
21 namespace Tizen.NUI
22 {
23
24     /// <summary>
25     /// The Color class.
26     /// </summary>
27     [TypeConverter(typeof(ColorTypeConverter))]
28     public class Color : global::System.IDisposable
29     {
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         /// <summary>
92         /// A Flat to check if it is already disposed.
93         /// </summary>
94         /// <since_tizen> 3 </since_tizen>
95         protected bool disposed = false;
96
97         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
98         //A Flag to check who called Dispose(). (By User or DisposeQueue)
99         private bool isDisposeQueued = false;
100
101         /// <summary>
102         /// Default constructor
103         /// </summary>
104         /// <since_tizen> 3 </since_tizen>
105         public Color() : this(NDalicPINVOKE.new_Vector4__SWIG_0(), true)
106         {
107             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
108         }
109
110
111         /// <summary>
112         /// The constructor.
113         /// </summary>
114         /// <param name="r">The red component.</param>
115         /// <param name="g">The green component.</param>
116         /// <param name="b">The blue component.</param>
117         /// <param name="a">The alpha component.</param>
118         /// <since_tizen> 3 </since_tizen>
119         public Color(float r, float g, float b, float a) : this(NDalicPINVOKE.new_Vector4__SWIG_1(ValueCheck(r), ValueCheck(g), ValueCheck(b), ValueCheck(a)), true)
120         {
121             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122         }
123
124         /// <summary>
125         /// The conversion constructor from an array of four floats.
126         /// </summary>
127         /// <param name="array">array Array of R,G,B,A.</param>
128         /// <since_tizen> 3 </since_tizen>
129         public Color(float[] array) : this(NDalicPINVOKE.new_Vector4__SWIG_2(ValueCheck(array)), true)
130         {
131             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
132         }
133
134         internal Color(global::System.IntPtr cPtr, bool cMemoryOwn)
135         {
136             swigCMemOwn = cMemoryOwn;
137             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
138         }
139
140         /// <summary>
141         /// Dispose.
142         /// </summary>
143         /// <since_tizen> 3 </since_tizen>
144         ~Color()
145         {
146             if (!isDisposeQueued)
147             {
148                 isDisposeQueued = true;
149                 DisposeQueue.Instance.Add(this);
150             }
151         }
152
153         /// <summary>
154         /// The red component.
155         /// </summary>
156         /// <since_tizen> 3 </since_tizen>
157         public float R
158         {
159             set
160             {
161                 NDalicPINVOKE.Vector4_r_set(swigCPtr, ValueCheck(value));
162                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163             }
164             get
165             {
166                 float ret = NDalicPINVOKE.Vector4_r_get(swigCPtr);
167                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
168                 return ret;
169             }
170         }
171
172         /// <summary>
173         /// The green component.
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         public float G
177         {
178             set
179             {
180                 NDalicPINVOKE.Vector4_g_set(swigCPtr, ValueCheck(value));
181                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182             }
183             get
184             {
185                 float ret = NDalicPINVOKE.Vector4_g_get(swigCPtr);
186                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
187                 return ret;
188             }
189         }
190
191         /// <summary>
192         /// The blue component.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         public float B
196         {
197             set
198             {
199                 NDalicPINVOKE.Vector4_b_set(swigCPtr, ValueCheck(value));
200                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
201             }
202             get
203             {
204                 float ret = NDalicPINVOKE.Vector4_b_get(swigCPtr);
205                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206                 return ret;
207             }
208         }
209
210         /// <summary>
211         /// The alpha component.
212         /// </summary>
213         /// <since_tizen> 3 </since_tizen>
214         public float A
215         {
216             set
217             {
218                 NDalicPINVOKE.Vector4_a_set(swigCPtr, ValueCheck(value));
219                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220             }
221             get
222             {
223                 float ret = NDalicPINVOKE.Vector4_a_get(swigCPtr);
224                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225                 return ret;
226             }
227         }
228
229         /// <summary>
230         /// The array subscript operator overload.
231         /// </summary>
232         /// <param name="index">The subscript index.</param>
233         /// <returns>The float at the given index.</returns>
234         /// <since_tizen> 3 </since_tizen>
235         public float this[uint index]
236         {
237             get
238             {
239                 return ValueOfIndex(index);
240             }
241         }
242
243         /// <summary>
244         /// Converts the Color class to Vector4 class implicitly.
245         /// </summary>
246         /// <param name="color">A color to be converted to Vector4</param>
247         /// <since_tizen> 3 </since_tizen>
248         public static implicit operator Vector4(Color color)
249         {
250             return new Vector4(color.R, color.G, color.B, color.A);
251         }
252
253         /// <summary>
254         /// Converts Vector4 class to Color class implicitly.
255         /// </summary>
256         /// <param name="vec">A Vector4 to be converted to color.</param>
257         /// <since_tizen> 3 </since_tizen>
258         public static implicit operator Color(Vector4 vec)
259         {
260             return new Color(vec.R, vec.G, vec.B, vec.A);
261         }
262
263         /// <summary>
264         /// The addition operator.
265         /// </summary>
266         /// <param name="arg1">The first value.</param>
267         /// <param name="arg2">The second value.</param>
268         /// <returns>The color containing the result of the addition.</returns>
269         /// <since_tizen> 3 </since_tizen>
270         public static Color operator +(Color arg1, Color arg2)
271         {
272             Color result = arg1.Add(arg2);
273             return ValueCheck(result);
274         }
275
276         /// <summary>
277         /// The subtraction operator.
278         /// </summary>
279         /// <param name="arg1">The first value.</param>
280         /// <param name="arg2">The second value.</param>
281         /// <returns>The color containing the result of the subtraction.</returns>
282         /// <since_tizen> 3 </since_tizen>
283         public static Color operator -(Color arg1, Color arg2)
284         {
285             Color result = arg1.Subtract(arg2);
286             return ValueCheck(result);
287         }
288
289         /// <summary>
290         /// The unary negation operator.
291         /// </summary>
292         /// <param name="arg1">The target value.</param>
293         /// <returns>The color containg the negation.</returns>
294         /// <since_tizen> 3 </since_tizen>
295         public static Color operator -(Color arg1)
296         {
297             Color result = arg1.Subtract();
298             return ValueCheck(result);
299         }
300
301         /// <summary>
302         /// The multiplication operator.
303         /// </summary>
304         /// <param name="arg1">The first value.</param>
305         /// <param name="arg2">The second value.</param>
306         /// <returns>The color containing the result of the multiplication.</returns>
307         /// <since_tizen> 3 </since_tizen>
308         public static Color operator *(Color arg1, Color arg2)
309         {
310             Color result = arg1.Multiply(arg2);
311             return ValueCheck(result);
312         }
313
314         /// <summary>
315         /// The multiplication operator.
316         /// </summary>
317         /// <param name="arg1">The first value.</param>
318         /// <param name="arg2">The second value.</param>
319         /// <returns>The color containing the result of the multiplication.</returns>
320         /// <since_tizen> 3 </since_tizen>
321         public static Color operator *(Color arg1, float arg2)
322         {
323             Color result = arg1.Multiply(arg2);
324             return ValueCheck(result);
325         }
326
327         /// <summary>
328         /// The division operator.
329         /// </summary>
330         /// <param name="arg1">The first value.</param>
331         /// <param name="arg2">The second value.</param>
332         /// <returns>The color containing the result of the division.</returns>
333         /// <since_tizen> 3 </since_tizen>
334         public static Color operator /(Color arg1, Color arg2)
335         {
336             Color result = arg1.Divide(arg2);
337             return ValueCheck(result);
338         }
339
340         /// <summary>
341         /// The division operator.
342         /// </summary>
343         /// <param name="arg1">The first value.</param>
344         /// <param name="arg2">The second value.</param>
345         /// <returns>The color containing the result of the division.</returns>
346         /// <since_tizen> 3 </since_tizen>
347         public static Color operator /(Color arg1, float arg2)
348         {
349             Color result = arg1.Divide(arg2);
350             return ValueCheck(result);
351         }
352
353         /// <summary>
354         /// To make a color instance be disposed.
355         /// </summary>
356         /// <since_tizen> 3 </since_tizen>
357         public void Dispose()
358         {
359             //Throw excpetion if Dispose() is called in separate thread.
360             if (!Window.IsInstalled())
361             {
362                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
363             }
364
365             if (isDisposeQueued)
366             {
367                 Dispose(DisposeTypes.Implicit);
368             }
369             else
370             {
371                 Dispose(DisposeTypes.Explicit);
372                 System.GC.SuppressFinalize(this);
373             }
374         }
375
376         /// <summary>
377         /// Checks if two color classes are same.
378         /// </summary>
379         /// <param name="rhs">A color to be compared.</param>
380         /// <returns>If two colors are are same, then true.</returns>
381         /// <since_tizen> 3 </since_tizen>
382         public bool EqualTo(Color rhs)
383         {
384             bool ret = NDalicPINVOKE.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
385
386             if (rhs == null) return false;
387
388             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
389             return ret;
390         }
391
392         /// <summary>
393         /// Checks if two color classes are different.
394         /// </summary>
395         /// <param name="rhs">A color to be compared.</param>
396         /// <returns>If two colors are are different, then true.</returns>
397         /// <since_tizen> 3 </since_tizen>
398         public bool NotEqualTo(Color rhs)
399         {
400             bool ret = NDalicPINVOKE.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
401             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
402             return ret;
403         }
404
405         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
406         {
407             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
408         }
409
410         internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
411         {
412             Color ret = new Color(cPtr, false);
413             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
414             return ret;
415         }
416
417         internal static Color ValueCheck(Color color)
418         {
419             if (color.R < 0.0f)
420             {
421                 color.R = 0.0f;
422                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
423             }
424             else if (color.R > 1.0f)
425             {
426                 color.R = 1.0f;
427                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
428             }
429             if (color.G < 0.0f)
430             {
431                 color.G = 0.0f;
432                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
433             }
434             else if (color.G > 1.0f)
435             {
436                 color.G = 1.0f;
437                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
438             }
439             if (color.B < 0.0f)
440             {
441                 color.B = 0.0f;
442                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
443             }
444             else if (color.B > 1.0f)
445             {
446                 color.B = 1.0f;
447                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
448             }
449             if (color.A < 0.0f)
450             {
451                 color.A = 0.0f;
452                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
453             }
454             else if (color.A > 1.0f)
455             {
456                 color.A = 1.0f;
457                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
458             }
459             return color;
460         }
461
462         internal static float ValueCheck(float value)
463         {
464             if (value < 0.0f)
465             {
466                 value = 0.0f;
467                 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
468             }
469             else if (value > 1.0f)
470             {
471                 value = 1.0f;
472                 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
473             }
474             return value;
475         }
476
477         internal static float[] ValueCheck(float[] arr)
478         {
479             for (int i = 0; i < arr.Length; i++)
480             {
481                 if (arr[i] < 0.0f)
482                 {
483                     arr[i] = 0.0f;
484                     NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
485                 }
486                 else if (arr[i] > 1.0f)
487                 {
488                     arr[i] = 1.0f;
489                     NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
490                 }
491             }
492             return arr;
493         }
494
495         /// <summary>
496         /// Dispose.
497         /// </summary>
498         /// <since_tizen> 3 </since_tizen>
499         protected virtual void Dispose(DisposeTypes type)
500         {
501             if (disposed)
502             {
503                 return;
504             }
505
506             if(type == DisposeTypes.Explicit)
507             {
508                 //Called by User
509                 //Release your own managed resources here.
510                 //You should release all of your own disposable objects here.
511             }
512
513             //Release your own unmanaged resources here.
514             //You should not access any managed member here except static instance.
515             //because the execution order of Finalizes is non-deterministic.
516
517             if (swigCPtr.Handle != global::System.IntPtr.Zero)
518             {
519                 if (swigCMemOwn)
520                 {
521                     swigCMemOwn = false;
522                     NDalicPINVOKE.delete_Vector4(swigCPtr);
523                 }
524                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
525             }
526             disposed = true;
527         }
528
529         private Color Add(Color rhs)
530         {
531             Color ret = new Color(NDalicPINVOKE.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
532             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
533             return ret;
534         }
535
536         private Color AddAssign(Vector4 rhs)
537         {
538             Color ret = new Color(NDalicPINVOKE.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
539             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
540             return ret;
541         }
542
543         private Color Subtract(Color rhs)
544         {
545             Color ret = new Color(NDalicPINVOKE.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
546             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
547             return ret;
548         }
549
550         private Color SubtractAssign(Color rhs)
551         {
552             Color ret = new Color(NDalicPINVOKE.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
553             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
554             return ret;
555         }
556
557         private Color Multiply(Color rhs)
558         {
559             Color ret = new Color(NDalicPINVOKE.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
560             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
561             return ret;
562         }
563
564         private Color Multiply(float rhs)
565         {
566             Color ret = new Color(NDalicPINVOKE.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
567             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
568             return ret;
569         }
570
571         private Color MultiplyAssign(Color rhs)
572         {
573             Color ret = new Color(NDalicPINVOKE.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
574             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
575             return ret;
576         }
577
578         private Color MultiplyAssign(float rhs)
579         {
580             Color ret = new Color(NDalicPINVOKE.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
581             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
582             return ret;
583         }
584
585         private Color Divide(Vector4 rhs)
586         {
587             Color ret = new Color(NDalicPINVOKE.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
588             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
589             return ret;
590         }
591
592         private Color Divide(float rhs)
593         {
594             Color ret = new Color(NDalicPINVOKE.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
595             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
596             return ret;
597         }
598
599         private Color DivideAssign(Color rhs)
600         {
601             Color ret = new Color(NDalicPINVOKE.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
602             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
603             return ret;
604         }
605
606         private Color DivideAssign(float rhs)
607         {
608             Color ret = new Color(NDalicPINVOKE.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
609             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
610             return ret;
611         }
612
613         private Color Subtract()
614         {
615             Color ret = new Color(NDalicPINVOKE.Vector4_Subtract__SWIG_1(swigCPtr), true);
616             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
617             return ret;
618         }
619
620         private float ValueOfIndex(uint index)
621         {
622             float ret = NDalicPINVOKE.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