2 * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using Tizen.NUI.Binding;
27 [TypeConverter(typeof(ColorTypeConverter))]
28 public class Color : global::System.IDisposable
30 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
34 /// <since_tizen> 3 </since_tizen>
35 protected bool swigCMemOwn;
37 internal Color(global::System.IntPtr cPtr, bool cMemoryOwn)
39 swigCMemOwn = cMemoryOwn;
40 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
43 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
45 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
48 //A Flag to check who called Dispose(). (By User or DisposeQueue)
49 private bool isDisposeQueued = false;
52 /// A Flat to check if it is already disposed.
54 /// <since_tizen> 3 </since_tizen>
55 protected bool disposed = false;
60 /// <since_tizen> 3 </since_tizen>
65 isDisposeQueued = true;
66 DisposeQueue.Instance.Add(this);
71 /// To make a color instance be disposed.
73 /// <since_tizen> 3 </since_tizen>
76 //Throw excpetion if Dispose() is called in separate thread.
77 if (!Window.IsInstalled())
79 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
84 Dispose(DisposeTypes.Implicit);
88 Dispose(DisposeTypes.Explicit);
89 System.GC.SuppressFinalize(this);
96 /// <since_tizen> 3 </since_tizen>
97 protected virtual void Dispose(DisposeTypes type)
104 if(type == DisposeTypes.Explicit)
107 //Release your own managed resources here.
108 //You should release all of your own disposable objects here.
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.
115 if (swigCPtr.Handle != global::System.IntPtr.Zero)
120 NDalicPINVOKE.delete_Vector4(swigCPtr);
122 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
128 /// The addition operator.
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)
136 Color result = arg1.Add(arg2);
137 return ValueCheck(result);
141 /// The subtraction operator.
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)
149 Color result = arg1.Subtract(arg2);
150 return ValueCheck(result);
154 /// The unary negation operator.
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)
161 Color result = arg1.Subtract();
162 return ValueCheck(result);
166 /// The multiplication operator.
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)
174 Color result = arg1.Multiply(arg2);
175 return ValueCheck(result);
179 /// The multiplication operator.
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)
187 Color result = arg1.Multiply(arg2);
188 return ValueCheck(result);
192 /// The division operator.
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)
200 Color result = arg1.Divide(arg2);
201 return ValueCheck(result);
205 /// The division operator.
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)
213 Color result = arg1.Divide(arg2);
214 return ValueCheck(result);
218 /// The array subscript operator overload.
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]
227 return ValueOfIndex(index);
231 internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
233 Color ret = new Color(cPtr, false);
234 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
239 /// Default constructor
241 /// <since_tizen> 3 </since_tizen>
242 public Color() : this(NDalicPINVOKE.new_Vector4__SWIG_0(), true)
244 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
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)
258 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
262 /// The conversion constructor from an array of four floats.
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)
268 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
271 private Color Add(Color rhs)
273 Color ret = new Color(NDalicPINVOKE.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
274 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
278 private Color AddAssign(Vector4 rhs)
280 Color ret = new Color(NDalicPINVOKE.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
281 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
285 private Color Subtract(Color rhs)
287 Color ret = new Color(NDalicPINVOKE.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
288 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
292 private Color SubtractAssign(Color rhs)
294 Color ret = new Color(NDalicPINVOKE.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
295 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
299 private Color Multiply(Color rhs)
301 Color ret = new Color(NDalicPINVOKE.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
302 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
306 private Color Multiply(float rhs)
308 Color ret = new Color(NDalicPINVOKE.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
309 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
313 private Color MultiplyAssign(Color rhs)
315 Color ret = new Color(NDalicPINVOKE.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
316 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
320 private Color MultiplyAssign(float rhs)
322 Color ret = new Color(NDalicPINVOKE.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
323 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327 private Color Divide(Vector4 rhs)
329 Color ret = new Color(NDalicPINVOKE.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
330 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
334 private Color Divide(float rhs)
336 Color ret = new Color(NDalicPINVOKE.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
337 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
341 private Color DivideAssign(Color rhs)
343 Color ret = new Color(NDalicPINVOKE.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
344 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
348 private Color DivideAssign(float rhs)
350 Color ret = new Color(NDalicPINVOKE.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
351 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355 private Color Subtract()
357 Color ret = new Color(NDalicPINVOKE.Vector4_Subtract__SWIG_1(swigCPtr), true);
358 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
363 /// Checks if two color classes are same.
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)
370 bool ret = NDalicPINVOKE.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
372 if (rhs == null) return false;
374 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
379 /// Checks if two color classes are different.
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)
386 bool ret = NDalicPINVOKE.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
387 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
392 private float ValueOfIndex(uint index)
394 float ret = NDalicPINVOKE.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
395 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400 /// The red component.
402 /// <since_tizen> 3 </since_tizen>
407 NDalicPINVOKE.Vector4_r_set(swigCPtr, ValueCheck(value));
408 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
412 float ret = NDalicPINVOKE.Vector4_r_get(swigCPtr);
413 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419 /// The green component.
421 /// <since_tizen> 3 </since_tizen>
426 NDalicPINVOKE.Vector4_g_set(swigCPtr, ValueCheck(value));
427 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
431 float ret = NDalicPINVOKE.Vector4_g_get(swigCPtr);
432 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
438 /// The blue component.
440 /// <since_tizen> 3 </since_tizen>
445 NDalicPINVOKE.Vector4_b_set(swigCPtr, ValueCheck(value));
446 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
450 float ret = NDalicPINVOKE.Vector4_b_get(swigCPtr);
451 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457 /// The alpha component.
459 /// <since_tizen> 3 </since_tizen>
464 NDalicPINVOKE.Vector4_a_set(swigCPtr, ValueCheck(value));
465 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
469 float ret = NDalicPINVOKE.Vector4_a_get(swigCPtr);
470 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
476 /// Gets the black colored Color class.
478 /// <since_tizen> 3 </since_tizen>
479 public static readonly Color Black = new Color(0.0f, 0.0f, 0.0f, 1.0f);
482 /// Gets the white colored Color class.
484 /// <since_tizen> 3 </since_tizen>
485 public static readonly Color White = new Color(1.0f, 1.0f, 1.0f, 1.0f);
488 /// Gets the red colored Color class.
490 /// <since_tizen> 3 </since_tizen>
491 public static readonly Color Red = new Color(1.0f, 0.0f, 0.0f, 1.0f);
494 /// Gets the green colored Color class.
496 /// <since_tizen> 3 </since_tizen>
497 public static readonly Color Green = new Color(0.0f, 1.0f, 0.0f, 1.0f);
500 /// Gets the blue colored Color class.
502 /// <since_tizen> 3 </since_tizen>
503 public static readonly Color Blue = new Color(0.0f, 0.0f, 1.0f, 1.0f);
506 /// Gets the yellow colored Color class.
508 /// <since_tizen> 3 </since_tizen>
509 public static readonly Color Yellow = new Color(1.0f, 1.0f, 0.0f, 1.0f);
512 /// Gets the magenta colored Color class.
514 /// <since_tizen> 3 </since_tizen>
515 public static readonly Color Magenta = new Color(1.0f, 0.0f, 1.0f, 1.0f);
518 /// Gets the cyan colored Color class.
520 /// <since_tizen> 3 </since_tizen>
521 public static readonly Color Cyan = new Color(0.0f, 1.0f, 1.0f, 1.0f);
524 /// Gets the transparent colored Color class.
526 /// <since_tizen> 3 </since_tizen>
527 public static readonly Color Transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
530 /// Converts the Color class to Vector4 class implicitly.
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)
536 return new Vector4(color.R, color.G, color.B, color.A);
540 /// Converts Vector4 class to Color class implicitly.
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)
546 return new Color(vec.R, vec.G, vec.B, vec.A);
549 internal static Color ValueCheck(Color color)
554 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
556 else if (color.R > 1.0f)
559 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
564 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
566 else if (color.G > 1.0f)
569 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
574 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
576 else if (color.B > 1.0f)
579 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
584 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
586 else if (color.A > 1.0f)
589 NUILog.Error( "The value of Result is invalid! Should be between [0, 1].");
594 internal static float ValueCheck(float value)
599 NUILog.Error( "The value of Parameters is invalid! Should be between [0, 1].");
601 else if (value > 1.0f)
604 NUILog.Error( "The value of Parameters is invalid! Should be between [0, 1].");
609 internal static float[] ValueCheck(float[] arr)
611 for(int i = 0; i < arr.Length; i++)
616 NUILog.Error( "The value of Parameters is invalid! Should be between [0, 1].");
618 else if (arr[i] > 1.0f)
621 NUILog.Error( "The value of Parameters is invalid! Should be between [0, 1].");