public static final String colorToHex(Color color) {

    String colorStr = "#";

    // Red
    String str = Integer.toHexString(color.getRed());
    if (str.length() > 2)
      throw new Error("invalid red value");
    else if (str.length() < 2)
      colorStr += "0" + str;
    else
      colorStr += str;

    // Green
    str = Integer.toHexString(color.getGreen());
    if (str.length() > 2)
      throw new Error("invalid green value");
    else if (str.length() < 2)
      colorStr += "0" + str;
    else
      colorStr += str;

    // Blue
    str = Integer.toHexString(color.getBlue());
    if (str.length() > 2)
      throw new Error("invalid green value");
    else if (str.length() < 2)
      colorStr += "0" + str;
    else
      colorStr += str;
    return colorStr.toUpperCase();
  }