[NUI] Create Color from Hex Color Code (#1511)
authorneostom432 <31119276+neostom432@users.noreply.github.com>
Wed, 1 Apr 2020 08:06:12 +0000 (17:06 +0900)
committerGitHub <noreply@github.com>
Wed, 1 Apr 2020 08:06:12 +0000 (17:06 +0900)
* [NUI] Create Color from Hex Color Code

Create color object from Hex Color Code (string).

1. Code with "#", it will be remove.
2. Without alpha channel, alpah channel will be set 1.0f.
3. Working both. Lowercase and uppercase,

"#FF0000FF" - Good
"#ff0000ff" - Good
"FF0000FF"  - Good
"FF0000"    - Good
"FF"        - Bad  ( too short )

* [NUI] Add ArgumentException to Color constructor

src/Tizen.NUI/src/public/Color.cs

index d7ad7b0..8aafc54 100755 (executable)
@@ -117,6 +117,30 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
+        /// <summary>
+        /// The conversion constructor from an hexcode of four floats.
+        /// </summary>
+        /// <param name="hexColor">Hex color code</param>
+        /// <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.new_Vector4__SWIG_0(), true)
+        {
+            try
+            {
+                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;
+            }
+            catch
+            {
+                throw new global::System.ArgumentException("Please check your hex code");
+            }
+        }
+
         internal Color(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
         {
                        hashDummy = false;