Source of ColourNames.java


  1: import java.awt.Color;
  2: import java.util.HashMap;
  3: import java.util.Map;

  5: /**
  6:  * A class to replace calls to javafx.scene.paint.color.web. That method is not 
  7:  * available in the latest versions of the JDK. The method offered here (get) 
  8:  * only returns named colours. It does not deal with rgb, hsl, or hexadecimal 
  9:  * codes.
 10:  * 
 11:  * @author Mark Young (A00000000)
 12:  */
 13: public class ColourNames {
 14:     
 15:     /**
 16:      * Get the java.awt.Color named in the given String. The name is not case
 17:      * sensitive.
 18:      * <p>
 19:      * The names and colour values are taken from the Wikipedia page
 20:      * https://en.wikipedia.org/wiki/Web_colors
 21:      * 
 22:      * @param name the name of the colour (case insensitive)
 23:      * @return the java.awt.Color corresponding to that name, or null if there
 24:      * is no colour with that name
 25:      */
 26:     public static Color get(String name) {
 27:         return namedColours.get(name.toLowerCase());
 28:     }

 30:     private static final Map<String, Color> namedColours
 31:             = allNamedColours();

 33:     /**
 34:      * Make and return a map of all the named web colours.
 35:      * 
 36:      * @return a map from colour names to colours
 37:      */
 38:     private static Map<String, Color> allNamedColours() {
 39:         Map<String, Color> result = new HashMap<>();

 41:         // pink colours
 42:         save(result, "MediumVioletRed", new Color(199, 21, 133));
 43:         save(result, "DeepPink", new Color(255, 20, 147));
 44:         save(result, "PaleVioletRed", new Color(219, 112, 147));
 45:         save(result, "HotPink", new Color(255, 105, 180));
 46:         save(result, "LightPink", new Color(255, 182, 193));
 47:         save(result, "Pink", new Color(255, 192, 203));

 49:         // red colours
 50:         save(result, "DarkRed", new Color(139, 0, 0));
 51:         save(result, "Red", new Color(255, 0, 0));
 52:         save(result, "Firebrick", new Color(178, 34, 34));
 53:         save(result, "Crimson", new Color(220, 20, 60));
 54:         save(result, "IndianRed", new Color(205, 92, 92));
 55:         save(result, "LightCoral", new Color(240, 128, 128));
 56:         save(result, "Salmon", new Color(250, 128, 114));
 57:         save(result, "DarkSalmon", new Color(233, 150, 122));
 58:         save(result, "LightSalmon", new Color(255, 160, 122));

 60:         // orange colours
 61:         save(result, "OrangeRed", new Color(255, 69, 0));
 62:         save(result, "Tomato", new Color(255, 99, 71));
 63:         save(result, "DarkOrange", new Color(255, 140, 0));
 64:         save(result, "Coral", new Color(255, 127, 80));
 65:         save(result, "Orange", new Color(255, 165, 0));

 67:         // yellow colours
 68:         save(result, "DarkKhaki", new Color(189, 183, 107));
 69:         save(result, "Gold", new Color(255, 215, 0));
 70:         save(result, "Khaki", new Color(240, 230, 140));
 71:         save(result, "PeachPuff", new Color(255, 218, 185));
 72:         save(result, "Yellow", new Color(255, 255, 0));
 73:         save(result, "PaleGoldenrod", new Color(238, 232, 170));
 74:         save(result, "Moccasin", new Color(255, 228, 181));
 75:         save(result, "PapayaWhip", new Color(255, 239, 213));
 76:         save(result, "LightGoldenrodYellow", new Color(250, 250, 210));
 77:         save(result, "LemonChiffon", new Color(255, 250, 205));
 78:         save(result, "LightYellow", new Color(255, 255, 224));

 80:         // brown colours
 81:         save(result, "Maroon", new Color(128, 0, 0));
 82:         save(result, "Brown", new Color(165, 42, 42));
 83:         save(result, "SaddleBrown", new Color(139, 69, 19));
 84:         save(result, "Sienna", new Color(160, 82, 45));
 85:         save(result, "Chocolate", new Color(210, 105, 30));
 86:         save(result, "DarkGoldenrod", new Color(184, 134, 11));
 87:         save(result, "Peru", new Color(205, 133, 63));
 88:         save(result, "RosyBrown", new Color(188, 143, 143));
 89:         save(result, "Goldenrod", new Color(218, 165, 32));
 90:         save(result, "SandyBrown", new Color(244, 164, 96));
 91:         save(result, "Tan", new Color(210, 180, 140));
 92:         save(result, "Burlywood", new Color(222, 184, 135));
 93:         save(result, "Wheat", new Color(245, 222, 179));
 94:         save(result, "NavajoWhite", new Color(255, 222, 173));
 95:         save(result, "Bisque", new Color(255, 228, 196));
 96:         save(result, "BlanchedAlmond", new Color(255, 235, 205));
 97:         save(result, "Cornsilk", new Color(255, 248, 220));

 99:         // green colours
100:         save(result, "DarkGreen", new Color(0, 100, 0));
101:         save(result, "Green", new Color(0, 128, 0));
102:         save(result, "DarkOliveGreen", new Color(85, 107, 47));
103:         save(result, "ForestGreen", new Color(34, 139, 34));
104:         save(result, "SeaGreen", new Color(46, 139, 87));
105:         save(result, "Olive", new Color(128, 128, 0));
106:         save(result, "OliveDrab", new Color(107, 142, 35));
107:         save(result, "MediumSeaGreen", new Color(60, 179, 113));
108:         save(result, "LimeGreen", new Color(50, 205, 50));
109:         save(result, "Lime", new Color(0, 255, 0));
110:         save(result, "SpringGreen", new Color(0, 255, 127));
111:         save(result, "MediumSpringGreen", new Color(0, 250, 154));
112:         save(result, "DarkSeaGreen", new Color(143, 188, 143));
113:         save(result, "MediumAquamarine", new Color(102, 205, 170));
114:         save(result, "YellowGreen", new Color(154, 205, 50));
115:         save(result, "LawnGreen", new Color(124, 252, 0));
116:         save(result, "Chartreuse", new Color(127, 255, 0));
117:         save(result, "LightGreen", new Color(144, 238, 144));
118:         save(result, "GreenYellow", new Color(173, 255, 47));
119:         save(result, "PaleGreen", new Color(152, 251, 152));

121:         // cyan colours
122:         save(result, "Teal", new Color(0, 128, 128));
123:         save(result, "DarkCyan", new Color(0, 139, 139));
124:         save(result, "LightSeaGreen", new Color(32, 178, 170));
125:         save(result, "CadetBlue", new Color(95, 158, 160));
126:         save(result, "DarkTurquoise", new Color(0, 206, 209));
127:         save(result, "MediumTurquoise", new Color(72, 209, 204));
128:         save(result, "Turquoise", new Color(64, 224, 208));
129:         save(result, "Aqua", new Color(0, 255, 255));
130:         save(result, "Cyan", new Color(0, 255, 255));
131:         save(result, "Aquamarine", new Color(127, 255, 212));
132:         save(result, "PaleTurquoise", new Color(175, 238, 238));
133:         save(result, "LightCyan", new Color(224, 255, 255));

135:         // blue colours
136:         save(result, "Navy", new Color(0, 0, 128));
137:         save(result, "DarkBlue", new Color(0, 0, 139));
138:         save(result, "MediumBlue", new Color(0, 0, 205));
139:         save(result, "Blue", new Color(0, 0, 255));
140:         save(result, "MidnightBlue", new Color(25, 25, 112));
141:         save(result, "RoyalBlue", new Color(65, 105, 225));
142:         save(result, "SteelBlue", new Color(70, 130, 180));
143:         save(result, "DodgerBlue", new Color(30, 144, 255));
144:         save(result, "DeepSkyBlue", new Color(0, 191, 255));
145:         save(result, "CornflowerBlue", new Color(100, 149, 237));
146:         save(result, "SkyBlue", new Color(135, 206, 235));
147:         save(result, "LightSkyBlue", new Color(135, 206, 250));
148:         save(result, "LightSteelBlue", new Color(176, 196, 222));
149:         save(result, "LightBlue", new Color(173, 216, 230));
150:         save(result, "PowderBlue", new Color(176, 224, 230));

152:         // purple colours
153:         save(result, "Indigo", new Color(75, 0, 130));
154:         save(result, "Purple", new Color(128, 0, 128));
155:         save(result, "DarkMagenta", new Color(139, 0, 139));
156:         save(result, "DarkViolet", new Color(148, 0, 211));
157:         save(result, "DarkSlateBlue", new Color(72, 61, 139));
158:         save(result, "BlueViolet", new Color(138, 43, 226));
159:         save(result, "DarkOrchid", new Color(153, 50, 204));
160:         save(result, "Fuchsia", new Color(255, 0, 255));
161:         save(result, "Magenta", new Color(255, 0, 255));
162:         save(result, "SlateBlue", new Color(106, 90, 205));
163:         save(result, "MediumSlateBlue", new Color(123, 104, 238));
164:         save(result, "MediumOrchid", new Color(186, 85, 211));
165:         save(result, "MediumPurple", new Color(147, 112, 219));
166:         save(result, "Orchid", new Color(218, 112, 214));
167:         save(result, "Violet", new Color(238, 130, 238));
168:         save(result, "Plum", new Color(221, 160, 221));
169:         save(result, "Thistle", new Color(216, 191, 216));
170:         save(result, "Lavender", new Color(230, 230, 250));

172:         // white colours
173:         save(result, "MistyRose", new Color(255, 228, 225));
174:         save(result, "AntiqueWhite", new Color(250, 235, 215));
175:         save(result, "Linen", new Color(250, 240, 230));
176:         save(result, "Beige", new Color(245, 245, 220));
177:         save(result, "WhiteSmoke", new Color(245, 245, 245));
178:         save(result, "LavenderBlush", new Color(255, 240, 245));
179:         save(result, "OldLace", new Color(253, 245, 230));
180:         save(result, "AliceBlue", new Color(240, 248, 255));
181:         save(result, "Seashell", new Color(255, 245, 238));
182:         save(result, "GhostWhite", new Color(248, 248, 255));
183:         save(result, "Honeydew", new Color(240, 255, 240));
184:         save(result, "FloralWhite", new Color(255, 250, 240));
185:         save(result, "Azure", new Color(240, 255, 255));
186:         save(result, "MintCream", new Color(245, 255, 250));
187:         save(result, "Snow", new Color(255, 250, 250));
188:         save(result, "Ivory", new Color(255, 255, 240));
189:         save(result, "White", new Color(255, 255, 255));

191:         // grey colours
192:         save(result, "Black", new Color(0, 0, 0));
193:         save(result, "DarkSlateGray", new Color(47, 79, 79));
194:         save(result, "DimGray", new Color(105, 105, 105));
195:         save(result, "SlateGray", new Color(112, 128, 144));
196:         save(result, "Gray", new Color(128, 128, 128));
197:         save(result, "LightSlateGray", new Color(119, 136, 153));
198:         save(result, "DarkGray", new Color(169, 169, 169));
199:         save(result, "Silver", new Color(192, 192, 192));
200:         save(result, "LightGray", new Color(211, 211, 211));
201:         save(result, "Gainsboro", new Color(220, 220, 220));

203:         return result;
204:     }

206:     /**
207:      * Save the named colour in the given map, using the lower case version of
208:      * the name. This allows later look-ups to be case-insensitive.
209:      * 
210:      * @param map the map to save the named colour in
211:      * @param name the name of the colour
212:      * @param color the colour
213:      */
214:     private static void save(Map<String, Color> map, String name, Color color) {
215:         map.put(name.toLowerCase(), color);
216:     }
217:     
218: }