1 /* 2 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. 3 * All rights reserved. Use is subject to license terms. 4 * 5 * This file is available and licensed under the following license: 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * - Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * - Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the distribution. 16 * - Neither the name of Oracle Corporation nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package com.oracle.javafx.scenebuilder.kit.metadata.util; 33 34 import java.util.Collections; 35 import java.util.HashMap; 36 import java.util.Locale; 37 import java.util.Map; 38 import javafx.scene.paint.Color; 39 40 /** 41 * 42 */ 43 public class ColorEncoder { 44 45 private static Map<String, Color> standardColors; 46 private static Map<Color, String> standardColorNames; 47 48 public static String encodeColor(Color color) { 49 final String colorName = getStandardColorNames().get(color); 50 final String result; 51 52 if (colorName != null) { 53 result = colorName; 54 } else { 55 result = makeColorEncoding(color); 56 } 57 58 return result; 59 } 60 61 public static synchronized Map<String, Color> getStandardColors() { 62 63 if (standardColors == null) { 64 standardColors = new HashMap<>(); 65 66 standardColors.put("ALICEBLUE", Color.ALICEBLUE); //NOI18N 67 standardColors.put("ANTIQUEWHITE", Color.ANTIQUEWHITE); //NOI18N 68 standardColors.put("AQUA", Color.AQUA); //NOI18N 69 standardColors.put("AQUAMARINE", Color.AQUAMARINE); //NOI18N 70 standardColors.put("AZURE", Color.AZURE); //NOI18N 71 standardColors.put("BEIGE", Color.BEIGE); //NOI18N 72 standardColors.put("BISQUE", Color.BISQUE); //NOI18N 73 standardColors.put("BLACK", Color.BLACK); //NOI18N 74 standardColors.put("BLANCHEDALMOND", Color.BLANCHEDALMOND); //NOI18N 75 standardColors.put("BLUE", Color.BLUE); //NOI18N 76 standardColors.put("BLUEVIOLET", Color.BLUEVIOLET); //NOI18N 77 standardColors.put("BROWN", Color.BROWN); //NOI18N 78 standardColors.put("BURLYWOOD", Color.BURLYWOOD); //NOI18N 79 standardColors.put("CADETBLUE", Color.CADETBLUE); //NOI18N 80 standardColors.put("CHARTREUSE", Color.CHARTREUSE); //NOI18N 81 standardColors.put("CHOCOLATE", Color.CHOCOLATE); //NOI18N 82 standardColors.put("CORAL", Color.CORAL); //NOI18N 83 standardColors.put("CORNFLOWERBLUE", Color.CORNFLOWERBLUE); //NOI18N 84 standardColors.put("CORNSILK", Color.CORNSILK); //NOI18N 85 standardColors.put("CRIMSON", Color.CRIMSON); //NOI18N 86 standardColors.put("CYAN", Color.CYAN); //NOI18N 87 standardColors.put("DARKBLUE", Color.DARKBLUE); //NOI18N 88 standardColors.put("DARKCYAN", Color.DARKCYAN); //NOI18N 89 standardColors.put("DARKGOLDENROD", Color.DARKGOLDENROD); //NOI18N 90 standardColors.put("DARKGRAY", Color.DARKGRAY); //NOI18N 91 standardColors.put("DARKGREEN", Color.DARKGREEN); //NOI18N 92 standardColors.put("DARKGREY", Color.DARKGREY); //NOI18N 93 standardColors.put("DARKKHAKI", Color.DARKKHAKI); //NOI18N 94 standardColors.put("DARKMAGENTA", Color.DARKMAGENTA); //NOI18N 95 standardColors.put("DARKOLIVEGREEN", Color.DARKOLIVEGREEN); //NOI18N 96 standardColors.put("DARKORANGE", Color.DARKORANGE); //NOI18N 97 standardColors.put("DARKORCHID", Color.DARKORCHID); //NOI18N 98 standardColors.put("DARKRED", Color.DARKRED); //NOI18N 99 standardColors.put("DARKSALMON", Color.DARKSALMON); //NOI18N 100 standardColors.put("DARKSEAGREEN", Color.DARKSEAGREEN); //NOI18N 101 standardColors.put("DARKSLATEBLUE", Color.DARKSLATEBLUE); //NOI18N 102 standardColors.put("DARKSLATEGRAY", Color.DARKSLATEGRAY); //NOI18N 103 standardColors.put("DARKSLATEGREY", Color.DARKSLATEGREY); //NOI18N 104 standardColors.put("DARKTURQUOISE", Color.DARKTURQUOISE); //NOI18N 105 standardColors.put("DARKVIOLET", Color.DARKVIOLET); //NOI18N 106 standardColors.put("DEEPPINK", Color.DEEPPINK); //NOI18N 107 standardColors.put("DEEPSKYBLUE", Color.DEEPSKYBLUE); //NOI18N 108 standardColors.put("DIMGRAY", Color.DIMGRAY); //NOI18N 109 standardColors.put("DIMGREY", Color.DIMGREY); //NOI18N 110 standardColors.put("DODGERBLUE", Color.DODGERBLUE); //NOI18N 111 standardColors.put("FIREBRICK", Color.FIREBRICK); //NOI18N 112 standardColors.put("FLORALWHITE", Color.FLORALWHITE); //NOI18N 113 standardColors.put("FORESTGREEN", Color.FORESTGREEN); //NOI18N 114 standardColors.put("FUCHSIA", Color.FUCHSIA); //NOI18N 115 standardColors.put("GAINSBORO", Color.GAINSBORO); //NOI18N 116 standardColors.put("GHOSTWHITE", Color.GHOSTWHITE); //NOI18N 117 standardColors.put("GOLD", Color.GOLD); //NOI18N 118 standardColors.put("GOLDENROD", Color.GOLDENROD); //NOI18N 119 standardColors.put("GRAY", Color.GRAY); //NOI18N 120 standardColors.put("GREEN", Color.GREEN); //NOI18N 121 standardColors.put("GREENYELLOW", Color.GREENYELLOW); //NOI18N 122 standardColors.put("GREY", Color.GREY); //NOI18N 123 standardColors.put("HONEYDEW", Color.HONEYDEW); //NOI18N 124 standardColors.put("HOTPINK", Color.HOTPINK); //NOI18N 125 standardColors.put("INDIANRED", Color.INDIANRED); //NOI18N 126 standardColors.put("INDIGO", Color.INDIGO); //NOI18N 127 standardColors.put("IVORY", Color.IVORY); //NOI18N 128 standardColors.put("KHAKI", Color.KHAKI); //NOI18N 129 standardColors.put("LAVENDER", Color.LAVENDER); //NOI18N 130 standardColors.put("LAVENDERBLUSH", Color.LAVENDERBLUSH); //NOI18N 131 standardColors.put("LAWNGREEN", Color.LAWNGREEN); //NOI18N 132 standardColors.put("LEMONCHIFFON", Color.LEMONCHIFFON); //NOI18N 133 standardColors.put("LIGHTBLUE", Color.LIGHTBLUE); //NOI18N 134 standardColors.put("LIGHTCORAL", Color.LIGHTCORAL); //NOI18N 135 standardColors.put("LIGHTCYAN", Color.LIGHTCYAN); //NOI18N 136 standardColors.put("LIGHTGOLDENRODYELLOW", Color.LIGHTGOLDENRODYELLOW); //NOI18N 137 standardColors.put("LIGHTGRAY", Color.LIGHTGRAY); //NOI18N 138 standardColors.put("LIGHTGREEN", Color.LIGHTGREEN); //NOI18N 139 standardColors.put("LIGHTGREY", Color.LIGHTGREY); //NOI18N 140 standardColors.put("LIGHTPINK", Color.LIGHTPINK); //NOI18N 141 standardColors.put("LIGHTSALMON", Color.LIGHTSALMON); //NOI18N 142 standardColors.put("LIGHTSEAGREEN", Color.LIGHTSEAGREEN); //NOI18N 143 standardColors.put("LIGHTSKYBLUE", Color.LIGHTSKYBLUE); //NOI18N 144 standardColors.put("LIGHTSLATEGRAY", Color.LIGHTSLATEGRAY); //NOI18N 145 standardColors.put("LIGHTSLATEGREY", Color.LIGHTSLATEGREY); //NOI18N 146 standardColors.put("LIGHTSTEELBLUE", Color.LIGHTSTEELBLUE); //NOI18N 147 standardColors.put("LIGHTYELLOW", Color.LIGHTYELLOW); //NOI18N 148 standardColors.put("LIME", Color.LIME); //NOI18N 149 standardColors.put("LIMEGREEN", Color.LIMEGREEN); //NOI18N 150 standardColors.put("LINEN", Color.LINEN); //NOI18N 151 standardColors.put("MAGENTA", Color.MAGENTA); //NOI18N 152 standardColors.put("MAROON", Color.MAROON); //NOI18N 153 standardColors.put("MEDIUMAQUAMARINE", Color.MEDIUMAQUAMARINE); //NOI18N 154 standardColors.put("MEDIUMBLUE", Color.MEDIUMBLUE); //NOI18N 155 standardColors.put("MEDIUMORCHID", Color.MEDIUMORCHID); //NOI18N 156 standardColors.put("MEDIUMPURPLE", Color.MEDIUMPURPLE); //NOI18N 157 standardColors.put("MEDIUMSEAGREEN", Color.MEDIUMSEAGREEN); //NOI18N 158 standardColors.put("MEDIUMSLATEBLUE", Color.MEDIUMSLATEBLUE); //NOI18N 159 standardColors.put("MEDIUMSPRINGGREEN", Color.MEDIUMSPRINGGREEN); //NOI18N 160 standardColors.put("MEDIUMTURQUOISE", Color.MEDIUMTURQUOISE); //NOI18N 161 standardColors.put("MEDIUMVIOLETRED", Color.MEDIUMVIOLETRED); //NOI18N 162 standardColors.put("MIDNIGHTBLUE", Color.MIDNIGHTBLUE); //NOI18N 163 standardColors.put("MINTCREAM", Color.MINTCREAM); //NOI18N 164 standardColors.put("MISTYROSE", Color.MISTYROSE); //NOI18N 165 standardColors.put("MOCCASIN", Color.MOCCASIN); //NOI18N 166 standardColors.put("NAVAJOWHITE", Color.NAVAJOWHITE); //NOI18N 167 standardColors.put("NAVY", Color.NAVY); //NOI18N 168 standardColors.put("OLDLACE", Color.OLDLACE); //NOI18N 169 standardColors.put("OLIVE", Color.OLIVE); //NOI18N 170 standardColors.put("OLIVEDRAB", Color.OLIVEDRAB); //NOI18N 171 standardColors.put("ORANGE", Color.ORANGE); //NOI18N 172 standardColors.put("ORANGERED", Color.ORANGERED); //NOI18N 173 standardColors.put("ORCHID", Color.ORCHID); //NOI18N 174 standardColors.put("PALEGOLDENROD", Color.PALEGOLDENROD); //NOI18N 175 standardColors.put("PALEGREEN", Color.PALEGREEN); //NOI18N 176 standardColors.put("PALETURQUOISE", Color.PALETURQUOISE); //NOI18N 177 standardColors.put("PALEVIOLETRED", Color.PALEVIOLETRED); //NOI18N 178 standardColors.put("PAPAYAWHIP", Color.PAPAYAWHIP); //NOI18N 179 standardColors.put("PEACHPUFF", Color.PEACHPUFF); //NOI18N 180 standardColors.put("PERU", Color.PERU); //NOI18N 181 standardColors.put("PINK", Color.PINK); //NOI18N 182 standardColors.put("PLUM", Color.PLUM); //NOI18N 183 standardColors.put("POWDERBLUE", Color.POWDERBLUE); //NOI18N 184 standardColors.put("PURPLE", Color.PURPLE); //NOI18N 185 standardColors.put("RED", Color.RED); //NOI18N 186 standardColors.put("ROSYBROWN", Color.ROSYBROWN); //NOI18N 187 standardColors.put("ROYALBLUE", Color.ROYALBLUE); //NOI18N 188 standardColors.put("SADDLEBROWN", Color.SADDLEBROWN); //NOI18N 189 standardColors.put("SALMON", Color.SALMON); //NOI18N 190 standardColors.put("SANDYBROWN", Color.SANDYBROWN); //NOI18N 191 standardColors.put("SEAGREEN", Color.SEAGREEN); //NOI18N 192 standardColors.put("SEASHELL", Color.SEASHELL); //NOI18N 193 standardColors.put("SIENNA", Color.SIENNA); //NOI18N 194 standardColors.put("SILVER", Color.SILVER); //NOI18N 195 standardColors.put("SKYBLUE", Color.SKYBLUE); //NOI18N 196 standardColors.put("SLATEBLUE", Color.SLATEBLUE); //NOI18N 197 standardColors.put("SLATEGRAY", Color.SLATEGRAY); //NOI18N 198 standardColors.put("SLATEGREY", Color.SLATEGREY); //NOI18N 199 standardColors.put("SNOW", Color.SNOW); //NOI18N 200 standardColors.put("SPRINGGREEN", Color.SPRINGGREEN); //NOI18N 201 standardColors.put("STEELBLUE", Color.STEELBLUE); //NOI18N 202 standardColors.put("TAN", Color.TAN); //NOI18N 203 standardColors.put("TEAL", Color.TEAL); //NOI18N 204 standardColors.put("THISTLE", Color.THISTLE); //NOI18N 205 standardColors.put("TOMATO", Color.TOMATO); //NOI18N 206 standardColors.put("TRANSPARENT", Color.TRANSPARENT); //NOI18N 207 standardColors.put("TURQUOISE", Color.TURQUOISE); //NOI18N 208 standardColors.put("VIOLET", Color.VIOLET); //NOI18N 209 standardColors.put("WHEAT", Color.WHEAT); //NOI18N 210 standardColors.put("WHITE", Color.WHITE); //NOI18N 211 standardColors.put("WHITESMOKE", Color.WHITESMOKE); //NOI18N 212 standardColors.put("YELLOW", Color.YELLOW); //NOI18N 213 standardColors.put("YELLOWGREEN", Color.YELLOWGREEN); //NOI18N 214 215 standardColors = Collections.unmodifiableMap(standardColors); 216 } 217 218 return standardColors; 219 } 220 221 public static synchronized Map<Color, String> getStandardColorNames() { 222 223 if (standardColorNames == null) { 224 standardColorNames = new HashMap<>(); 225 for (Map.Entry<String, Color> e : getStandardColors().entrySet()) { 226 standardColorNames.put(e.getValue(), e.getKey()); 227 } 228 standardColorNames = Collections.unmodifiableMap(standardColorNames); 229 } 230 231 return standardColorNames; 232 } 233 234 235 public static String encodeColorToRGBA(Color color) { 236 final String result; 237 if (color == null) { 238 result = "null";//NOI18N 239 } else { 240 final int red = (int) (color.getRed() * 255); 241 final int green = (int) (color.getGreen() * 255); 242 final int blue = (int) (color.getBlue() * 255); 243 result = "rgba("+red+","+green+","+blue +","+color.getOpacity()+")";//NOI18N 244 } 245 return result; 246 } 247 248 /* 249 * Private 250 */ 251 private static String makeColorEncoding(Color c) { 252 final int red, green, blue, alpha; 253 final String result; 254 255 red = (int) Math.round(c.getRed() * 255.0); 256 green = (int) Math.round(c.getGreen() * 255.0); 257 blue = (int) Math.round(c.getBlue() * 255.0); 258 alpha = (int) Math.round(c.getOpacity() * 255.0); 259 if (alpha == 255) { 260 result = String.format((Locale)null, "#%02x%02x%02x", red, green, blue); //NOI18N 261 } else { 262 result = String.format((Locale)null, "#%02x%02x%02x%02x", red, green, blue, alpha); //NOI18N 263 } 264 265 return result; 266 } 267 268 }