views:

15

answers:

1

Is there a way to get the html color code from a JColorChooser

My java Applet takes three colors from the user and averages them and displays the color

I want to get the html color code after they look at the average color

how can I do that

+1  A: 

Write a method to convert a Color to a String.

An HTML color code is just the R, G, and B values converted to hex and displayed as a string with a pound sign in front. This is a fairly simple method to write.

public static String toHexString(Color c) {
  StringBuilder sb = new StringBuilder('#');

  if (c.getRed() < 16) sb.append('0');
  sb.append(Integer.toHexString(c.getRed()));

  if (c.getGreen() < 16) sb.append('0');
  sb.append(Integer.toHexString(c.getGreen()));

  if (c.getBlue() < 16) sb.append('0');
  sb.append(Integer.toHexString(c.getBlue()));

  return sb.toString();
}
Erick Robertson
thanks it worked
Luck