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.NewVector4(), 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.NewVector4(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.NewVector4(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.NewVector4(), 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.NewVector4(), 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.NewVector4(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.
193 /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
196 /// // DO NOT use like the followings!
197 /// Color color = new Color();
199 /// // Please USE like this
200 /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
201 /// Color color = new Color(r, g, b, a);
203 /// <since_tizen> 3 </since_tizen>
208 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
209 Interop.Vector4.RSet(SwigCPtr, ValueCheck(value));
210 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212 callback?.Invoke(R, G, B, A);
216 float ret = Interop.Vector4.RGet(SwigCPtr);
217 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
223 /// The green component.
226 /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
229 /// // DO NOT use like the followings!
230 /// Color color = new Color();
232 /// // Please USE like this
233 /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
234 /// Color color = new Color(r, g, b, a);
236 /// <since_tizen> 3 </since_tizen>
241 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
242 Interop.Vector4.GSet(SwigCPtr, ValueCheck(value));
243 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
245 callback?.Invoke(R, G, B, A);
249 float ret = Interop.Vector4.GGet(SwigCPtr);
250 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
256 /// The blue component.
259 /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
262 /// // DO NOT use like the followings!
263 /// Color color = new Color();
265 /// // Please USE like this
266 /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
267 /// Color color = new Color(r, g, b, a);
269 /// <since_tizen> 3 </since_tizen>
274 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
275 Interop.Vector4.BSet(SwigCPtr, ValueCheck(value));
276 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
278 callback?.Invoke(R, G, B, A);
282 float ret = Interop.Vector4.BGet(SwigCPtr);
283 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
289 /// The alpha component.
292 /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
295 /// // DO NOT use like the followings!
296 /// Color color = new Color();
298 /// // Please USE like this
299 /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
300 /// Color color = new Color(r, g, b, a);
302 /// <since_tizen> 3 </since_tizen>
307 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
308 Interop.Vector4.ASet(SwigCPtr, ValueCheck(value));
309 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
311 callback?.Invoke(R, G, B, A);
315 float ret = Interop.Vector4.AGet(SwigCPtr);
316 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
322 /// The array subscript operator overload.
324 /// <param name="index">The subscript index.</param>
325 /// <returns>The float at the given index.</returns>
326 /// <since_tizen> 3 </since_tizen>
327 public float this[uint index]
331 return ValueOfIndex(index);
336 /// Converts the Color class to Vector4 class implicitly.
338 /// <param name="color">A color to be converted to Vector4</param>
339 /// <since_tizen> 3 </since_tizen>
340 public static implicit operator Vector4(Color color)
342 return new Vector4((float)color?.R, (float)color.G, (float)color.B, (float)color.A);
346 /// Converts Vector4 class to Color class implicitly.
348 /// <param name="vec">A Vector4 to be converted to color.</param>
349 /// <since_tizen> 3 </since_tizen>
350 public static implicit operator Color(Vector4 vec)
352 return new Color((float)vec?.R, (float)vec.G, (float)vec.B, (float)vec.A);
356 /// The addition operator.
358 /// <param name="arg1">The first value.</param>
359 /// <param name="arg2">The second value.</param>
360 /// <returns>The color containing the result of the addition.</returns>
361 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
362 /// <since_tizen> 3 </since_tizen>
363 public static Color operator +(Color arg1, Color arg2)
367 throw new ArgumentNullException(nameof(arg1));
369 Color result = arg1.Add(arg2);
370 return ValueCheck(result);
374 /// The subtraction operator.
376 /// <param name="arg1">The first value.</param>
377 /// <param name="arg2">The second value.</param>
378 /// <returns>The color containing the result of the subtraction.</returns>
379 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
380 /// <since_tizen> 3 </since_tizen>
381 public static Color operator -(Color arg1, Color arg2)
385 throw new ArgumentNullException(nameof(arg1));
387 Color result = arg1.Subtract(arg2);
388 return ValueCheck(result);
392 /// The unary negation operator.
394 /// <param name="arg1">The target value.</param>
395 /// <returns>The color containg the negation.</returns>
396 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
397 /// <since_tizen> 3 </since_tizen>
398 public static Color operator -(Color arg1)
402 throw new ArgumentNullException(nameof(arg1));
404 Color result = arg1.Subtract();
405 return ValueCheck(result);
409 /// The multiplication operator.
411 /// <param name="arg1">The first value.</param>
412 /// <param name="arg2">The second value.</param>
413 /// <returns>The color containing the result of the multiplication.</returns>
414 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
415 /// <since_tizen> 3 </since_tizen>
416 public static Color operator *(Color arg1, Color arg2)
420 throw new ArgumentNullException(nameof(arg1));
422 Color result = arg1.Multiply(arg2);
423 return ValueCheck(result);
427 /// The multiplication operator.
429 /// <param name="arg1">The first value.</param>
430 /// <param name="arg2">The second value.</param>
431 /// <returns>The color containing the result of the multiplication.</returns>
432 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
433 /// <since_tizen> 3 </since_tizen>
434 public static Color operator *(Color arg1, float arg2)
438 throw new ArgumentNullException(nameof(arg1));
440 Color result = arg1.Multiply(arg2);
441 return ValueCheck(result);
445 /// The division operator.
447 /// <param name="arg1">The first value.</param>
448 /// <param name="arg2">The second value.</param>
449 /// <returns>The color containing the result of the division.</returns>
450 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
451 /// <since_tizen> 3 </since_tizen>
452 public static Color operator /(Color arg1, Color arg2)
456 throw new ArgumentNullException(nameof(arg1));
458 Color result = arg1.Divide(arg2);
459 return ValueCheck(result);
463 /// The division operator.
465 /// <param name="arg1">The first value.</param>
466 /// <param name="arg2">The second value.</param>
467 /// <returns>The color containing the result of the division.</returns>
468 /// <exception cref="ArgumentNullException"> Thrown when arg1 is null. </exception>
469 /// <since_tizen> 3 </since_tizen>
470 public static Color operator /(Color arg1, float arg2)
474 throw new ArgumentNullException(nameof(arg1));
476 Color result = arg1.Divide(arg2);
477 return ValueCheck(result);
481 /// Checks if two color classes are same.
483 /// <param name="rhs">A color to be compared.</param>
484 /// <returns>If two colors are are same, then true.</returns>
485 /// <since_tizen> 3 </since_tizen>
486 public bool EqualTo(Color rhs)
488 bool ret = Interop.Vector4.EqualTo(SwigCPtr, Color.getCPtr(rhs));
490 if (rhs == null) return false;
492 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
497 /// Checks if two color classes are different.
499 /// <param name="rhs">A color to be compared.</param>
500 /// <returns>If two colors are are different, then true.</returns>
501 /// <since_tizen> 3 </since_tizen>
502 public bool NotEqualTo(Color rhs)
504 bool ret = Interop.Vector4.NotEqualTo(SwigCPtr, Color.getCPtr(rhs));
505 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
510 [EditorBrowsable(EditorBrowsableState.Never)]
511 public object Clone() => new Color(this);
513 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
515 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
518 internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
520 Color ret = new Color(cPtr, false);
521 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
525 internal static Color ValueCheck(Color color)
532 if (IsInvalidValue(ref r) | IsInvalidValue(ref g) | IsInvalidValue(ref b) | IsInvalidValue(ref a))
534 NUILog.Error($"The value of Result is invalid! Should be between [0, 1]. Color is {color.R}, {color.G}, {color.B}, {color.A}");
536 color = new Color(r, g, b, a);
540 internal static float ValueCheck(float value)
542 float refValue = value;
543 if (IsInvalidValue(ref refValue))
545 NUILog.Error($"The value of Result is invalid! Should be between [0, 1]. float value is {value}");
550 internal static float[] ValueCheck(float[] arr)
554 throw new ArgumentNullException(nameof(arr));
557 for (int i = 0; i < arr.Length; i++)
559 float refValue = arr[i];
560 if (IsInvalidValue(ref refValue))
562 NUILog.Error($"The value of Result is invalid! Should be between [0, 1]. arr[] is {arr[i]}");
569 private static bool IsInvalidValue(ref float value)
576 else if (value > 1.0f)
584 /// This will not be public opened.
585 [EditorBrowsable(EditorBrowsableState.Never)]
586 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
588 Interop.Vector4.DeleteVector4(swigCPtr);
591 private Color Add(Color rhs)
593 Color ret = new Color(Interop.Vector4.Add(SwigCPtr, Color.getCPtr(rhs)), true);
594 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
598 private Color AddAssign(Vector4 rhs)
600 Color ret = new Color(Interop.Vector4.AddAssign(SwigCPtr, Color.getCPtr(rhs)), false);
601 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
605 private Color Subtract(Color rhs)
607 Color ret = new Color(Interop.Vector4.Subtract(SwigCPtr, Color.getCPtr(rhs)), true);
608 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
612 private Color SubtractAssign(Color rhs)
614 Color ret = new Color(Interop.Vector4.SubtractAssign(SwigCPtr, Color.getCPtr(rhs)), false);
615 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
619 private Color Multiply(Color rhs)
621 Color ret = new Color(Interop.Vector4.Multiply(SwigCPtr, Color.getCPtr(rhs)), true);
622 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
626 private Color Multiply(float rhs)
628 Color ret = new Color(Interop.Vector4.Multiply(SwigCPtr, rhs), true);
629 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
633 private Color MultiplyAssign(Color rhs)
635 Color ret = new Color(Interop.Vector4.MultiplyAssign(SwigCPtr, Color.getCPtr(rhs)), false);
636 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
640 private Color MultiplyAssign(float rhs)
642 Color ret = new Color(Interop.Vector4.MultiplyAssign(SwigCPtr, rhs), false);
643 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
647 private Color Divide(Vector4 rhs)
649 Color ret = new Color(Interop.Vector4.Divide(SwigCPtr, Color.getCPtr(rhs)), true);
650 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
654 private Color Divide(float rhs)
656 Color ret = new Color(Interop.Vector4.Divide(SwigCPtr, rhs), true);
657 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
661 private Color DivideAssign(Color rhs)
663 Color ret = new Color(Interop.Vector4.DivideAssign(SwigCPtr, Color.getCPtr(rhs)), false);
664 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
668 private Color DivideAssign(float rhs)
670 Color ret = new Color(Interop.Vector4.DivideAssign(SwigCPtr, rhs), false);
671 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
675 private Color Subtract()
677 Color ret = new Color(Interop.Vector4.Subtract(SwigCPtr), true);
678 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
682 private static bool EqualsColorValue(float f1, float f2)
684 float EPS = (float)Math.Abs(f1 * .00001);
685 if (Math.Abs(f1 - f2) <= EPS)
695 private static bool EqualsColor(Color c1, Color c2)
697 return EqualsColorValue(c1.R, c2.R) && EqualsColorValue(c1.G, c2.G)
698 && EqualsColorValue(c1.B, c2.B) && EqualsColorValue(c1.A, c2.A);
702 /// Determines whether the specified object is equal to the current object.
704 /// <param name="obj">The object to compare with the current object.</param>
705 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
706 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
707 [EditorBrowsable(EditorBrowsableState.Never)]
708 public override bool Equals(System.Object obj)
710 Color color = obj as Color;
717 if (EqualsColor(this, color))
724 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
725 [EditorBrowsable(EditorBrowsableState.Never)]
726 public override int GetHashCode()
728 return hashDummy.GetHashCode();
731 private float ValueOfIndex(uint index)
733 float ret = Interop.Vector4.ValueOfIndex(SwigCPtr, index);
734 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();