001: // Copyright © 2006-2007 ASERT. Released under the Canoo Webtest license.
002: package com.canoo.webtest.plugins.exceltest;
003:
004: import java.util.HashMap;
005: import java.util.Map;
006:
007: import org.apache.poi.hssf.util.HSSFColor;
008:
009: /**
010: * Util class for converting a color index to a string in an Excel Spreadsheet.<p>
011: *
012: * @author Rob Nielsen
013: */
014: public class ExcelColorUtils {
015: private static final int AUTOMATIC_COLOR = 64;
016: private static final Map STANDARD_COLORS = new HashMap();
017:
018: static {
019: STANDARD_COLORS.put("#33cccc", "aqua");
020: STANDARD_COLORS.put("#000000", "black");
021: STANDARD_COLORS.put("#0000ff", "blue");
022: STANDARD_COLORS.put("#666699", "blue gray");
023: STANDARD_COLORS.put("#00ff00", "bright green");
024: STANDARD_COLORS.put("#993300", "brown");
025: STANDARD_COLORS.put("#ff8080", "coral");
026: STANDARD_COLORS.put("#000080", "dark blue");
027: STANDARD_COLORS.put("#003300", "dark green");
028: STANDARD_COLORS.put("#660066", "dark purple");
029: STANDARD_COLORS.put("#800000", "dark red");
030: STANDARD_COLORS.put("#003366", "dark teal");
031: STANDARD_COLORS.put("#808000", "dark yellow");
032: STANDARD_COLORS.put("#ffcc00", "gold");
033: STANDARD_COLORS.put("#008000", "green");
034: STANDARD_COLORS.put("#c0c0c0", "25% gray");
035: STANDARD_COLORS.put("#969696", "40% gray");
036: STANDARD_COLORS.put("#808080", "50% gray");
037: STANDARD_COLORS.put("#333333", "80% gray");
038: STANDARD_COLORS.put("#ccccff", "ice blue");
039: STANDARD_COLORS.put("#333399", "indigo");
040: STANDARD_COLORS.put("#ffffcc", "ivory");
041: STANDARD_COLORS.put("#cc99ff", "lavender");
042: STANDARD_COLORS.put("#3366ff", "light blue");
043: STANDARD_COLORS.put("#ccffcc", "light green");
044: STANDARD_COLORS.put("#ff9900", "light orange");
045: STANDARD_COLORS.put("#ccffff", "light turquoise");
046: STANDARD_COLORS.put("#ffff99", "light yellow");
047: STANDARD_COLORS.put("#99cc00", "lime");
048: STANDARD_COLORS.put("#993366", "maroon");
049: STANDARD_COLORS.put("#0066cc", "ocean blue");
050: STANDARD_COLORS.put("#333300", "olive green");
051: STANDARD_COLORS.put("#ff6600", "orange");
052: STANDARD_COLORS.put("#99ccff", "pale blue");
053: STANDARD_COLORS.put("#9999ff", "periwinkle");
054: STANDARD_COLORS.put("#ff00ff", "pink");
055: STANDARD_COLORS.put("#993366", "plum");
056: STANDARD_COLORS.put("#ff0000", "red");
057: STANDARD_COLORS.put("#ff99cc", "rose");
058: STANDARD_COLORS.put("#339966", "sea green");
059: STANDARD_COLORS.put("#00ccff", "sky blue");
060: STANDARD_COLORS.put("#ffcc99", "tan");
061: STANDARD_COLORS.put("#008080", "teal");
062: STANDARD_COLORS.put("#00ffff", "turquoise");
063: STANDARD_COLORS.put("#800080", "violet");
064: STANDARD_COLORS.put("#ffffff", "white");
065: STANDARD_COLORS.put("#ffff00", "yellow");
066: }
067:
068: public static String getColorName(final AbstractExcelStep step,
069: final short borderColor) {
070: if (borderColor == AUTOMATIC_COLOR) {
071: return "auto";
072: }
073: final HSSFColor color = step.getExcelWorkbook()
074: .getCustomPalette().getColor(borderColor);
075: if (color == null) {
076: return "none";
077: }
078: final short[] triplet = color.getTriplet();
079: final String colorString = "#" + toHex(triplet[0])
080: + toHex(triplet[1]) + toHex(triplet[2]);
081: return lookupStandardColorName(colorString);
082: }
083:
084: public static String lookupStandardColorName(
085: final String colorString) {
086: final String colorName = (String) STANDARD_COLORS
087: .get(colorString);
088: if (colorName != null) {
089: return colorName;
090: }
091: return colorString;
092: }
093:
094: private static String toHex(final short value) {
095: String ret = Integer.toHexString(value & 0xFF);
096: if (ret.length() == 1) {
097: ret = "0" + ret;
098: }
099: return ret;
100: }
101: }
|