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