Merge "[SystemSettings] Added <feature> <exception> for methods"
[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             ValueCheck(result);
135             return result;
136         }
137
138         /// <summary>
139         /// The subtraction operator.
140         /// </summary>
141         /// <param name="arg1">The first value.</param>
142         /// <param name="arg2">The second value.</param>
143         /// <returns>The color containing the result of the subtraction.</returns>
144         /// <since_tizen> 3 </since_tizen>
145         public static Color operator -(Color arg1, Color arg2)
146         {
147             Color result = arg1.Subtract(arg2);
148             ValueCheck(result);
149             return result;
150         }
151
152         /// <summary>
153         /// The unary negation operator.
154         /// </summary>
155         /// <param name="arg1">The target value.</param>
156         /// <returns>The color containg the negation.</returns>
157         /// <since_tizen> 3 </since_tizen>
158         public static Color operator -(Color arg1)
159         {
160             Color result = arg1.Subtract();
161             ValueCheck(result);
162             return result;
163         }
164
165         /// <summary>
166         /// The multiplication operator.
167         /// </summary>
168         /// <param name="arg1">The first value.</param>
169         /// <param name="arg2">The second value.</param>
170         /// <returns>The color containing the result of the multiplication.</returns>
171         /// <since_tizen> 3 </since_tizen>
172         public static Color operator *(Color arg1, Color arg2)
173         {
174             Color result = arg1.Multiply(arg2);
175             ValueCheck(result);
176             return result;
177         }
178
179         /// <summary>
180         /// The multiplication operator.
181         /// </summary>
182         /// <param name="arg1">The first value.</param>
183         /// <param name="arg2">The second value.</param>
184         /// <returns>The color containing the result of the multiplication.</returns>
185         /// <since_tizen> 3 </since_tizen>
186         public static Color operator*(Color arg1, float arg2)
187         {
188             Color result = arg1.Multiply(arg2);
189             ValueCheck(result);
190             return result;
191         }
192
193         /// <summary>
194         /// The division operator.
195         /// </summary>
196         /// <param name="arg1">The first value.</param>
197         /// <param name="arg2">The second value.</param>
198         /// <returns>The color containing the result of the division.</returns>
199         /// <since_tizen> 3 </since_tizen>
200         public static Color operator /(Color arg1, Color arg2)
201         {
202             Color result = arg1.Divide(arg2);
203             ValueCheck(result);
204             return result;
205         }
206
207         /// <summary>
208         /// The division operator.
209         /// </summary>
210         /// <param name="arg1">The first value.</param>
211         /// <param name="arg2">The second value.</param>
212         /// <returns>The color containing the result of the division.</returns>
213         /// <since_tizen> 3 </since_tizen>
214         public static Color operator/(Color arg1, float arg2)
215         {
216             Color result = arg1.Divide(arg2);
217             ValueCheck(result);
218             return result;
219         }
220
221         /// <summary>
222         /// The array subscript operator overload.
223         /// </summary>
224         /// <param name="index">The subscript index.</param>
225         /// <returns>The float at the given index.</returns>
226         /// <since_tizen> 3 </since_tizen>
227         public float this[uint index]
228         {
229             get
230             {
231                 return ValueOfIndex(index);
232             }
233         }
234
235         internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
236         {
237             Color ret = new Color(cPtr, false);
238             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239             return ret;
240         }
241
242         /// <summary>
243         /// Default constructor
244         /// </summary>
245         /// <since_tizen> 3 </since_tizen>
246         public Color() : this(NDalicPINVOKE.new_Vector4__SWIG_0(), true)
247         {
248             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
249         }
250
251
252         /// <summary>
253         /// The constructor.
254         /// </summary>
255         /// <param name="r">The red component.</param>
256         /// <param name="g">The green component.</param>
257         /// <param name="b">The blue component.</param>
258         /// <param name="a">The alpha component.</param>
259         /// <since_tizen> 3 </since_tizen>
260         public Color(float r, float g, float b, float a) : this(NDalicPINVOKE.new_Vector4__SWIG_1(r, g, b, a), true)
261         {
262             ValueCheck(ref r);
263             ValueCheck(ref g);
264             ValueCheck(ref b);
265             ValueCheck(ref a);
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267         }
268
269         /// <summary>
270         /// The conversion constructor from an array of four floats.
271         /// </summary>
272         /// <param name="array">array Array of R,G,B,A.</param>
273         /// <since_tizen> 3 </since_tizen>
274         public Color(float[] array) : this(NDalicPINVOKE.new_Vector4__SWIG_2(array), true)
275         {
276             ValueCheck(ref array[0]);
277             ValueCheck(ref array[1]);
278             ValueCheck(ref array[2]);
279             ValueCheck(ref array[3]);
280             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
281         }
282
283         private Color Add(Color rhs)
284         {
285             Color ret = new Color(NDalicPINVOKE.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
286             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
287             return ret;
288         }
289
290         private Color AddAssign(Vector4 rhs)
291         {
292             Color ret = new Color(NDalicPINVOKE.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
293             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
294             return ret;
295         }
296
297         private Color Subtract(Color rhs)
298         {
299             Color ret = new Color(NDalicPINVOKE.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
300             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
301             return ret;
302         }
303
304         private Color SubtractAssign(Color rhs)
305         {
306             Color ret = new Color(NDalicPINVOKE.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
307             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
308             return ret;
309         }
310
311         private Color Multiply(Color rhs)
312         {
313             Color ret = new Color(NDalicPINVOKE.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
314             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
315             return ret;
316         }
317
318         private Color Multiply(float rhs)
319         {
320             Color ret = new Color(NDalicPINVOKE.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
321             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
322             return ret;
323         }
324
325         private Color MultiplyAssign(Color rhs)
326         {
327             Color ret = new Color(NDalicPINVOKE.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
328             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
329             return ret;
330         }
331
332         private Color MultiplyAssign(float rhs)
333         {
334             Color ret = new Color(NDalicPINVOKE.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
335             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
336             return ret;
337         }
338
339         private Color Divide(Vector4 rhs)
340         {
341             Color ret = new Color(NDalicPINVOKE.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
342             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
343             return ret;
344         }
345
346         private Color Divide(float rhs)
347         {
348             Color ret = new Color(NDalicPINVOKE.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
349             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
350             return ret;
351         }
352
353         private Color DivideAssign(Color rhs)
354         {
355             Color ret = new Color(NDalicPINVOKE.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
356             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
357             return ret;
358         }
359
360         private Color DivideAssign(float rhs)
361         {
362             Color ret = new Color(NDalicPINVOKE.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
363             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
364             return ret;
365         }
366
367         private Color Subtract()
368         {
369             Color ret = new Color(NDalicPINVOKE.Vector4_Subtract__SWIG_1(swigCPtr), true);
370             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
371             return ret;
372         }
373
374         /// <summary>
375         /// Checks if two color classes are same.
376         /// </summary>
377         /// <param name="rhs">A color to be compared.</param>
378         /// <returns>If two colors are are same, then true.</returns>
379         /// <since_tizen> 3 </since_tizen>
380         public bool EqualTo(Color rhs)
381         {
382             bool ret = NDalicPINVOKE.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
383
384             if (rhs == null) return false;
385
386             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387             return ret;
388         }
389
390         /// <summary>
391         /// Checks if two color classes are different.
392         /// </summary>
393         /// <param name="rhs">A color to be compared.</param>
394         /// <returns>If two colors are are different, then true.</returns>
395         /// <since_tizen> 3 </since_tizen>
396         public bool NotEqualTo(Color rhs)
397         {
398             bool ret = NDalicPINVOKE.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
399             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400             return ret;
401         }
402
403
404         private float ValueOfIndex(uint index)
405         {
406             float ret = NDalicPINVOKE.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
407             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408             return ret;
409         }
410
411         /// <summary>
412         /// The red component.
413         /// </summary>
414         /// <since_tizen> 3 </since_tizen>
415         public float R
416         {
417             set
418             {
419                 ValueCheck(ref value);
420                 NDalicPINVOKE.Vector4_r_set(swigCPtr, value);
421                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422             }
423             get
424             {
425                 float ret = NDalicPINVOKE.Vector4_r_get(swigCPtr);
426                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
427                 return ret;
428             }
429         }
430
431         /// <summary>
432         /// The green component.
433         /// </summary>
434         /// <since_tizen> 3 </since_tizen>
435         public float G
436         {
437             set
438             {
439                 ValueCheck(ref value);
440                 NDalicPINVOKE.Vector4_g_set(swigCPtr, value);
441                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
442             }
443             get
444             {
445                 float ret = NDalicPINVOKE.Vector4_g_get(swigCPtr);
446                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447                 return ret;
448             }
449         }
450
451         /// <summary>
452         /// The blue component.
453         /// </summary>
454         /// <since_tizen> 3 </since_tizen>
455         public float B
456         {
457             set
458             {
459                 ValueCheck(ref value);
460                 NDalicPINVOKE.Vector4_b_set(swigCPtr, value);
461                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
462             }
463             get
464             {
465                 float ret = NDalicPINVOKE.Vector4_b_get(swigCPtr);
466                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
467                 return ret;
468             }
469         }
470
471         /// <summary>
472         /// The alpha component.
473         /// </summary>
474         /// <since_tizen> 3 </since_tizen>
475         public float A
476         {
477             set
478             {
479                 ValueCheck(ref value);
480                 NDalicPINVOKE.Vector4_a_set(swigCPtr, value);
481                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482             }
483             get
484             {
485                 float ret = NDalicPINVOKE.Vector4_a_get(swigCPtr);
486                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
487                 return ret;
488             }
489         }
490
491         /// <summary>
492         /// Gets the black colored Color class.
493         /// </summary>
494         /// <since_tizen> 3 </since_tizen>
495         public static readonly Color Black = new Color(0.0f, 0.0f, 0.0f, 1.0f);
496
497         /// <summary>
498         /// Gets the white colored Color class.
499         /// </summary>
500         /// <since_tizen> 3 </since_tizen>
501         public static readonly Color White = new Color(1.0f, 1.0f, 1.0f, 1.0f);
502
503         /// <summary>
504         /// Gets the red colored Color class.
505         /// </summary>
506         /// <since_tizen> 3 </since_tizen>
507         public static readonly Color Red = new Color(1.0f, 0.0f, 0.0f, 1.0f);
508
509         /// <summary>
510         /// Gets the green colored Color class.
511         /// </summary>
512         /// <since_tizen> 3 </since_tizen>
513         public static readonly Color Green = new Color(0.0f, 1.0f, 0.0f, 1.0f);
514
515         /// <summary>
516         /// Gets the blue colored Color class.
517         /// </summary>
518         /// <since_tizen> 3 </since_tizen>
519         public static readonly Color Blue = new Color(0.0f, 0.0f, 1.0f, 1.0f);
520
521         /// <summary>
522         /// Gets the yellow colored Color class.
523         /// </summary>
524         /// <since_tizen> 3 </since_tizen>
525         public static readonly Color Yellow = new Color(1.0f, 1.0f, 0.0f, 1.0f);
526
527         /// <summary>
528         /// Gets the magenta colored Color class.
529         /// </summary>
530         /// <since_tizen> 3 </since_tizen>
531         public static readonly Color Magenta = new Color(1.0f, 0.0f, 1.0f, 1.0f);
532
533         /// <summary>
534         /// Gets the cyan colored Color class.
535         /// </summary>
536         /// <since_tizen> 3 </since_tizen>
537         public static readonly Color Cyan = new Color(0.0f, 1.0f, 1.0f, 1.0f);
538
539         /// <summary>
540         /// Gets the  transparent colored Color class.
541         /// </summary>
542         /// <since_tizen> 3 </since_tizen>
543         public static readonly Color Transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
544
545         /// <summary>
546         /// Converts the Color class to Vector4 class implicitly.
547         /// </summary>
548         /// <param name="color">A color to be converted to Vector4</param>
549         /// <since_tizen> 3 </since_tizen>
550         public static implicit operator Vector4(Color color)
551         {
552             return new Vector4(color.R, color.G, color.B, color.A);
553         }
554
555         /// <summary>
556         /// Converts Vector4 class to Color class implicitly.
557         /// </summary>
558         /// <param name="vec">A Vector4 to be converted to color.</param>
559         /// <since_tizen> 3 </since_tizen>
560         public static implicit operator Color(Vector4 vec)
561         {
562             return new Color(vec.R, vec.G, vec.B, vec.A);
563         }
564
565         internal static void ValueCheck(Color color)
566         {
567             if (color.R < 0.0f)
568             {
569                 color.R = 0.0f;
570                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
571             }
572             else if (color.R > 1.0f)
573             {
574                 color.R = 1.0f;
575                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
576             }
577             if (color.G < 0.0f)
578             {
579                 color.G = 0.0f;
580                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
581             }
582             else if (color.G > 1.0f)
583             {
584                 color.G = 1.0f;
585                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
586             }
587             if (color.B < 0.0f)
588             {
589                 color.B = 0.0f;
590                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
591             }
592             else if (color.B > 1.0f)
593             {
594                 color.B = 1.0f;
595                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
596             }
597             if (color.A < 0.0f)
598             {
599                 color.A = 0.0f;
600                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
601             }
602             else if (color.A > 1.0f)
603             {
604                 color.A = 1.0f;
605                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
606             }
607         }
608
609         internal static void ValueCheck(ref float value)
610         {
611             if (value < 0.0f)
612             {
613                 value = 0.0f;
614                 Tizen.Log.Fatal("NUI", "The value of Parameters is invalid! Should be between [0, 1].");
615             }
616             else if (value > 1.0f)
617             {
618                 value = 1.0f;
619                 Tizen.Log.Fatal("NUI", "The value of Parameters is invalid! Should be between [0, 1].");
620             }
621         }
622
623     }
624
625 }
626