Exemple #1
0
        /**
         * Gives you a BaseColor based on a name.
         *
         * @param name
         *            a name such as black, violet, cornflowerblue or #RGB or
         *            #RRGGBB or RGB or RRGGBB or rgb(R,G,B)
         * @return the corresponding BaseColor object. Never returns null.
         * @throws IllegalArgumentException
         *             if the String isn't a know representation of a color.
         */
        public static BaseColor GetRGBColor(String name)
        {
            int[]  color               = { 0, 0, 0, 255 };
            String colorName           = name.ToLower();
            bool   colorStrWithoutHash = MissingHashColorFormat(colorName);

            if (colorName.StartsWith("#") || colorStrWithoutHash)
            {
                if (!colorStrWithoutHash)
                {
                    // lop off the # to unify hex parsing.
                    colorName = colorName.Substring(1);
                }
                if (colorName.Length == 3)
                {
                    String red = colorName.Substring(0, 1);
                    color[0] = int.Parse(red + red, NumberStyles.HexNumber);
                    String green = colorName.Substring(1, 1);
                    color[1] = int.Parse(green + green, NumberStyles.HexNumber);
                    String blue = colorName.Substring(2);
                    color[2] = int.Parse(blue + blue, NumberStyles.HexNumber);
                    return(new BaseColor(color[0], color[1], color[2], color[3]));
                }
                if (colorName.Length == 6)
                {
                    color[0] = int.Parse(colorName.Substring(0, 2), NumberStyles.HexNumber);
                    color[1] = int.Parse(colorName.Substring(2, 2), NumberStyles.HexNumber);
                    color[2] = int.Parse(colorName.Substring(4), NumberStyles.HexNumber);
                    return(new BaseColor(color[0], color[1], color[2], color[3]));
                }
                throw new FormatException(
                          MessageLocalization
                          .GetComposedMessage("unknown.color.format.must.be.rgb.or.rrggbb"));
            }

            if (colorName.StartsWith("rgb("))
            {
                String          delim = "rgb(), \t\r\n\f";
                StringTokenizer tok   = new StringTokenizer(colorName, delim);
                for (int k = 0; k < 3; ++k)
                {
                    if (tok.HasMoreTokens())
                    {
                        color[k] = GetRGBChannelValue(tok.NextToken());
                        color[k] = Math.Max(0, color[k]);
                        color[k] = Math.Min(255, color[k]);
                    }
                }
                return(new BaseColor(color[0], color[1], color[2], color[3]));
            }

            if (!NAMES.ContainsKey(colorName))
            {
                throw new FormatException(
                          MessageLocalization.GetComposedMessage("color.not.found",
                                                                 new String[] { colorName }));
            }
            color = NAMES[colorName];
            return(new BaseColor(color[0], color[1], color[2], color[3]));
        }
        /// <summary>
        /// Gives you a Color based on a name.
        /// a name such as black, violet, cornflowerblue or #RGB or #RRGGBB
        /// or rgb(R,G,B)
        /// @throws IllegalArgumentException
        /// if the String isn't a know representation of a color.
        /// </summary>
        /// <param name="name"></param>
        /// <returns>the corresponding Color object</returns>
        public static BaseColor GetRgbColor(string name)
        {
            int[] c = { 0, 0, 0, 0 };
            if (name.StartsWith("#"))
            {
                if (name.Length == 4)
                {
                    c[0] = int.Parse(name.Substring(1, 1), NumberStyles.HexNumber) * 16;
                    c[1] = int.Parse(name.Substring(2, 1), NumberStyles.HexNumber) * 16;
                    c[2] = int.Parse(name.Substring(3), NumberStyles.HexNumber) * 16;
                    return(new BaseColor(c[0], c[1], c[2], c[3]));
                }
                if (name.Length == 7)
                {
                    c[0] = int.Parse(name.Substring(1, 2), NumberStyles.HexNumber);
                    c[1] = int.Parse(name.Substring(3, 2), NumberStyles.HexNumber);
                    c[2] = int.Parse(name.Substring(5), NumberStyles.HexNumber);
                    return(new BaseColor(c[0], c[1], c[2], c[3]));
                }
                throw new ArgumentException(
                          "Unknown color format. Must be #RGB or #RRGGBB");
            }
            else if (name.StartsWith("rgb("))
            {
                var tok = new StringTokenizer(name, "rgb(), \t\r\n\f");
                for (var k = 0; k < 3; ++k)
                {
                    var v = tok.NextToken();
                    if (v.EndsWith("%"))
                    {
                        c[k] = int.Parse(v.Substring(0, v.Length - 1)) * 255 / 100;
                    }
                    else
                    {
                        c[k] = int.Parse(v);
                    }

                    if (c[k] < 0)
                    {
                        c[k] = 0;
                    }
                    else if (c[k] > 255)
                    {
                        c[k] = 255;
                    }
                }
                return(new BaseColor(c[0], c[1], c[2], c[3]));
            }
            name = name.ToLowerInvariant();
            if (!Names.ContainsKey(name))
            {
                throw new ArgumentException("Color '" + name
                                            + "' not found.");
            }

            c = (int[])Names[name];
            return(new BaseColor(c[0], c[1], c[2], c[3]));
        }
