001/*
002 * $Id: WebColors.java 4784 2011-03-15 08:33:00Z blowagie $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text.html;
045
046import java.util.HashMap;
047import java.util.StringTokenizer;
048
049import com.itextpdf.text.BaseColor;
050import com.itextpdf.text.error_messages.MessageLocalization;
051
052/**
053 * This class is a HashMap that contains the names of colors as a key and the
054 * corresponding BaseColor as value. (Source: Wikipedia
055 * http://en.wikipedia.org/wiki/Web_colors )
056 *
057 * @author blowagie
058 */
059public class WebColors extends HashMap<String, int[]> {
060
061        private static final long serialVersionUID = 3542523100813372896L;
062        /** HashMap containing all the names and corresponding color values. */
063        public static final WebColors NAMES = new WebColors();
064        static {
065                NAMES.put("aliceblue", new int[] { 0xf0, 0xf8, 0xff, 0xff });
066                NAMES.put("antiquewhite", new int[] { 0xfa, 0xeb, 0xd7, 0xff });
067                NAMES.put("aqua", new int[] { 0x00, 0xff, 0xff, 0xff });
068                NAMES.put("aquamarine", new int[] { 0x7f, 0xff, 0xd4, 0xff });
069                NAMES.put("azure", new int[] { 0xf0, 0xff, 0xff, 0xff });
070                NAMES.put("beige", new int[] { 0xf5, 0xf5, 0xdc, 0xff });
071                NAMES.put("bisque", new int[] { 0xff, 0xe4, 0xc4, 0xff });
072                NAMES.put("black", new int[] { 0x00, 0x00, 0x00, 0xff });
073                NAMES.put("blanchedalmond", new int[] { 0xff, 0xeb, 0xcd, 0xff });
074                NAMES.put("blue", new int[] { 0x00, 0x00, 0xff, 0xff });
075                NAMES.put("blueviolet", new int[] { 0x8a, 0x2b, 0xe2, 0xff });
076                NAMES.put("brown", new int[] { 0xa5, 0x2a, 0x2a, 0xff });
077                NAMES.put("burlywood", new int[] { 0xde, 0xb8, 0x87, 0xff });
078                NAMES.put("cadetblue", new int[] { 0x5f, 0x9e, 0xa0, 0xff });
079                NAMES.put("chartreuse", new int[] { 0x7f, 0xff, 0x00, 0xff });
080                NAMES.put("chocolate", new int[] { 0xd2, 0x69, 0x1e, 0xff });
081                NAMES.put("coral", new int[] { 0xff, 0x7f, 0x50, 0xff });
082                NAMES.put("cornflowerblue", new int[] { 0x64, 0x95, 0xed, 0xff });
083                NAMES.put("cornsilk", new int[] { 0xff, 0xf8, 0xdc, 0xff });
084                NAMES.put("crimson", new int[] { 0xdc, 0x14, 0x3c, 0xff });
085                NAMES.put("cyan", new int[] { 0x00, 0xff, 0xff, 0xff });
086                NAMES.put("darkblue", new int[] { 0x00, 0x00, 0x8b, 0xff });
087                NAMES.put("darkcyan", new int[] { 0x00, 0x8b, 0x8b, 0xff });
088                NAMES.put("darkgoldenrod", new int[] { 0xb8, 0x86, 0x0b, 0xff });
089                NAMES.put("darkgray", new int[] { 0xa9, 0xa9, 0xa9, 0xff });
090                NAMES.put("darkgreen", new int[] { 0x00, 0x64, 0x00, 0xff });
091                NAMES.put("darkkhaki", new int[] { 0xbd, 0xb7, 0x6b, 0xff });
092                NAMES.put("darkmagenta", new int[] { 0x8b, 0x00, 0x8b, 0xff });
093                NAMES.put("darkolivegreen", new int[] { 0x55, 0x6b, 0x2f, 0xff });
094                NAMES.put("darkorange", new int[] { 0xff, 0x8c, 0x00, 0xff });
095                NAMES.put("darkorchid", new int[] { 0x99, 0x32, 0xcc, 0xff });
096                NAMES.put("darkred", new int[] { 0x8b, 0x00, 0x00, 0xff });
097                NAMES.put("darksalmon", new int[] { 0xe9, 0x96, 0x7a, 0xff });
098                NAMES.put("darkseagreen", new int[] { 0x8f, 0xbc, 0x8f, 0xff });
099                NAMES.put("darkslateblue", new int[] { 0x48, 0x3d, 0x8b, 0xff });
100                NAMES.put("darkslategray", new int[] { 0x2f, 0x4f, 0x4f, 0xff });
101                NAMES.put("darkturquoise", new int[] { 0x00, 0xce, 0xd1, 0xff });
102                NAMES.put("darkviolet", new int[] { 0x94, 0x00, 0xd3, 0xff });
103                NAMES.put("deeppink", new int[] { 0xff, 0x14, 0x93, 0xff });
104                NAMES.put("deepskyblue", new int[] { 0x00, 0xbf, 0xff, 0xff });
105                NAMES.put("dimgray", new int[] { 0x69, 0x69, 0x69, 0xff });
106                NAMES.put("dodgerblue", new int[] { 0x1e, 0x90, 0xff, 0xff });
107                NAMES.put("firebrick", new int[] { 0xb2, 0x22, 0x22, 0xff });
108                NAMES.put("floralwhite", new int[] { 0xff, 0xfa, 0xf0, 0xff });
109                NAMES.put("forestgreen", new int[] { 0x22, 0x8b, 0x22, 0xff });
110                NAMES.put("fuchsia", new int[] { 0xff, 0x00, 0xff, 0xff });
111                NAMES.put("gainsboro", new int[] { 0xdc, 0xdc, 0xdc, 0xff });
112                NAMES.put("ghostwhite", new int[] { 0xf8, 0xf8, 0xff, 0xff });
113                NAMES.put("gold", new int[] { 0xff, 0xd7, 0x00, 0xff });
114                NAMES.put("goldenrod", new int[] { 0xda, 0xa5, 0x20, 0xff });
115                NAMES.put("gray", new int[] { 0x80, 0x80, 0x80, 0xff });
116                NAMES.put("green", new int[] { 0x00, 0x80, 0x00, 0xff });
117                NAMES.put("greenyellow", new int[] { 0xad, 0xff, 0x2f, 0xff });
118                NAMES.put("honeydew", new int[] { 0xf0, 0xff, 0xf0, 0xff });
119                NAMES.put("hotpink", new int[] { 0xff, 0x69, 0xb4, 0xff });
120                NAMES.put("indianred", new int[] { 0xcd, 0x5c, 0x5c, 0xff });
121                NAMES.put("indigo", new int[] { 0x4b, 0x00, 0x82, 0xff });
122                NAMES.put("ivory", new int[] { 0xff, 0xff, 0xf0, 0xff });
123                NAMES.put("khaki", new int[] { 0xf0, 0xe6, 0x8c, 0xff });
124                NAMES.put("lavender", new int[] { 0xe6, 0xe6, 0xfa, 0xff });
125                NAMES.put("lavenderblush", new int[] { 0xff, 0xf0, 0xf5, 0xff });
126                NAMES.put("lawngreen", new int[] { 0x7c, 0xfc, 0x00, 0xff });
127                NAMES.put("lemonchiffon", new int[] { 0xff, 0xfa, 0xcd, 0xff });
128                NAMES.put("lightblue", new int[] { 0xad, 0xd8, 0xe6, 0xff });
129                NAMES.put("lightcoral", new int[] { 0xf0, 0x80, 0x80, 0xff });
130                NAMES.put("lightcyan", new int[] { 0xe0, 0xff, 0xff, 0xff });
131                NAMES.put("lightgoldenrodyellow", new int[] { 0xfa, 0xfa, 0xd2, 0xff });
132                NAMES.put("lightgreen", new int[] { 0x90, 0xee, 0x90, 0xff });
133                NAMES.put("lightgrey", new int[] { 0xd3, 0xd3, 0xd3, 0xff });
134                NAMES.put("lightpink", new int[] { 0xff, 0xb6, 0xc1, 0xff });
135                NAMES.put("lightsalmon", new int[] { 0xff, 0xa0, 0x7a, 0xff });
136                NAMES.put("lightseagreen", new int[] { 0x20, 0xb2, 0xaa, 0xff });
137                NAMES.put("lightskyblue", new int[] { 0x87, 0xce, 0xfa, 0xff });
138                NAMES.put("lightslategray", new int[] { 0x77, 0x88, 0x99, 0xff });
139                NAMES.put("lightsteelblue", new int[] { 0xb0, 0xc4, 0xde, 0xff });
140                NAMES.put("lightyellow", new int[] { 0xff, 0xff, 0xe0, 0xff });
141                NAMES.put("lime", new int[] { 0x00, 0xff, 0x00, 0xff });
142                NAMES.put("limegreen", new int[] { 0x32, 0xcd, 0x32, 0xff });
143                NAMES.put("linen", new int[] { 0xfa, 0xf0, 0xe6, 0xff });
144                NAMES.put("magenta", new int[] { 0xff, 0x00, 0xff, 0xff });
145                NAMES.put("maroon", new int[] { 0x80, 0x00, 0x00, 0xff });
146                NAMES.put("mediumaquamarine", new int[] { 0x66, 0xcd, 0xaa, 0xff });
147                NAMES.put("mediumblue", new int[] { 0x00, 0x00, 0xcd, 0xff });
148                NAMES.put("mediumorchid", new int[] { 0xba, 0x55, 0xd3, 0xff });
149                NAMES.put("mediumpurple", new int[] { 0x93, 0x70, 0xdb, 0xff });
150                NAMES.put("mediumseagreen", new int[] { 0x3c, 0xb3, 0x71, 0xff });
151                NAMES.put("mediumslateblue", new int[] { 0x7b, 0x68, 0xee, 0xff });
152                NAMES.put("mediumspringgreen", new int[] { 0x00, 0xfa, 0x9a, 0xff });
153                NAMES.put("mediumturquoise", new int[] { 0x48, 0xd1, 0xcc, 0xff });
154                NAMES.put("mediumvioletred", new int[] { 0xc7, 0x15, 0x85, 0xff });
155                NAMES.put("midnightblue", new int[] { 0x19, 0x19, 0x70, 0xff });
156                NAMES.put("mintcream", new int[] { 0xf5, 0xff, 0xfa, 0xff });
157                NAMES.put("mistyrose", new int[] { 0xff, 0xe4, 0xe1, 0xff });
158                NAMES.put("moccasin", new int[] { 0xff, 0xe4, 0xb5, 0xff });
159                NAMES.put("navajowhite", new int[] { 0xff, 0xde, 0xad, 0xff });
160                NAMES.put("navy", new int[] { 0x00, 0x00, 0x80, 0xff });
161                NAMES.put("oldlace", new int[] { 0xfd, 0xf5, 0xe6, 0xff });
162                NAMES.put("olive", new int[] { 0x80, 0x80, 0x00, 0xff });
163                NAMES.put("olivedrab", new int[] { 0x6b, 0x8e, 0x23, 0xff });
164                NAMES.put("orange", new int[] { 0xff, 0xa5, 0x00, 0xff });
165                NAMES.put("orangered", new int[] { 0xff, 0x45, 0x00, 0xff });
166                NAMES.put("orchid", new int[] { 0xda, 0x70, 0xd6, 0xff });
167                NAMES.put("palegoldenrod", new int[] { 0xee, 0xe8, 0xaa, 0xff });
168                NAMES.put("palegreen", new int[] { 0x98, 0xfb, 0x98, 0xff });
169                NAMES.put("paleturquoise", new int[] { 0xaf, 0xee, 0xee, 0xff });
170                NAMES.put("palevioletred", new int[] { 0xdb, 0x70, 0x93, 0xff });
171                NAMES.put("papayawhip", new int[] { 0xff, 0xef, 0xd5, 0xff });
172                NAMES.put("peachpuff", new int[] { 0xff, 0xda, 0xb9, 0xff });
173                NAMES.put("peru", new int[] { 0xcd, 0x85, 0x3f, 0xff });
174                NAMES.put("pink", new int[] { 0xff, 0xc0, 0xcb, 0xff });
175                NAMES.put("plum", new int[] { 0xdd, 0xa0, 0xdd, 0xff });
176                NAMES.put("powderblue", new int[] { 0xb0, 0xe0, 0xe6, 0xff });
177                NAMES.put("purple", new int[] { 0x80, 0x00, 0x80, 0xff });
178                NAMES.put("red", new int[] { 0xff, 0x00, 0x00, 0xff });
179                NAMES.put("rosybrown", new int[] { 0xbc, 0x8f, 0x8f, 0xff });
180                NAMES.put("royalblue", new int[] { 0x41, 0x69, 0xe1, 0xff });
181                NAMES.put("saddlebrown", new int[] { 0x8b, 0x45, 0x13, 0xff });
182                NAMES.put("salmon", new int[] { 0xfa, 0x80, 0x72, 0xff });
183                NAMES.put("sandybrown", new int[] { 0xf4, 0xa4, 0x60, 0xff });
184                NAMES.put("seagreen", new int[] { 0x2e, 0x8b, 0x57, 0xff });
185                NAMES.put("seashell", new int[] { 0xff, 0xf5, 0xee, 0xff });
186                NAMES.put("sienna", new int[] { 0xa0, 0x52, 0x2d, 0xff });
187                NAMES.put("silver", new int[] { 0xc0, 0xc0, 0xc0, 0xff });
188                NAMES.put("skyblue", new int[] { 0x87, 0xce, 0xeb, 0xff });
189                NAMES.put("slateblue", new int[] { 0x6a, 0x5a, 0xcd, 0xff });
190                NAMES.put("slategray", new int[] { 0x70, 0x80, 0x90, 0xff });
191                NAMES.put("snow", new int[] { 0xff, 0xfa, 0xfa, 0xff });
192                NAMES.put("springgreen", new int[] { 0x00, 0xff, 0x7f, 0xff });
193                NAMES.put("steelblue", new int[] { 0x46, 0x82, 0xb4, 0xff });
194                NAMES.put("tan", new int[] { 0xd2, 0xb4, 0x8c, 0xff });
195                NAMES.put("teal", new int[] { 0x00, 0x80, 0x80, 0xff });
196                NAMES.put("thistle", new int[] { 0xd8, 0xbf, 0xd8, 0xff });
197                NAMES.put("tomato", new int[] { 0xff, 0x63, 0x47, 0xff });
198                NAMES.put("transparent", new int[] { 0x00, 0x00, 0x00, 0x00 });
199                NAMES.put("turquoise", new int[] { 0x40, 0xe0, 0xd0, 0xff });
200                NAMES.put("violet", new int[] { 0xee, 0x82, 0xee, 0xff });
201                NAMES.put("wheat", new int[] { 0xf5, 0xde, 0xb3, 0xff });
202                NAMES.put("white", new int[] { 0xff, 0xff, 0xff, 0xff });
203                NAMES.put("whitesmoke", new int[] { 0xf5, 0xf5, 0xf5, 0xff });
204                NAMES.put("yellow", new int[] { 0xff, 0xff, 0x00, 0xff });
205                NAMES.put("yellowgreen", new int[] { 0x9a, 0xcd, 0x32, 0xff });
206        }
207
208        /**
209         * A web color string without the leading # will be 3 or 6 characters long
210         * and all those characters will be hex digits.
211         * NOTE: colStr must be all lower case or the current hex letter test will fail.
212         * @param colStr A non-null, lower case string that might describe an RGB color in hex.
213         * @return Is this a web color hex string without the leading #?
214         * @since 5.0.6
215         */
216        private static boolean missingHashColorFormat(String colStr) {
217            int len = colStr.length();
218            if (len == 3 || len == 6) {
219                // and it just contains hex chars 0-9, a-f, A-F
220                String match = "[0-9a-f]{" + len + "}";
221                return colStr.matches(match);
222            }
223            return false;
224        }
225
226        /**
227         * Gives you a BaseColor based on a name.
228         *
229         * @param name
230         *            a name such as black, violet, cornflowerblue or #RGB or #RRGGBB
231     *            or RGB or RRGGBB or rgb(R,G,B)
232         * @return the corresponding BaseColor object.  Never returns null.
233         * @throws IllegalArgumentException
234         *             if the String isn't a know representation of a color.
235         */
236        public static BaseColor getRGBColor(String name)
237                        throws IllegalArgumentException {
238                int[] c = { 0, 0, 0, 255 };
239                name = name.toLowerCase();
240                boolean colorStrWithoutHash = missingHashColorFormat(name);
241                if (name.startsWith("#") || colorStrWithoutHash) {
242                    if (!colorStrWithoutHash) {
243                        name = name.substring(1); // lop off the # to unify hex parsing.
244                    }
245                        if (name.length() == 3) {
246                            String s = name.substring(0, 1);
247                                c[0] = Integer.parseInt(s+s, 16);
248                                String s2 = name.substring(1, 2);
249                c[1] = Integer.parseInt(s2+s2, 16);
250                                String s3 = name.substring(2);
251                c[2] = Integer.parseInt(s3+s3, 16);
252                                return new BaseColor(c[0], c[1], c[2], c[3]);
253                        }
254                        if (name.length() == 6) {
255                                c[0] = Integer.parseInt(name.substring(0, 2), 16);
256                                c[1] = Integer.parseInt(name.substring(2, 4), 16);
257                                c[2] = Integer.parseInt(name.substring(4), 16);
258                                return new BaseColor(c[0], c[1], c[2], c[3]);
259                        }
260                        throw new IllegalArgumentException(MessageLocalization.getComposedMessage("unknown.color.format.must.be.rgb.or.rrggbb"));
261                }
262        else if (name.startsWith("rgb(")) {
263            StringTokenizer tok = new StringTokenizer(name, "rgb(), \t\r\n\f");
264            for (int k = 0; k < 3; ++k) {
265                String v = tok.nextToken();
266                if (v.endsWith("%"))
267                    c[k] = Integer.parseInt(v.substring(0, v.length() - 1)) * 255 / 100;
268                else
269                    c[k] = Integer.parseInt(v);
270                if (c[k] < 0)
271                    c[k] = 0;
272                else if (c[k] > 255)
273                    c[k] = 255;
274            }
275            return new BaseColor(c[0], c[1], c[2], c[3]);
276        }
277
278                if (!NAMES.containsKey(name))
279                        // TODO localize this error message.
280                        throw new IllegalArgumentException("Color '" + name
281                                        + "' not found.");
282                c = NAMES.get(name);
283                return new BaseColor(c[0], c[1], c[2], c[3]);
284        }
285}