Open Source Repository

Home /itextpdf/itextpdf-5.1.2 | Repository Home



com/itextpdf/text/html/WebColors.java
/*
 * $Id: WebColors.java 4784 2011-03-15 08:33:00Z blowagie $
 *
 * This file is part of the iText (R) project.
 * Copyright (c) 1998-2011 1T3XT BVBA
 * Authors: Bruno Lowagie, Paulo Soares, et al.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation with the addition of the
 * following permission added to Section 15 as permitted in Section 7(a):
 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
 * http://itextpdf.com/terms-of-use/
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License,
 * a covered work must retain the producer line in every PDF that is created
 * or manipulated using iText.
 *
 * You can be released from the requirements of the license by purchasing
 * a commercial license. Buying such a license is mandatory as soon as you
 * develop commercial activities involving the iText software without
 * disclosing the source code of your own applications.
 * These activities include: offering paid services to customers as an ASP,
 * serving PDFs on the fly in a web application, shipping iText with a closed
 * source product.
 *
 * For more information, please contact iText Software Corp. at this
 * address: [email protected]
 */
package com.itextpdf.text.html;

import java.util.HashMap;
import java.util.StringTokenizer;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.error_messages.MessageLocalization;

/**
 * This class is a HashMap that contains the names of colors as a key and the
 * corresponding BaseColor as value. (Source: Wikipedia
 * http://en.wikipedia.org/wiki/Web_colors )
 *
 @author blowagie
 */
public class WebColors extends HashMap<String, int[]{