Exemple #3
0
 /**
  * Gives you a Color based on a name.
  *
  * @param name
  *            a name such as black, violet, cornflowerblue or #RGB or #RRGGBB
  *            or rgb(R,G,B)
  * @return the corresponding Color object
  * @throws IllegalArgumentException
  *             if the String isn't a know representation of a color.
  */
 public static BaseColor GetRGBColor(String name)
 {
     int[] c = { 0, 0, 0, 0 };
     if (name.StartsWith("#"))
     {
         if (name.Length == 4)
         {
             c[0] = int.Parse(name.Substring(1, 1), NumberStyles.HexNumber) * 16;
             c[1] = int.Parse(name.Substring(2, 1), NumberStyles.HexNumber) * 16;
             c[2] = int.Parse(name.Substring(3), NumberStyles.HexNumber) * 16;
             return(new BaseColor(c[0], c[1], c[2], c[3]));
         }
         if (name.Length == 7)
         {
             c[0] = int.Parse(name.Substring(1, 2), NumberStyles.HexNumber);
             c[1] = int.Parse(name.Substring(3, 2), NumberStyles.HexNumber);
             c[2] = int.Parse(name.Substring(5), NumberStyles.HexNumber);
             return(new BaseColor(c[0], c[1], c[2], c[3]));
         }
         throw new ArgumentException(MessageLocalization.GetComposedMessage("unknown.color.format.must.be.rgb.or.rrggbb"));
     }
     else if (name.StartsWith("rgb("))
     {
         StringTokenizer tok = new StringTokenizer(name, "rgb(), \t\r\n\f");
         for (int k = 0; k < 3; ++k)
         {
             String v = tok.NextToken();
             if (v.EndsWith("%"))
             {
                 c[k] = int.Parse(v.Substring(0, v.Length - 1)) * 255 / 100;
             }
             else
             {
                 c[k] = int.Parse(v);
             }
             if (c[k] < 0)
             {
                 c[k] = 0;
             }
             else if (c[k] > 255)
             {
                 c[k] = 255;
             }
         }
         return(new BaseColor(c[0], c[1], c[2], c[3]));
     }
     name = name.ToLower(CultureInfo.InvariantCulture);
     if (!NAMES.ContainsKey(name))
     {
         throw new ArgumentException("Color '" + name
                                     + "' not found.");
     }
     c = (int[])NAMES[name];
     return(new BaseColor(c[0], c[1], c[2], c[3]));
 }
