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