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