2 * Copyright(c) 2019 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, ICloneable
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);
85 private readonly bool hashDummy;
88 /// Default constructor
90 /// <since_tizen> 3 </since_tizen>
91 public Color() : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
93 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
100 /// <param name="r">The red component.</param>
101 /// <param name="g">The green component.</param>
102 /// <param name="b">The blue component.</param>
103 /// <param name="a">The alpha component.</param>
104 /// <since_tizen> 3 </since_tizen>
105 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)
107 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
111 /// The conversion constructor from an array of four floats.
113 /// <param name="array">array Array of R,G,B,A.</param>
114 /// <since_tizen> 3 </since_tizen>
115 public Color(float[] array) : this(Interop.Vector4.new_Vector4__SWIG_2(ValueCheck(array)), true)
117 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121 /// The conversion constructor from an hexcode of four floats.
123 /// <param name="hexColor">Hex color code</param>
124 /// <exception cref="ArgumentNullException">This exception is thrown when hexColor is null.</exception>
125 /// <since_tizen> 6 </since_tizen>
126 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
127 [EditorBrowsable(EditorBrowsableState.Never)]
128 public Color(string hexColor) : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
132 if (null == hexColor)
134 throw new ArgumentNullException(nameof(hexColor));
136 hexColor = hexColor.Replace("#", "");
138 R = ((float)Convert.ToInt32(hexColor.Substring(0, 2), 16)) / 255.0f;
139 G = ((float)Convert.ToInt32(hexColor.Substring(2, 2), 16)) / 255.0f;
140 B = ((float)Convert.ToInt32(hexColor.Substring(4, 2), 16)) / 255.0f;
141 A = hexColor.Length > 6 ? ((float)Convert.ToInt32(hexColor.Substring(6, 2), 16)) / 255.0f : 1.0f;
145 throw new global::System.ArgumentException("Please check your hex code");
150 /// The conversion constructor from an System.Drawing.Color instance.
152 /// <param name="color">System.Drawing.Color instance</param>
153 [EditorBrowsable(EditorBrowsableState.Never)]
154 public Color(global::System.Drawing.Color color) : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
156 R = color.R / 255.0f;
157 G = color.G / 255.0f;
158 B = color.B / 255.0f;
159 A = color.A / 255.0f;
163 /// The copy constructor.
165 /// <param name="other">The copy target.</param>
166 [EditorBrowsable(EditorBrowsableState.Never)]
167 public Color(Color other) : this((float)other?.R, (float)other.G, (float)other.B, (float)other.A)
171 internal Color(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
176 internal Color(ColorChangedCallback cb, float r, float g, float b, float a) : this(Interop.Vector4.new_Vector4__SWIG_1(ValueCheck(r), ValueCheck(g), ValueCheck(b), ValueCheck(a)), true)
179 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182 internal Color(ColorChangedCallback cb, Color other) : this(cb, other.R, other.G, other.B, other.A)
186 internal delegate void ColorChangedCallback(float r, float g, float b, float a);
187 private ColorChangedCallback callback = null;
190 /// The red component.
192 /// <since_tizen> 3 </since_tizen>
193 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor")]
198 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
199 Interop.Vector4.Vector4_r_set(swigCPtr, ValueCheck(value));
200 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
202 callback?.Invoke(R, G, B, A);
206 float ret = Interop.Vector4.Vector4_r_get(swigCPtr);
207 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213 /// The green component.
215 /// <since_tizen> 3 </since_tizen>
216 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor")]
221 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
222 Interop.Vector4.Vector4_g_set(swigCPtr, ValueCheck(value));
223 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225 callback?.Invoke(R, G, B, A);
229 float ret = Interop.Vector4.Vector4_g_get(swigCPtr);
230 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236 /// The blue component.
238 /// <since_tizen> 3 </since_tizen>
239 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor")]
244 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
245 Interop.Vector4.Vector4_b_set(swigCPtr, ValueCheck(value));
246 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
248 callback?.Invoke(R, G, B, A);
252 float ret = Interop.Vector4.Vector4_b_get(swigCPtr);
253 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
259 /// The alpha component.
261 /// <since_tizen> 3 </since_tizen>
262 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor")]
267 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
268 Interop.Vector4.Vector4_a_set(swigCPtr, ValueCheck(value));
269 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
271 callback?.Invoke(R, G, B, A);
275 float ret = Interop.Vector4.Vector4_a_get(swigCPtr);
276 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
282 /// The array subscript operator overload.
284 /// <param name="index">The subscript index.</param>
285 /// <returns>The float at the given index.</returns>
286 /// <since_tizen> 3 </since_tizen>
287 public float this[uint index]
291 return ValueOfIndex(index);
296 /// Converts the Color class to Vector4 class implicitly.
298 /// <param name="color">A color to be converted to Vector4</param>
299 /// <since_tizen> 3 </since_tizen>
300 public static implicit operator Vector4(Color color)
302 return new Vector4((float)color?.R, (float)color.G, (float)color.B, (float)color.A);
306 /// Converts Vector4 class to Color class implicitly.
308 /// <param name="vec">A Vector4 to be converted to color.</param>
309 /// <since_tizen> 3 </since_tizen>
310 public static implicit operator Color(Vector4 vec)
312 return new Color((float)vec?.R, (float)vec.G, (float)vec.B, (float)vec.A);
316 /// The addition operator.
318 /// <param name="arg1">The first value.</param>
319 /// <param name="arg2">The second value.</param>
320 /// <returns>The color containing the result of the addition.</returns>
321 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
322 /// <since_tizen> 3 </since_tizen>
323 public static Color operator +(Color arg1, Color arg2)
327 throw new ArgumentNullException(nameof(arg1));
329 Color result = arg1.Add(arg2);
330 return ValueCheck(result);
334 /// The subtraction operator.
336 /// <param name="arg1">The first value.</param>
337 /// <param name="arg2">The second value.</param>
338 /// <returns>The color containing the result of the subtraction.</returns>
339 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
340 /// <since_tizen> 3 </since_tizen>
341 public static Color operator -(Color arg1, Color arg2)
345 throw new ArgumentNullException(nameof(arg1));
347 Color result = arg1.Subtract(arg2);
348 return ValueCheck(result);
352 /// The unary negation operator.
354 /// <param name="arg1">The target value.</param>
355 /// <returns>The color containg the negation.</returns>
356 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
357 /// <since_tizen> 3 </since_tizen>
358 public static Color operator -(Color arg1)
362 throw new ArgumentNullException(nameof(arg1));
364 Color result = arg1.Subtract();
365 return ValueCheck(result);
369 /// The multiplication operator.
371 /// <param name="arg1">The first value.</param>
372 /// <param name="arg2">The second value.</param>
373 /// <returns>The color containing the result of the multiplication.</returns>
374 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
375 /// <since_tizen> 3 </since_tizen>
376 public static Color operator *(Color arg1, Color arg2)
380 throw new ArgumentNullException(nameof(arg1));
382 Color result = arg1.Multiply(arg2);
383 return ValueCheck(result);
387 /// The multiplication operator.
389 /// <param name="arg1">The first value.</param>
390 /// <param name="arg2">The second value.</param>
391 /// <returns>The color containing the result of the multiplication.</returns>
392 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
393 /// <since_tizen> 3 </since_tizen>
394 public static Color operator *(Color arg1, float arg2)
398 throw new ArgumentNullException(nameof(arg1));
400 Color result = arg1.Multiply(arg2);
401 return ValueCheck(result);
405 /// The division operator.
407 /// <param name="arg1">The first value.</param>
408 /// <param name="arg2">The second value.</param>
409 /// <returns>The color containing the result of the division.</returns>
410 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
411 /// <since_tizen> 3 </since_tizen>
412 public static Color operator /(Color arg1, Color arg2)
416 throw new ArgumentNullException(nameof(arg1));
418 Color result = arg1.Divide(arg2);
419 return ValueCheck(result);
423 /// The division operator.
425 /// <param name="arg1">The first value.</param>
426 /// <param name="arg2">The second value.</param>
427 /// <returns>The color containing the result of the division.</returns>
428 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
429 /// <since_tizen> 3 </since_tizen>
430 public static Color operator /(Color arg1, float arg2)
434 throw new ArgumentNullException(nameof(arg1));
436 Color result = arg1.Divide(arg2);
437 return ValueCheck(result);
441 /// Checks if two color classes are same.
443 /// <param name="rhs">A color to be compared.</param>
444 /// <returns>If two colors are are same, then true.</returns>
445 /// <since_tizen> 3 </since_tizen>
446 public bool EqualTo(Color rhs)
448 bool ret = Interop.Vector4.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
450 if (rhs == null) return false;
452 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457 /// Checks if two color classes are different.
459 /// <param name="rhs">A color to be compared.</param>
460 /// <returns>If two colors are are different, then true.</returns>
461 /// <since_tizen> 3 </since_tizen>
462 public bool NotEqualTo(Color rhs)
464 bool ret = Interop.Vector4.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
465 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470 [EditorBrowsable(EditorBrowsableState.Never)]
471 public object Clone() => new Color(this);
473 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
475 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
478 internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
480 Color ret = new Color(cPtr, false);
481 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
485 internal static Color ValueCheck(Color color)
490 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
492 else if (color.R > 1.0f)
495 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
500 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
502 else if (color.G > 1.0f)
505 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
510 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
512 else if (color.B > 1.0f)
515 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
520 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
522 else if (color.A > 1.0f)
525 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
530 internal static float ValueCheck(float value)
535 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
537 else if (value > 1.0f)
540 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
545 internal static float[] ValueCheck(float[] arr)
549 throw new ArgumentNullException(nameof(arr));
552 for (int i = 0; i < arr.Length; i++)
557 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
559 else if (arr[i] > 1.0f)
562 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
568 /// This will not be public opened.
569 [EditorBrowsable(EditorBrowsableState.Never)]
570 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
572 Interop.Vector4.delete_Vector4(swigCPtr);
575 private Color Add(Color rhs)
577 Color ret = new Color(Interop.Vector4.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
578 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
582 private Color AddAssign(Vector4 rhs)
584 Color ret = new Color(Interop.Vector4.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
585 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
589 private Color Subtract(Color rhs)
591 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
592 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
596 private Color SubtractAssign(Color rhs)
598 Color ret = new Color(Interop.Vector4.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
599 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
603 private Color Multiply(Color rhs)
605 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
606 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
610 private Color Multiply(float rhs)
612 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
613 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
617 private Color MultiplyAssign(Color rhs)
619 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
620 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
624 private Color MultiplyAssign(float rhs)
626 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
627 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
631 private Color Divide(Vector4 rhs)
633 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
634 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
638 private Color Divide(float rhs)
640 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
641 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
645 private Color DivideAssign(Color rhs)
647 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
648 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
652 private Color DivideAssign(float rhs)
654 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
655 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
659 private Color Subtract()
661 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_1(swigCPtr), true);
662 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
666 private static bool EqualsColorValue(float f1, float f2)
668 float EPS = (float)Math.Abs(f1 * .00001);
669 if (Math.Abs(f1 - f2) <= EPS)
679 private static bool EqualsColor(Color c1, Color c2)
681 return EqualsColorValue(c1.R, c2.R) && EqualsColorValue(c1.G, c2.G)
682 && EqualsColorValue(c1.B, c2.B) && EqualsColorValue(c1.A, c2.A);
686 /// Determines whether the specified object is equal to the current object.
688 /// <param name="obj">The object to compare with the current object.</param>
689 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
690 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
691 [EditorBrowsable(EditorBrowsableState.Never)]
692 public override bool Equals(System.Object obj)
694 Color color = obj as Color;
701 if (EqualsColor(this, color))
708 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
709 [EditorBrowsable(EditorBrowsableState.Never)]
710 public override int GetHashCode()
712 return hashDummy.GetHashCode();
715 private float ValueOfIndex(uint index)
717 float ret = Interop.Vector4.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
718 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();