Exemple #4
0
 /**
 * Gives you a Color based on a name.
 * 
 * @param name
 *            a name such as black, violet, cornflowerblue or #RGB or #RRGGBB
 *            or rgb(R,G,B)
 * @return the corresponding Color object
 * @throws IllegalArgumentException
 *             if the String isn't a know representation of a color.
 */
 public static Color GetRGBColor(String name) {
     int[] c = { 0, 0, 0, 0 };
     if (name.StartsWith("#")) {
         if (name.Length == 4) {
             c[0] = int.Parse(name.Substring(1, 1), NumberStyles.HexNumber) * 16;
             c[1] = int.Parse(name.Substring(2, 1), NumberStyles.HexNumber) * 16;
             c[2] = int.Parse(name.Substring(3), NumberStyles.HexNumber) * 16;
             return new Color(c[0], c[1], c[2], c[3]);
         }
         if (name.Length == 7) {
             c[0] = int.Parse(name.Substring(1, 2), NumberStyles.HexNumber);
             c[1] = int.Parse(name.Substring(3, 2), NumberStyles.HexNumber);
             c[2] = int.Parse(name.Substring(5), NumberStyles.HexNumber);
             return new Color(c[0], c[1], c[2], c[3]);
         }
         throw new ArgumentException(
                 "Unknown color format. Must be #RGB or #RRGGBB");
     }
     else if (name.StartsWith("rgb(")) {
         StringTokenizer tok = new StringTokenizer(name, "rgb(), \t\r\n\f");
         for (int k = 0; k < 3; ++k) {
             String v = tok.NextToken();
             if (v.EndsWith("%"))
                 c[k] = int.Parse(v.Substring(0, v.Length - 1)) * 255 / 100;
             else
                 c[k] = int.Parse(v);
             if (c[k] < 0)
                 c[k] = 0;
             else if (c[k] > 255)
                 c[k] = 255;
         }
         return new Color(c[0], c[1], c[2], c[3]);
     }
     name = name.ToLower(CultureInfo.InvariantCulture);
     if (!NAMES.ContainsKey(name))
         throw new ArgumentException("Color '" + name
                 + "' not found.");
     c = (int[]) NAMES[name];
     return new Color(c[0], c[1], c[2], c[3]);
 }
Exemple #5
0
        /**
         * Gives you a BaseColor based on a name.
         *
         * @param name
         *            a name such as black, violet, cornflowerblue or #RGB or #RRGGBB
         *            or RGB or RRGGBB or rgb(R,G,B)
         * @return the corresponding BaseColor object.  Never returns null.
         * @throws IllegalArgumentException
         *             if the String isn't a know representation of a color.
         */
        public static BaseColor GetRGBColor(String name)
        {
            int[] c = { 0, 0, 0, 255 };
            name = name.ToLowerInvariant();
            bool colorStrWithoutHash = MissingHashColorFormat(name);

            if (name.StartsWith("#") || colorStrWithoutHash)
            {
                if (!colorStrWithoutHash)
                {
                    name = name.Substring(1); // lop off the # to unify hex parsing.
                }
                if (name.Length == 3)
                {
                    String s = name.Substring(0, 1);
                    c[0] = int.Parse(s + s, NumberStyles.HexNumber);
                    String s2 = name.Substring(1, 1);
                    c[1] = int.Parse(s2 + s2, NumberStyles.HexNumber);
                    String s3 = name.Substring(2);
                    c[2] = int.Parse(s3 + s3, NumberStyles.HexNumber);
                    return(new BaseColor(c[0], c[1], c[2], c[3]));
                }
                if (name.Length == 6)
                {
                    c[0] = int.Parse(name.Substring(0, 2), NumberStyles.HexNumber);
                    c[1] = int.Parse(name.Substring(2, 2), NumberStyles.HexNumber);
                    c[2] = int.Parse(name.Substring(4), NumberStyles.HexNumber);
                    return(new BaseColor(c[0], c[1], c[2], c[3]));
                }
                throw new ArgumentException(MessageLocalization.GetComposedMessage("unknown.color.format.must.be.rgb.or.rrggbb"));
            }
            else if (name.StartsWith("rgb("))
            {
                StringTokenizer tok = new StringTokenizer(name, "rgb(), \t\r\n\f");
                for (int k = 0; k < 3; ++k)
                {
                    String v = tok.NextToken();
                    if (v.EndsWith("%"))
                    {
                        c[k] = int.Parse(v.Substring(0, v.Length - 1)) * 255 / 100;
                    }
                    else
                    {
                        c[k] = int.Parse(v);
                    }
                    if (c[k] < 0)
                    {
                        c[k] = 0;
                    }
                    else if (c[k] > 255)
                    {
                        c[k] = 255;
                    }
                }
                return(new BaseColor(c[0], c[1], c[2], c[3]));
            }
            name = name.ToLower(CultureInfo.InvariantCulture);
            if (!NAMES.ContainsKey(name))
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("color.not.found", new String[] { name }));
            }
            c = NAMES[name];
            return(new BaseColor(c[0], c[1], c[2], c[3]));
        }