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