  private static final long serialVersionUID = 3542523100813372896L;
  /** HashMap containing all the names and corresponding color values. */
  public static final WebColors NAMES = new WebColors();
  static {
    NAMES.put("aliceblue"new int[] { 0xf00xf80xff0xff });
    NAMES.put("antiquewhite"new int[] { 0xfa0xeb0xd70xff });
    NAMES.put("aqua"new int[] { 0x000xff0xff0xff });
    NAMES.put("aquamarine"new int[] { 0x7f0xff0xd40xff });
    NAMES.put("azure"new int[] { 0xf00xff0xff0xff });
    NAMES.put("beige"new int[] { 0xf50xf50xdc0xff });
    NAMES.put("bisque"new int[] { 0xff0xe40xc40xff });
    NAMES.put("black"new int[] { 0x000x000x000xff });
    NAMES.put("blanchedalmond"new int[] { 0xff0xeb0xcd0xff });
    NAMES.put("blue"new int[] { 0x000x000xff0xff });
    NAMES.put("blueviolet"new int[] { 0x8a0x2b0xe20xff });
    NAMES.put("brown"new int[] { 0xa50x2a0x2a0xff });
    NAMES.put("burlywood"new int[] { 0xde0xb80x870xff });
    NAMES.put("cadetblue"new int[] { 0x5f0x9e0xa00xff });
    NAMES.put("chartreuse"new int[] { 0x7f0xff0x000xff });
    NAMES.put("chocolate"new int[] { 0xd20x690x1e0xff });
    NAMES.put("coral"new int[] { 0xff0x7f0x500xff });
    NAMES.put("cornflowerblue"new int[] { 0x640x950xed0xff });
    NAMES.put("cornsilk"new int[] { 0xff0xf80xdc0xff });
    NAMES.put("crimson"new int[] { 0xdc0x140x3c0xff });
    NAMES.put("cyan"new int[] { 0x000xff0xff0xff });
    NAMES.put("darkblue"new int[] { 0x000x000x8b0xff });
    NAMES.put("darkcyan"new int[] { 0x000x8b0x8b0xff });
    NAMES.put("darkgoldenrod"new int[] { 0xb80x860x0b0xff });
    NAMES.put("darkgray"new int[] { 0xa90xa90xa90xff });
    NAMES.put("darkgreen"new int[] { 0x000x640x000xff });
    NAMES.put("darkkhaki"new int[] { 0xbd0xb70x6b0xff });
    NAMES.put("darkmagenta"new int[] { 0x8b0x000x8b0xff });
    NAMES.put("darkolivegreen"new int[] { 0x550x6b0x2f0xff });
    NAMES.put("darkorange"new int[] { 0xff0x8c0x000xff });
    NAMES.put("darkorchid"new int[] { 0x990x320xcc0xff });
    NAMES.put("darkred"new int[] { 0x8b0x000x000xff });
    NAMES.put("darksalmon"new int[] { 0xe90x960x7a0xff });
    NAMES.put("darkseagreen"new int[] { 0x8f0xbc0x8f0xff });
    NAMES.put("darkslateblue"new int[] { 0x480x3d0x8b0xff });
    NAMES.put("darkslategray"new int[] { 0x2f0x4f0x4f0xff });
    NAMES.put("darkturquoise"new int[] { 0x000xce0xd10xff });
    NAMES.put("darkviolet"new int[] { 0x940x000xd30xff });
    NAMES.put("deeppink"new int[] { 0xff0x140x930xff });
    NAMES.put("deepskyblue"new int[] { 0x000xbf0xff0xff });
    NAMES.put("dimgray"new int[] { 0x690x690x690xff });
    NAMES.put("dodgerblue"new int[] { 0x1e0x900xff0xff });
    NAMES.put("firebrick"new int[] { 0xb20x220x220xff });
    NAMES.put("floralwhite"new int[] { 0xff0xfa0xf00xff });
    NAMES.put("forestgreen"new int[] { 0x220x8b0x220xff });
    NAMES.put("fuchsia"new int[] { 0xff0x000xff0xff });
    NAMES.put("gainsboro"new int[] { 0xdc0xdc0xdc0xff });
    NAMES.put("ghostwhite"new int[] { 0xf80xf80xff0xff });
    NAMES.put("gold"new int[] { 0xff0xd70x000xff });
    NAMES.put("goldenrod"new int[] { 0xda0xa50x200xff });
    NAMES.put("gray"new int[] { 0x800x800x800xff });
    NAMES.put("green"new int[] { 0x000x800x000xff });
    NAMES.put("greenyellow"new int[] { 0xad0xff0x2f0xff });
    NAMES.put("honeydew"new int[] { 0xf00xff0xf00xff });
    NAMES.put("hotpink"new int[] { 0xff0x690xb40xff });
    NAMES.put("indianred"new int[] { 0xcd0x5c0x5c0xff });
    NAMES.put("indigo"new int[] { 0x4b0x000x820xff });
    NAMES.put("ivory"new int[] { 0xff0xff0xf00xff });
    NAMES.put("khaki"new int[] { 0xf00xe60x8c0xff });
    NAMES.put("lavender"new int[] { 0xe60xe60xfa0xff });
    NAMES.put("lavenderblush"new int[] { 0xff0xf00xf50xff });
    NAMES.put("lawngreen"new int[] { 0x7c0xfc0x000xff });
    NAMES.put("lemonchiffon"new int[] { 0xff0xfa0xcd0xff });
    NAMES.put("lightblue"new int[] { 0xad0xd80xe60xff });
    NAMES.put("lightcoral"new int[] { 0xf00x800x800xff });
    NAMES.put("lightcyan"new int[] { 0xe00xff0xff0xff });
    NAMES.put("lightgoldenrodyellow"new int[] { 0xfa0xfa0xd20xff });
    NAMES.put("lightgreen"new int[] { 0x900xee0x900xff });
    NAMES.put("lightgrey"new int[] { 0xd30xd30xd30xff });
    NAMES.put("lightpink"new int[] { 0xff0xb60xc10xff });
    NAMES.put("lightsalmon"new int[] { 0xff0xa00x7a0xff });
    NAMES.put("lightseagreen"new int[] { 0x200xb20xaa0xff });
    NAMES.put("lightskyblue"new int[] { 0x870xce0xfa0xff });
    NAMES.put("lightslategray"new int[] { 0x770x880x990xff });
    NAMES.put("lightsteelblue"new int[] { 0xb00xc40xde0xff });
    NAMES.put("lightyellow"new int[] { 0xff0xff0xe00xff });
    NAMES.put("lime"new int[] { 0x000xff0x000xff });
    NAMES.put("limegreen"new int[] { 0x320xcd0x320xff });
    NAMES.put("linen"new int[] { 0xfa0xf00xe60xff });
    NAMES.put("magenta"new int[] { 0xff0x000xff0xff });
    NAMES.put("maroon"new int[] { 0x800x000x000xff });
    NAMES.put("mediumaquamarine"new int[] { 0x660xcd0xaa0xff });
    NAMES.put("mediumblue"new int[] { 0x000x000xcd0xff });
    NAMES.put("mediumorchid"new int[] { 0xba0x550xd30xff });
    NAMES.put("mediumpurple"new int[] { 0x930x700xdb0xff });
    NAMES.put("mediumseagreen"new int[] { 0x3c0xb30x710xff });
    NAMES.put("mediumslateblue"new int[] { 0x7b0x680xee0xff });
    NAMES.put("mediumspringgreen"new int[] { 0x000xfa0x9a0xff });
    NAMES.put("mediumturquoise"new int[] { 0x480xd10xcc0xff });
    NAMES.put("mediumvioletred"new int[] { 0xc70x150x850xff });
    NAMES.put("midnightblue"new int[] { 0x190x190x700xff });
    NAMES.put("mintcream"new int[] { 0xf50xff0xfa0xff });
    NAMES.put("mistyrose"new int[] { 0xff0xe40xe10xff });
    NAMES.put("moccasin"new int[] { 0xff0xe40xb50xff });
    NAMES.put("navajowhite"new int[] { 0xff0xde0xad0xff });
    NAMES.put("navy"new int[] { 0x000x000x800xff });
    NAMES.put("oldlace"new int[] { 0xfd0xf50xe60xff });
    NAMES.put("olive"new int[] { 0x800x800x000xff });
    NAMES.put("olivedrab"new int[] { 0x6b0x8e0x230xff });
    NAMES.put("orange"new int[] { 0xff0xa50x000xff });
    NAMES.put("orangered"new int[] { 0xff0x450x000xff });
    NAMES.put("orchid"new int[] { 0xda0x700xd60xff });
    NAMES.put("palegoldenrod"new int[] { 0xee0xe80xaa0xff });
    NAMES.put("palegreen"new int[] { 0x980xfb0x980xff });
    NAMES.put("paleturquoise"new int[] { 0xaf0xee0xee0xff });
    NAMES.put("palevioletred"new int[] { 0xdb0x700x930xff });
    NAMES.put("papayawhip"new int[] { 0xff0xef0xd50xff });
    NAMES.put("peachpuff"new int[] { 0xff0xda0xb90xff });
    NAMES.put("peru"new int[] { 0xcd0x850x3f0xff });
    NAMES.put("pink"new int[] { 0xff0xc00xcb0xff });
    NAMES.put("plum"new int[] { 0xdd0xa00xdd0xff });
    NAMES.put("powderblue"new int[] { 0xb00xe00xe60xff });
    NAMES.put("purple"new int[] { 0x800x000x800xff });
    NAMES.put("red"new int[] { 0xff0x000x000xff });
    NAMES.put("rosybrown"new int[] { 0xbc0x8f0x8f0xff });
    NAMES.put("royalblue"new int[] { 0x410x690xe10xff });
    NAMES.put("saddlebrown"new int[] { 0x8b0x450x130xff });
    NAMES.put("salmon"new int[] { 0xfa0x800x720xff });
    NAMES.put("sandybrown"new int[] { 0xf40xa40x600xff });
    NAMES.put("seagreen"new int[] { 0x2e0x8b0x570xff });
    NAMES.put("seashell"new int[] { 0xff0xf50xee0xff });
    NAMES.put("sienna"new int[] { 0xa00x520x2d0xff });
    NAMES.put("silver"new int[] { 0xc00xc00xc00xff });
    NAMES.put("skyblue"new int[] { 0x870xce0xeb0xff });
    NAMES.put("slateblue"new int[] { 0x6a0x5a0xcd0xff });
    NAMES.put("slategray"new int[] { 0x700x800x900xff });
    NAMES.put("snow"new int[] { 0xff0xfa0xfa0xff });
    NAMES.put("springgreen"new int[] { 0x000xff0x7f0xff });
    NAMES.put("steelblue"new int[] { 0x460x820xb40xff });
    NAMES.put("tan"new int[] { 0xd20xb40x8c0xff });
    NAMES.put("teal"new int[] { 0x000x800x800xff });
    NAMES.put("thistle"new int[] { 0xd80xbf0xd80xff });
    NAMES.put("tomato"new int[] { 0xff0x630x470xff });
    NAMES.put("transparent"new int[] { 0x000x000x000x00 });
    NAMES.put("turquoise"new int[] { 0x400xe00xd00xff });
    NAMES.put("violet"new int[] { 0xee0x820xee0xff });
    NAMES.put("wheat"new int[] { 0xf50xde0xb30xff });
    NAMES.put("white"new int[] { 0xff0xff0xff0xff });
    NAMES.put("whitesmoke"new int[] { 0xf50xf50xf50xff });
    NAMES.put("yellow"new int[] { 0xff0xff0x000xff });
    NAMES.put("yellowgreen"new int[] { 0x9a0xcd0x320xff });
  }

