public class ColorRechner {
private DecimalFormat df = new DecimalFormat("#");
public String RGBinHEX(int red, int green, int blue) {
String[] hex = new String[3];
hex[0] = Integer.toHexString(red).toUpperCase();
hex[1] = Integer.toHexString(green).toUpperCase();
hex[2] = Integer.toHexString(blue).toUpperCase();
for (int i = hex.length - 1; i > -1; i--) {
hex[i] = ((hex[i].length() < 2) ? "0" + hex[i] : hex[i]);
}
return hex[0] + hex[1] + hex[2];
}
public float[] RGBinHSB(int red, int green, int blue) {
float[] hsb = new float[3];
hsb = Color.RGBtoHSB(red, green, blue, new float[3]);
hsb[0] = hsb[0] * 360;
hsb[1] = hsb[1] * 100;
hsb[2] = hsb[2] * 100;
return hsb;
}
public int[] HSBinRGB(float hue, float saturation, float brightness) {
int[] rgb = new int[3];
Color col = Color.getHSBColor(hue / 360, saturation / 100, brightness / 100);
rgb[0] = col.getRed();
rgb[1] = col.getGreen();
rgb[2] = col.getBlue();
return rgb;
}
public int[] HEXinRGB(String hex) {
int[] rgb = new int[3];
for (int i = 0; i < rgb.length; i++) {
rgb[i] = Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
}
return rgb;
}
public String HSBinHEX(float hue, float saturation, float brightness) {
int[] rgb = HSBinRGB(hue, saturation, brightness);
return RGBinHEX(rgb[0], rgb[1], rgb[2]);
}
public float[] HEXinHSB(String hex) {
int[] rgb = HEXinRGB(hex);
return RGBinHSB(rgb[0], rgb[1], rgb[2]);
}
public float[] RGBinCMYK(int red, int green, int blue) {
float[] rgb = {red, green, blue};
float[] cmyk = new float[4];
float highest = ((red >= green && red >= blue) ? red : ((green > blue) ? green : blue));
cmyk[3] = 1 - highest / 255;
for (int i = rgb.length - 1; i > -1; i--) {
if (rgb[i] == highest) {
cmyk[i] = 0;
}
else {
cmyk[i] = 1 - rgb[i] / highest;
}
}
return cmyk;
}
public int[] CMYKinRGB(float cyan, float magenta, float yellow, float key) {
int[] rgb = new int[3];
float[] cmyk = {cyan, magenta, yellow, key};
for (int i = rgb.length - 1; i > -1; i--) {
rgb[i] = Integer.parseInt(df.format((float)(((cmyk[i] == 0) ? (1 - cmyk[3]) * 255 : (1 - cmyk[3]) * 255) * (1 - cmyk[i]))));
}
return rgb;
}
public float[] HEXinCMYK(String hex) {
int[] rgb = HEXinRGB(hex);
return RGBinCMYK(rgb[0], rgb[1], rgb[2]);
}
public float[] HSBinCMYK(float hue, float saturation, float brightness) {
int[] rgb = HSBinRGB(hue, saturation, brightness);
return RGBinCMYK(rgb[0], rgb[1], rgb[2]);
}
public float[] CMYKinHSB(float cyan, float magenta, float yellow, float key) {
int[] rgb = CMYKinRGB(cyan, magenta, yellow, key);
return RGBinHSB(rgb[0], rgb[1], rgb[2]);
}
public String CMYKinHEX(float cyan, float magenta, float yellow, float key) {
int[] rgb = CMYKinRGB(cyan, magenta, yellow, key);
return RGBinHEX(rgb[0], rgb[1], rgb[2]);
}
}