De la manera sencilla:
Código Javascript
:
Ver originalpublic void test(){
String hex = "#009eb0";
Color color = toColor(hex);
System.out.println(color);
}
public Color toColor(String hex) {
return Color.decode(hex);
}
De la manera "manual":
Código Javascript
:
Ver originalpublic void test(){
String hex = "#009eb0";
RGB rgb = toRGB(hex);
System.out.println(rgb.getRed() + " : " + rgb.getGreen() + " : " + rgb.getBlue());
}
public RGB toRGB(String hex) {
RGB rgb = new RGB();
String tmp = hex.replaceAll("#", "");
if (tmp.length() == 6) {
rgb.setRed(Integer.decode("#" + tmp.substring(0, 2)));
rgb.setGreen(Integer.decode("#" + tmp.substring(2, 4)));
rgb.setBlue(Integer.decode("#" + tmp.substring(4, 6)));
}
return rgb;
}
public class RGB {
private int red;
private int green;
private int blue;
public RGB() {
}
public RGB(int red, int green, int blue) {
this.red = red;
this.green = green;
this.blue = blue;
}
public int getRed() {
return red;
}
public void setRed(int red) {
this.red = red;
}
public int getGreen() {
return green;
}
public void setGreen(int green) {
this.green = green;
}
public int getBlue() {
return blue;
}
public void setBlue(int blue) {
this.blue = blue;
}
public Color getColor() {
return new Color(red, green, blue);
}
}