[NUI] Fix comments according to document review
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / Color.cs
index f49e44f..e09972c 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2019 Samsung Electronics Co., Ltd.
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 using System;
 using Tizen.NUI.Binding;
 using System.ComponentModel;
+using System.Globalization;
 
 namespace Tizen.NUI
 {
-
     /// <summary>
     /// The Color class.
     /// </summary>
@@ -692,7 +692,7 @@ namespace Tizen.NUI
         /// Gets the  Pale_Turquoise colored Color class.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly Color PaleTurquoise = NDalic. PALE_TURQUOISE;
+        public static readonly Color PaleTurquoise = NDalic.PALE_TURQUOISE;
 
         /// <summary>
         /// Gets the Pale_Violet_Red colored Color class.
@@ -704,7 +704,7 @@ namespace Tizen.NUI
         /// Gets the Papaya_whip  colored Color class.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public static readonly Color PapayaWhip = NDalic.PAPAYA_WHIP ;
+        public static readonly Color PapayaWhip = NDalic.PAPAYA_WHIP;
 
         /// <summary>
         /// Gets the Peach_puff colored Color class.
@@ -953,31 +953,90 @@ namespace Tizen.NUI
         }
 
         /// <summary>
-        /// The conversion constructor from an hexcode of four floats.
+        /// The conversion constructor from text color representation.
+        /// hexcode representation : #RGB #RGBA #RRGGBB #RRGGBBAA
+        /// rgb representation : rgb(0-255,0-255,0-255) rgba(0-255,0-255,0-255,0.0-1.0)
         /// </summary>
-        /// <param name="hexColor">Hex color code</param>
+        /// <param name="textColor">color text representation as Hexcode, rgb() or rgba()</param>
         /// <exception cref="ArgumentNullException">This exception is thrown when hexColor is null.</exception>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public Color(string hexColor) : this(Interop.Vector4.NewVector4(), true)
+        public Color(string textColor) : this(Interop.Vector4.NewVector4(), true)
         {
             try
             {
-                if (null == hexColor)
+                if (null == textColor)
                 {
-                    throw new ArgumentNullException(nameof(hexColor));
+                    throw new ArgumentNullException(nameof(textColor));
                 }
-                hexColor = hexColor.Replace("#", "");
 
-                R = ((float)Convert.ToInt32(hexColor.Substring(0, 2), 16)) / 255.0f;
-                G = ((float)Convert.ToInt32(hexColor.Substring(2, 2), 16)) / 255.0f;
-                B = ((float)Convert.ToInt32(hexColor.Substring(4, 2), 16)) / 255.0f;
-                A = hexColor.Length > 6 ? ((float)Convert.ToInt32(hexColor.Substring(6, 2), 16)) / 255.0f : 1.0f;
+                textColor = textColor.ToUpperInvariant();
+                textColor = textColor.Replace(" ", "");
+
+                if (textColor.Length > 0 && textColor[0] == '#')
+                {
+                    textColor = textColor.Replace("#", "");
+                    int textColorLength = textColor.Length;
+
+                    if (textColorLength == 6 || textColorLength == 8) /* #RRGGBB or #RRGGBBAA */
+                    {
+                        R = ((float)Convert.ToInt32(textColor.Substring(0, 2), 16)) / 255.0f;
+                        G = ((float)Convert.ToInt32(textColor.Substring(2, 2), 16)) / 255.0f;
+                        B = ((float)Convert.ToInt32(textColor.Substring(4, 2), 16)) / 255.0f;
+                        A = textColor.Length > 6 ? ((float)Convert.ToInt32(textColor.Substring(6, 2), 16)) / 255.0f : 1.0f;
+                    }
+                    else if (textColorLength == 3 || textColorLength == 4) /* #RGB */
+                    {
+                        R = ((float)Convert.ToInt32(textColor.Substring(0, 1), 16)) / 15.0f;
+                        G = ((float)Convert.ToInt32(textColor.Substring(1, 1), 16)) / 15.0f;
+                        B = ((float)Convert.ToInt32(textColor.Substring(2, 1), 16)) / 15.0f;
+                        A = textColor.Length > 3 ? ((float)Convert.ToInt32(textColor.Substring(3, 1), 16)) / 15.0f : 1.0f;
+                    }
+                    else
+                    {
+                        throw new global::System.ArgumentException("Please check your color text code");
+                    }
+                }
+                else // example rgb(255,255,255) or rgb(255,255,255,1.0)
+                {
+                    bool isRGBA = textColor.StartsWith("RGBA(");
+                    bool isRGB = textColor.StartsWith("RGB(");
+
+                    if (!isRGBA && !isRGB)
+                    {
+                        throw new global::System.ArgumentException("Please check your color text code");
+                    }
+
+                    if (isRGBA)
+                        textColor = textColor.Substring(4);
+                    if (isRGB)
+                        textColor = textColor.Substring(3);
+
+                    textColor = textColor.Replace(")", "");
+                    textColor = textColor.Replace("(", "");
+
+                    string[] components = textColor.Split(',');
+
+                    if (components.Length == 3 && isRGB)
+                    {
+                        R = Math.Min(1.0f, ((float)Convert.ToInt32(components[0], 10)) / 255.0f);
+                        G = Math.Min(1.0f, ((float)Convert.ToInt32(components[1], 10)) / 255.0f);
+                        B = Math.Min(1.0f, ((float)Convert.ToInt32(components[2], 10)) / 255.0f);
+                        A = 1.0f;
+                    }
+                    else if (components.Length == 4 && isRGBA)
+                    {
+                        R = Math.Min(1.0f, ((float)Convert.ToInt32(components[0], 10)) / 255.0f);
+                        G = Math.Min(1.0f, ((float)Convert.ToInt32(components[1], 10)) / 255.0f);
+                        B = Math.Min(1.0f, ((float)Convert.ToInt32(components[2], 10)) / 255.0f);
+                        A = Math.Min(1.0f, float.Parse(components[3], CultureInfo.InvariantCulture));
+                    }
+                }
             }
             catch
             {
-                throw new global::System.ArgumentException("Please check your hex code");
+                throw new global::System.ArgumentException("Please check your color text code");
             }
         }
 
