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 /// <since_tizen> 6 </since_tizen>
125 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
126 [EditorBrowsable(EditorBrowsableState.Never)]
127 public Color(string hexColor) : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
131 hexColor = hexColor.Replace("#", "");
133 R = ((float)Convert.ToInt32(hexColor.Substring(0,2), 16))/255.0f;
134 G = ((float)Convert.ToInt32(hexColor.Substring(2,2), 16))/255.0f;
135 B = ((float)Convert.ToInt32(hexColor.Substring(4,2), 16))/255.0f;
136 A = hexColor.Length > 6 ? ((float)Convert.ToInt32(hexColor.Substring(6,2), 16))/255.0f : 1.0f;
140 throw new global::System.ArgumentException("Please check your hex code");
145 /// The conversion constructor from an System.Drawing.Color instance.
147 /// <param name="color">System.Drawing.Color instance</param>
148 [EditorBrowsable(EditorBrowsableState.Never)]
149 public Color(global::System.Drawing.Color color) : this(Interop.Vector4.new_Vector4__SWIG_0(), true)
151 R = color.R / 255.0f;
152 G = color.G / 255.0f;
153 B = color.B / 255.0f;
154 A = color.A / 255.0f;
158 /// The copy constructor.
160 /// <param name="other">The copy target.</param>
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public Color(Color other) : this(other.R, other.G, other.B, other.A)
166 internal Color(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
171 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)
174 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
177 internal Color(ColorChangedCallback cb, Color other) : this(cb, other.R, other.G, other.B, other.A)
181 internal delegate void ColorChangedCallback(float r, float g, float b, float a);
182 private ColorChangedCallback callback = null;
185 /// The red component.
187 /// <since_tizen> 3 </since_tizen>
192 Interop.Vector4.Vector4_r_set(swigCPtr, ValueCheck(value));
193 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
195 callback?.Invoke(R, G, B, A);
199 float ret = Interop.Vector4.Vector4_r_get(swigCPtr);
200 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206 /// The green component.
208 /// <since_tizen> 3 </since_tizen>
213 Interop.Vector4.Vector4_g_set(swigCPtr, ValueCheck(value));
214 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216 callback?.Invoke(R, G, B, A);
220 float ret = Interop.Vector4.Vector4_g_get(swigCPtr);
221 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227 /// The blue component.
229 /// <since_tizen> 3 </since_tizen>
234 Interop.Vector4.Vector4_b_set(swigCPtr, ValueCheck(value));
235 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237 callback?.Invoke(R, G, B, A);
241 float ret = Interop.Vector4.Vector4_b_get(swigCPtr);
242 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
248 /// The alpha component.
250 /// <since_tizen> 3 </since_tizen>
255 Interop.Vector4.Vector4_a_set(swigCPtr, ValueCheck(value));
256 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
258 callback?.Invoke(R, G, B, A);
262 float ret = Interop.Vector4.Vector4_a_get(swigCPtr);
263 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
269 /// The array subscript operator overload.
271 /// <param name="index">The subscript index.</param>
272 /// <returns>The float at the given index.</returns>
273 /// <since_tizen> 3 </since_tizen>
274 public float this[uint index]
278 return ValueOfIndex(index);
283 /// Converts the Color class to Vector4 class implicitly.
285 /// <param name="color">A color to be converted to Vector4</param>
286 /// <since_tizen> 3 </since_tizen>
287 public static implicit operator Vector4(Color color)
289 return new Vector4(color.R, color.G, color.B, color.A);
293 /// Converts Vector4 class to Color class implicitly.
295 /// <param name="vec">A Vector4 to be converted to color.</param>
296 /// <since_tizen> 3 </since_tizen>
297 public static implicit operator Color(Vector4 vec)
299 return new Color(vec.R, vec.G, vec.B, vec.A);
303 /// The addition operator.
305 /// <param name="arg1">The first value.</param>
306 /// <param name="arg2">The second value.</param>
307 /// <returns>The color containing the result of the addition.</returns>
308 /// <since_tizen> 3 </since_tizen>
309 public static Color operator +(Color arg1, Color arg2)
311 Color result = arg1.Add(arg2);
312 return ValueCheck(result);
316 /// The subtraction 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 subtraction.</returns>
321 /// <since_tizen> 3 </since_tizen>
322 public static Color operator -(Color arg1, Color arg2)
324 Color result = arg1.Subtract(arg2);
325 return ValueCheck(result);
329 /// The unary negation operator.
331 /// <param name="arg1">The target value.</param>
332 /// <returns>The color containg the negation.</returns>
333 /// <since_tizen> 3 </since_tizen>
334 public static Color operator -(Color arg1)
336 Color result = arg1.Subtract();
337 return ValueCheck(result);
341 /// The multiplication operator.
343 /// <param name="arg1">The first value.</param>
344 /// <param name="arg2">The second value.</param>
345 /// <returns>The color containing the result of the multiplication.</returns>
346 /// <since_tizen> 3 </since_tizen>
347 public static Color operator *(Color arg1, Color arg2)
349 Color result = arg1.Multiply(arg2);
350 return ValueCheck(result);
354 /// The multiplication operator.
356 /// <param name="arg1">The first value.</param>
357 /// <param name="arg2">The second value.</param>
358 /// <returns>The color containing the result of the multiplication.</returns>
359 /// <since_tizen> 3 </since_tizen>
360 public static Color operator *(Color arg1, float arg2)
362 Color result = arg1.Multiply(arg2);
363 return ValueCheck(result);
367 /// The division operator.
369 /// <param name="arg1">The first value.</param>
370 /// <param name="arg2">The second value.</param>
371 /// <returns>The color containing the result of the division.</returns>
372 /// <since_tizen> 3 </since_tizen>
373 public static Color operator /(Color arg1, Color arg2)
375 Color result = arg1.Divide(arg2);
376 return ValueCheck(result);
380 /// The division operator.
382 /// <param name="arg1">The first value.</param>
383 /// <param name="arg2">The second value.</param>
384 /// <returns>The color containing the result of the division.</returns>
385 /// <since_tizen> 3 </since_tizen>
386 public static Color operator /(Color arg1, float arg2)
388 Color result = arg1.Divide(arg2);
389 return ValueCheck(result);
393 /// Checks if two color classes are same.
395 /// <param name="rhs">A color to be compared.</param>
396 /// <returns>If two colors are are same, then true.</returns>
397 /// <since_tizen> 3 </since_tizen>
398 public bool EqualTo(Color rhs)
400 bool ret = Interop.Vector4.Vector4_EqualTo(swigCPtr, Color.getCPtr(rhs));
402 if (rhs == null) return false;
404 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
409 /// Checks if two color classes are different.
411 /// <param name="rhs">A color to be compared.</param>
412 /// <returns>If two colors are are different, then true.</returns>
413 /// <since_tizen> 3 </since_tizen>
414 public bool NotEqualTo(Color rhs)
416 bool ret = Interop.Vector4.Vector4_NotEqualTo(swigCPtr, Color.getCPtr(rhs));
417 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422 [EditorBrowsable(EditorBrowsableState.Never)]
423 public object Clone() => new Color(this);
425 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
427 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
430 internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
432 Color ret = new Color(cPtr, false);
433 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
437 internal static Color ValueCheck(Color color)
442 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
444 else if (color.R > 1.0f)
447 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
452 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
454 else if (color.G > 1.0f)
457 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
462 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
464 else if (color.B > 1.0f)
467 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
472 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
474 else if (color.A > 1.0f)
477 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
482 internal static float ValueCheck(float value)
487 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
489 else if (value > 1.0f)
492 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
497 internal static float[] ValueCheck(float[] arr)
499 for (int i = 0; i < arr.Length; i++)
504 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
506 else if (arr[i] > 1.0f)
509 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
515 /// This will not be public opened.
516 [EditorBrowsable(EditorBrowsableState.Never)]
517 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
519 Interop.Vector4.delete_Vector4(swigCPtr);
522 private Color Add(Color rhs)
524 Color ret = new Color(Interop.Vector4.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
525 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
529 private Color AddAssign(Vector4 rhs)
531 Color ret = new Color(Interop.Vector4.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
532 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
536 private Color Subtract(Color rhs)
538 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
539 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
543 private Color SubtractAssign(Color rhs)
545 Color ret = new Color(Interop.Vector4.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
546 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
550 private Color Multiply(Color rhs)
552 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
553 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
557 private Color Multiply(float rhs)
559 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
560 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
564 private Color MultiplyAssign(Color rhs)
566 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
567 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
571 private Color MultiplyAssign(float rhs)
573 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
574 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
578 private Color Divide(Vector4 rhs)
580 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
581 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
585 private Color Divide(float rhs)
587 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
588 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
592 private Color DivideAssign(Color rhs)
594 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
595 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
599 private Color DivideAssign(float rhs)
601 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
602 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
606 private Color Subtract()
608 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_1(swigCPtr), true);
609 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
613 private static bool EqualsColorValue(float f1, float f2)
615 float EPS = (float)Math.Abs(f1 * .00001);
616 if(Math.Abs(f1 - f2) <= EPS)
626 private static bool EqualsColor(Color c1, Color c2)
628 return EqualsColorValue(c1.R, c2.R) && EqualsColorValue(c1.G, c2.G)
629 && EqualsColorValue(c1.B, c2.B) && EqualsColorValue(c1.A, c2.A);
633 /// Determines whether the specified object is equal to the current object.
635 /// <param name="obj">The object to compare with the current object.</param>
636 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
637 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
638 [EditorBrowsable(EditorBrowsableState.Never)]
639 public override bool Equals(System.Object obj)
641 Color color = obj as Color;
648 if (EqualsColor(this, color))
655 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
656 [EditorBrowsable(EditorBrowsableState.Never)]
657 public override int GetHashCode()
659 return hashDummy.GetHashCode();
662 private float ValueOfIndex(uint index)
664 float ret = Interop.Vector4.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
665 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();