[NUI] Parse Color from Strings (hexCode, RGB, RGBA) (#2646)
authorAli-Alzyoud <ali198724@gmail.com>
Fri, 19 Feb 2021 05:12:50 +0000 (07:12 +0200)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Thu, 25 Feb 2021 08:24:58 +0000 (17:24 +0900)
- 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)

Co-authored-by: Jiyun Yang <ji.yang@samsung.com>
src/Tizen.NUI/src/public/Common/Color.cs

index 26cc912..254d385 100755 (executable)
@@ -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]));
+                    }
+                }
             }
             catch
             {
-                throw new global::System.ArgumentException("Please check your hex code");
+                throw new global::System.ArgumentException("Please check your color text code");
             }
         }