@@ -998,8 +1057,9 @@ namespace Tizen.NUI
         /// The copy constructor.
         /// </summary>
         /// <param name="other">The copy target.</param>
+        /// <exception cref="ArgumentNullException"> Thrown when other is null. </exception>
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public Color(Color other) : this((float)other?.R, (float)other.G, (float)other.B, (float)other.A)
+        public Color(Color other) : this(other == null ? throw new ArgumentNullException(nameof(other)) : other.R, other.G, other.B, other.A)
         {
         }
 
@@ -1025,22 +1085,22 @@ namespace Tizen.NUI
         /// The red component.
         /// </summary>
         /// <remarks>
-        /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
+        /// The setter is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor.
         /// </remarks>
         /// <code>
-        /// // DO NOT use like the followings!
+        /// // DO NOT use as follows:
         /// Color color = new Color();
         /// color.R = 0.1f; 
-        /// // Please USE like this
+        /// // USE like this
         /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
         /// Color color = new Color(r, g, b, a);
         /// </code>
         /// <since_tizen> 3 </since_tizen>
         public float R
         {
+            [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor")]
             set
             {
-                Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
                 Interop.Vector4.RSet(SwigCPtr, ValueCheck(value));
                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
 
@@ -1058,22 +1118,22 @@ namespace Tizen.NUI
         /// The green component.
         /// </summary>
         /// <remarks>
-        /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
+        /// The setter is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor.
         /// </remarks>
         /// <code>
-        /// // DO NOT use like the followings!
+        /// // DO NOT use as follows:
         /// Color color = new Color();
         /// color.G = 0.5f; 
-        /// // Please USE like this
+        /// // USE like this
         /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
         /// Color color = new Color(r, g, b, a);
         /// </code>
         /// <since_tizen> 3 </since_tizen>
         public float G
         {
+            [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor")]
             set
             {
-                Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
                 Interop.Vector4.GSet(SwigCPtr, ValueCheck(value));
                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
 
@@ -1091,22 +1151,22 @@ namespace Tizen.NUI
         /// The blue component.
         /// </summary>
         /// <remarks>
-        /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
+        /// The setter is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor.
         /// </remarks>
         /// <code>
-        /// // DO NOT use like the followings!
+        /// // DO NOT use as follows:
         /// Color color = new Color();
         /// color.B = 0.9f; 
-        /// // Please USE like this
+        /// // USE like this
         /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
         /// Color color = new Color(r, g, b, a);
         /// </code>
         /// <since_tizen> 3 </since_tizen>
         public float B
         {
+            [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor")]
             set
             {
-                Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
                 Interop.Vector4.BSet(SwigCPtr, ValueCheck(value));
                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
 
@@ -1124,22 +1184,22 @@ namespace Tizen.NUI
         /// The alpha component.
         /// </summary>
         /// <remarks>
-        /// The setter is deprecated in API8 and will be removed in API10. Please use new Color(...) constructor.
+        /// The setter is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor.
         /// </remarks>
         /// <code>
-        /// // DO NOT use like the followings!
+        /// // DO NOT use as follows:
         /// Color color = new Color();
         /// color.A = 1.0f; 
-        /// // Please USE like this
+        /// // USE like this
         /// float r = 0.1f, g = 0.5f, b = 0.9f, a = 1.0f;
         /// Color color = new Color(r, g, b, a);
         /// </code>
         /// <since_tizen> 3 </since_tizen>
         public float A
         {
+            [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use the new Color(...) constructor")]
             set
             {
-                Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Color(...) constructor");
                 Interop.Vector4.ASet(SwigCPtr, ValueCheck(value));
                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
 
@@ -1174,7 +1234,11 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public static implicit operator Vector4(Color color)
         {
-            return new Vector4((float)color?.R, (float)color.G, (float)color.B, (float)color.A);
+            if (color == null)
+            {
+                return null;
+            }
+            return new Vector4(color.R, color.G, color.B, color.A);
         }
 
         /// <summary>
@@ -1184,7 +1248,11 @@ namespace Tizen.NUI
         /// <since_tizen> 3 </since_tizen>
         public static implicit operator Color(Vector4 vec)
         {
-            return new Color((float)vec?.R, (float)vec.G, (float)vec.B, (float)vec.A);
+            if (vec == null)
+            {
+                return null;
+            }
+            return new Color(vec.R, vec.G, vec.B, vec.A);
         }
 
         /// <summary>
@@ -1345,11 +1413,6 @@ namespace Tizen.NUI
         [EditorBrowsable(EditorBrowsableState.Never)]
         public object Clone() => new Color(this);
 
-        internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Color obj)
-        {
-            return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
-        }
-
         internal static Color GetColorFromPtr(global::System.IntPtr cPtr)
         {
             Color ret = new Color(cPtr, false);