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
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();
421 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
423 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
426 internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
428 Color ret = new Color(cPtr, false);
429 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
433 internal static Color ValueCheck(Color color)
438 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
440 else if (color.R > 1.0f)
443 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
448 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
450 else if (color.G > 1.0f)
453 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
458 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
460 else if (color.B > 1.0f)
463 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
468 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
470 else if (color.A > 1.0f)
473 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
478 internal static float ValueCheck(float value)
483 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
485 else if (value > 1.0f)
488 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
493 internal static float[] ValueCheck(float[] arr)
495 for (int i = 0; i < arr.Length; i++)
500 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
502 else if (arr[i] > 1.0f)
505 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
511 /// This will not be public opened.
512 [EditorBrowsable(EditorBrowsableState.Never)]
513 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
515 Interop.Vector4.delete_Vector4(swigCPtr);
518 private Color Add(Color rhs)
520 Color ret = new Color(Interop.Vector4.Vector4_Add(swigCPtr, Color.getCPtr(rhs)), true);
521 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
525 private Color AddAssign(Vector4 rhs)
527 Color ret = new Color(Interop.Vector4.Vector4_AddAssign(swigCPtr, Color.getCPtr(rhs)), false);
528 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
532 private Color Subtract(Color rhs)
534 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
535 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
539 private Color SubtractAssign(Color rhs)
541 Color ret = new Color(Interop.Vector4.Vector4_SubtractAssign(swigCPtr, Color.getCPtr(rhs)), false);
542 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
546 private Color Multiply(Color rhs)
548 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
549 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
553 private Color Multiply(float rhs)
555 Color ret = new Color(Interop.Vector4.Vector4_Multiply__SWIG_1(swigCPtr, rhs), true);
556 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
560 private Color MultiplyAssign(Color rhs)
562 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
563 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
567 private Color MultiplyAssign(float rhs)
569 Color ret = new Color(Interop.Vector4.Vector4_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
570 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
574 private Color Divide(Vector4 rhs)
576 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_0(swigCPtr, Color.getCPtr(rhs)), true);
577 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
581 private Color Divide(float rhs)
583 Color ret = new Color(Interop.Vector4.Vector4_Divide__SWIG_1(swigCPtr, rhs), true);
584 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
588 private Color DivideAssign(Color rhs)
590 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_0(swigCPtr, Color.getCPtr(rhs)), false);
591 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
595 private Color DivideAssign(float rhs)
597 Color ret = new Color(Interop.Vector4.Vector4_DivideAssign__SWIG_1(swigCPtr, rhs), false);
598 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
602 private Color Subtract()
604 Color ret = new Color(Interop.Vector4.Vector4_Subtract__SWIG_1(swigCPtr), true);
605 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
609 private static bool EqualsColorValue(float f1, float f2)
611 float EPS = (float)Math.Abs(f1 * .00001);
612 if(Math.Abs(f1 - f2) <= EPS)
622 private static bool EqualsColor(Color c1, Color c2)
624 return EqualsColorValue(c1.R, c2.R) && EqualsColorValue(c1.G, c2.G)
625 && EqualsColorValue(c1.B, c2.B) && EqualsColorValue(c1.A, c2.A);
629 /// Determines whether the specified object is equal to the current object.
631 /// <param name="obj">The object to compare with the current object.</param>
632 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
633 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
634 [EditorBrowsable(EditorBrowsableState.Never)]
635 public override bool Equals(System.Object obj)
637 Color color = obj as Color;
644 if (EqualsColor(this, color))
651 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
652 [EditorBrowsable(EditorBrowsableState.Never)]
653 public override int GetHashCode()
655 return hashDummy.GetHashCode();
658 private float ValueOfIndex(uint index)
660 float ret = Interop.Vector4.Vector4_ValueOfIndex__SWIG_0(swigCPtr, index);
661 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();