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