001 /*--------------------------------------------------------------------------+
002 $Id: ColorUtils.java 26283 2010-02-18 11:18:57Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.commons.color;
019
020 import java.awt.Color;
021 import java.util.regex.Pattern;
022
023 import edu.tum.cs.commons.enums.EnumUtils;
024
025 /**
026 * Methods for handling colors.
027 *
028 * @author Benjamin Hummel
029 * @author $Author: juergens $
030 * @version $Rev: 26283 $
031 * @levd.rating GREEN Hash: 38B8ECE8FE52143FC23C834589C88499
032 */
033 public class ColorUtils {
034
035 /** Pattern used for finding */
036 private static Pattern HTML_COLOR_PATTERN = Pattern.compile("#[0-9a-f]{6}",
037 Pattern.CASE_INSENSITIVE);
038
039 /**
040 * Returns a color from a string. If the string could not be decoded, null
041 * is returned. This methods supports the html color format (i.e. #RRGGBB)
042 * and some predefined names (e.g. red, green, etc.).
043 */
044 public static Color fromString(String s) {
045
046 if (HTML_COLOR_PATTERN.matcher(s).matches()) {
047 return Color.decode("0x" + s.substring(1));
048 }
049
050 if (s.startsWith("ccsm-")) {
051 ECCSMColor color = EnumUtils.valueOfIgnoreCase(ECCSMColor.class, s
052 .substring(5));
053 if (color != null) {
054 return color.getColor();
055 }
056 }
057
058 EAWTColors color = EnumUtils.valueOfIgnoreCase(EAWTColors.class, s);
059 if (color != null) {
060 return color.getColor();
061 }
062
063 return null;
064 }
065
066 /** List of colors defined in AWT used as a lookup table. */
067 private static enum EAWTColors {
068
069 /** Red */
070 RED(Color.RED),
071
072 /** Green */
073 GREEN(Color.GREEN),
074
075 /** Blue */
076 BLUE(Color.BLUE),
077
078 /** Yellow */
079 YELLOW(Color.YELLOW),
080
081 /** Orange */
082 ORANGE(Color.ORANGE),
083
084 /** White */
085 WHITE(Color.WHITE),
086
087 /** Black */
088 BLACK(Color.BLACK),
089
090 /** Gray */
091 GRAY(Color.GRAY),
092
093 /** Cyan */
094 CYAN(Color.CYAN),
095
096 /** Magenta */
097 MAGENTA(Color.MAGENTA);
098
099 /** The color actual color. */
100 private final Color color;
101
102 /** Constructor. */
103 private EAWTColors(Color color) {
104 this.color = color;
105 }
106
107 /** Returns the color for a enum constant. */
108 public Color getColor() {
109 return color;
110 }
111 }
112 }