  /**
   * A web color string without the leading # will be 3 or 6 characters long
   * and all those characters will be hex digits.
   * NOTE: colStr must be all lower case or the current hex letter test will fail.
   @param colStr A non-null, lower case string that might describe an RGB color in hex.
   @return Is this a web color hex string without the leading #?
   @since 5.0.6
   */
  private static boolean missingHashColorFormat(String colStr) {
      int len = colStr.length();
      if (len == || len == 6) {
          // and it just contains hex chars 0-9, a-f, A-F
          String match = "[0-9a-f]{" + len + "}";
          return colStr.matches(match);
      }
      return false;
  }

  /**
   * 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)
      throws IllegalArgumentException {
    int[] c = 000255 };
    name = name.toLowerCase();
    boolean 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(01);
        c[0= Integer.parseInt(s+s, 16);
        String s2 = name.substring(12);
                c[1= Integer.parseInt(s2+s2, 16);
        String s3 = name.substring(2);
                c[2= Integer.parseInt(s3+s3, 16);
        return new BaseColor(c[0], c[1], c[2], c[3]);
      }
      if (name.length() == 6) {
        c[0= Integer.parseInt(name.substring(02)16);
        c[1= Integer.parseInt(name.substring(24)16);
        c[2= Integer.parseInt(name.substring(4)16);
        return new BaseColor(c[0], c[1], c[2], c[3]);
      }
      throw new IllegalArgumentException(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= Integer.parseInt(v.substring(0, v.length() 1)) 255 100;
                else
                    c[k= Integer.parseInt(v);
                if (c[k0)
                    c[k0;
                else if (c[k255)
                    c[k255;
            }
            return new BaseColor(c[0], c[1], c[2], c[3]);
        }

    if (!NAMES.containsKey(name))
      // TODO localize this error message.
      throw new IllegalArgumentException("Color '" + name
          "' not found.");
    c = NAMES.get(name);
    return new BaseColor(c[0], c[1], c[2], c[3]);
  }
}