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;
20 using System.ComponentModel;
28 [Tizen.NUI.Binding.TypeConverter(typeof(ColorTypeConverter))]
29 public class Color : Disposable
32 /// Gets the black colored Color class.
34 /// <since_tizen> 3 </since_tizen>
35 public static readonly Color Black = new Color(0.0f, 0.0f, 0.0f, 1.0f);
38 /// Gets the white colored Color class.
40 /// <since_tizen> 3 </since_tizen>
41 public static readonly Color White = new Color(1.0f, 1.0f, 1.0f, 1.0f);
44 /// Gets the red colored Color class.
46 /// <since_tizen> 3 </since_tizen>
47 public static readonly Color Red = new Color(1.0f, 0.0f, 0.0f, 1.0f);
50 /// Gets the green colored Color class.
52 /// <since_tizen> 3 </since_tizen>
53 public static readonly Color Green = new Color(0.0f, 1.0f, 0.0f, 1.0f);
56 /// Gets the blue colored Color class.
58 /// <since_tizen> 3 </since_tizen>
59 public static readonly Color Blue = new Color(0.0f, 0.0f, 1.0f, 1.0f);
62 /// Gets the yellow colored Color class.
64 /// <since_tizen> 3 </since_tizen>
65 public static readonly Color Yellow = new Color(1.0f, 1.0f, 0.0f, 1.0f);
68 /// Gets the magenta colored Color class.
70 /// <since_tizen> 3 </since_tizen>
71 public static readonly Color Magenta = new Color(1.0f, 0.0f, 1.0f, 1.0f);
74 /// Gets the cyan colored Color class.
76 /// <since_tizen> 3 </since_tizen>
77 public static readonly Color Cyan = new Color(0.0f, 1.0f, 1.0f, 1.0f);
80 /// Gets the transparent colored Color class.
82 /// <since_tizen> 3 </since_tizen>
83 public static readonly Color Transparent = new Color(0.0f, 0.0f, 0.0f, 0.0f);
88 /// <since_tizen> 3 </since_tizen>
89 protected bool swigCMemOwn;
91 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
92 private readonly bool hashDummy;
95 /// Default constructor
97 /// <since_tizen> 3 </since_tizen>
98 public Color() : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
100 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
107 /// <param name="r">The red component.</param>
108 /// <param name="g">The green component.</param>
109 /// <param name="b">The blue component.</param>
110 /// <param name="a">The alpha component.</param>
111 /// <since_tizen> 3 </since_tizen>
112 public Color(float r, float g, float b, float a) : this(Interop.Vector4.new_Vector4__SWIG_1(ValueCheck(r), ValueCheck(g), ValueCheck(b), ValueCheck(a)), true)
114 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
118 /// The conversion constructor from an array of four floats.
120 /// <param name="array">array Array of R,G,B,A.</param>
121 /// <since_tizen> 3 </since_tizen>
122 public Color(float[] array) : this(Interop.Vector4.new_Vector4__SWIG_2(ValueCheck(array)), true)
124 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
127 internal Color(global::System.IntPtr cPtr, bool cMemoryOwn)
129 swigCMemOwn = cMemoryOwn;
130 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
135 /// The red component.
137 /// <since_tizen> 3 </since_tizen>
142 Interop.Vector4.Vector4_r_set(swigCPtr, ValueCheck(value));
143 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
147 float ret = Interop.Vector4.Vector4_r_get(swigCPtr);
148 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
154 /// The green component.
156 /// <since_tizen> 3 </since_tizen>
161 Interop.Vector4.Vector4_g_set(swigCPtr, ValueCheck(value));
162 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
166 float ret = Interop.Vector4.Vector4_g_get(swigCPtr);
167 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
173 /// The blue component.
175 /// <since_tizen> 3 </since_tizen>
180 Interop.Vector4.Vector4_b_set(swigCPtr, ValueCheck(value));
181 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
185 float ret = Interop.Vector4.Vector4_b_get(swigCPtr);
186 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192 /// The alpha component.
194 /// <since_tizen> 3 </since_tizen>
199 Interop.Vector4.Vector4_a_set(swigCPtr, ValueCheck(value));
200 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204 float ret = Interop.Vector4.Vector4_a_get(swigCPtr);
205 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
211 /// The array subscript operator overload.
213 /// <param name="index">The subscript index.</param>
214 /// <returns>The float at the given index.</returns>
215 /// <since_tizen> 3 </since_tizen>
216 public float this[uint index]
220 return ValueOfIndex(index);
225 /// Converts the Color class to Vector4 class implicitly.
227 /// <param name="color">A color to be converted to Vector4</param>
228 /// <since_tizen> 3 </since_tizen>
229 public static implicit operator Vector4(Color color)
231 return new Vector4(color.R, color.G, color.B, color.A);
235 /// Converts Vector4 class to Color class implicitly.
237 /// <param name="vec">A Vector4 to be converted to color.</param>
238 /// <since_tizen> 3 </since_tizen>
239 public static implicit operator Color(Vector4 vec)
241 return new Color(vec.R, vec.G, vec.B, vec.A);
245 /// The addition operator.
247 /// <param name="arg1">The first value.</param>
248 /// <param name="arg2">The second value.</param>
249 /// <returns>The color containing the result of the addition.</returns>
250 /// <since_tizen> 3 </since_tizen>
251 public static Color operator +(Color arg1, Color arg2)
253 Color result = arg1.Add(arg2);
254 return ValueCheck(result);
258 /// The subtraction operator.
260 /// <param name="arg1">The first value.</param>
261 /// <param name="arg2">The second value.</param>
262 /// <returns>The color containing the result of the subtraction.</returns>
263 /// <since_tizen> 3 </since_tizen>
264 public static Color operator -(Color arg1, Color arg2)
266 Color result = arg1.Subtract(arg2);
267 return ValueCheck(result);
271 /// The unary negation operator.
273 /// <param name="arg1">The target value.</param>
274 /// <returns>The color containg the negation.</returns>
275 /// <since_tizen> 3 </since_tizen>
276 public static Color operator -(Color arg1)
278 Color result = arg1.Subtract();
279 return ValueCheck(result);
283 /// The multiplication operator.
285 /// <param name="arg1">The first value.</param>
286 /// <param name="arg2">The second value.</param>
287 /// <returns>The color containing the result of the multiplication.</returns>
288 /// <since_tizen> 3 </since_tizen>
289 public static Color operator *(Color arg1, Color arg2)
291 Color result = arg1.Multiply(arg2);
292 return ValueCheck(result);
296 /// The multiplication operator.
298 /// <param name="arg1">The first value.</param>
299 /// <param name="arg2">The second value.</param>
300 /// <returns>The color containing the result of the multiplication.</returns>
301 /// <since_tizen> 3 </since_tizen>
302 public static Color operator *(Color arg1, float arg2)
304 Color result = arg1.Multiply(arg2);
305 return ValueCheck(result);
309 /// The division operator.
311 /// <param name="arg1">The first value.</param>
312 /// <param name="arg2">The second value.</param>
313 /// <returns>The color containing the result of the division.</returns>
314 /// <since_tizen> 3 </since_tizen>
315 public static Color operator /(Color arg1, Color arg2)
317 Color result = arg1.Divide(arg2);
318 return ValueCheck(result);
322 /// The division operator.
324 /// <param name="arg1">The first value.</param>
325 /// <param name="arg2">The second value.</param>
326 /// <returns>The color containing the result of the division.</returns>
327 /// <since_tizen> 3 </since_tizen>
328 public static Color operator /(Color arg1, float arg2)
330 Color result = arg1.Divide(arg2);
331 return ValueCheck(result);
335 /// Checks if two color classes are same.
337 /// <param name="rhs">A color to be compared.</param>
338 /// <returns>If two colors are are same, then true.</returns>
339 /// <since_tizen> 3 </since_tizen>
340 public bool EqualTo(Color rhs)
342 bool ret = Interop.Vector4.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
344 if (rhs == null) return false;
346 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
351 /// Checks if two color classes are different.
353 /// <param name="rhs">A color to be compared.</param>
354 /// <returns>If two colors are are different, then true.</returns>
355 /// <since_tizen> 3 </since_tizen>
356 public bool NotEqualTo(Color rhs)
358 bool ret = Interop.Vector4.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
359 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
363 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
365 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
368 internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
370 Color ret = new Color(cPtr, false);
371 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375 internal static Color ValueCheck(Color color)
380 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
382 else if (color.R > 1.0f)
385 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
390 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
392 else if (color.G > 1.0f)
395 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
400 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
402 else if (color.B > 1.0f)
405 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
410 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
412 else if (color.A > 1.0f)
415 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
420 internal static float ValueCheck(float value)
425 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
427 else if (value > 1.0f)
430 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
435 internal static float[] ValueCheck(float[] arr)
437 for (int i = 0; i < arr.Length; i++)
442 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
444 else if (arr[i] > 1.0f)
447 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
456 /// <since_tizen> 3 </since_tizen>
457 protected override void Dispose(DisposeTypes type)
464 //Release your own unmanaged resources here.
465 //You should not access any managed member here except static instance.
466 //because the execution order of Finalizes is non-deterministic.
468 if (swigCPtr.Handle != global::System.IntPtr.Zero)
473 Interop.Vector4.delete_Vector4(swigCPtr);
475 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
480 private Color Add(Color rhs)
482 Color ret = new Color(Interop.Vector4.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
483 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
487 private Color AddAssign(Vector4 rhs)
489 Color ret = new Color(Interop.Vector4.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
490 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
494 private Color Subtract(Color rhs)
496 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
497 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
501 private Color SubtractAssign(Color rhs)
503 Color ret = new Color(Interop.Vector4.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
504 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
508 private Color Multiply(Color rhs)
510 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
511 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
515 private Color Multiply(float rhs)
517 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
518 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
522 private Color MultiplyAssign(Color rhs)
524 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
525 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
529 private Color MultiplyAssign(float rhs)
531 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
532 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
536 private Color Divide(Vector4 rhs)
538 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
539 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
543 private Color Divide(float rhs)
545 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
546 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
550 private Color DivideAssign(Color rhs)
552 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
553 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
557 private Color DivideAssign(float rhs)
559 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
560 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
564 private Color Subtract()
566 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_1(swigCPtr), true);
567 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
571 private static bool EqualsColorValue(float f1, float f2)
573 float EPS = (float)Math.Abs(f1 * .00001);
574 if(Math.Abs(f1 - f2) <= EPS)
584 private static bool EqualsColor(Color c1, Color c2)
586 return EqualsColorValue(c1.R, c2.R) && EqualsColorValue(c1.G, c2.G)
587 && EqualsColorValue(c1.B, c2.B) && EqualsColorValue(c1.A, c2.A);
591 /// Determines whether the specified object is equal to the current object.
593 /// <param name="obj">The object to compare with the current object.</param>
594 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
595 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
596 [EditorBrowsable(EditorBrowsableState.Never)]
597 public override bool Equals(System.Object obj)
599 Color color = obj as Color;
606 if (EqualsColor(this, color))
613 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
614 [EditorBrowsable(EditorBrowsableState.Never)]
615 public override int GetHashCode()
617 return hashDummy.GetHashCode();
620 private float ValueOfIndex(uint index)
622 float ret = Interop.Vector4.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
623 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();