0

I am trying to change color value to color hexa code. So, I code like this:

color = Integer.toHexString(colorpick.getValue().hashCode()).substring(0, 6).toUpperCase();

The above code is Ok for all colors except for "Black" color.

It gives the following error for "Black" color.

String index out of range : 6 error

Is any solution for my problem, please?

4
  • Okay, cool story, although the character development was lacking. Did you have a question? Commented Oct 5, 2016 at 3:29
  • Ask clearly!!! I think that you mean, you code give error for input value "Black". Is Right? Commented Oct 5, 2016 at 3:30
  • Check the return value of colorpick.getValue(). I think, the problem come from that part. Commented Oct 5, 2016 at 3:37
  • Yes, my question is why does it work for every color except the black Commented Oct 5, 2016 at 4:08

3 Answers 3

2

this is absolutely wrong here:

colorpick.getValue().hashCode()

hashcode is a specific code generated by the JVM to manage hash numbers related to instances and hash-tables... and has NOTHING to do with colors..

this should be more than ok

colorpick.getValue()
Sign up to request clarification or add additional context in comments.

3 Comments

When I run the code System.out.println(Integer.toHexString(Color.BLACK.hashCode()));, It give me result ff000000. So, I think that the problem is not from hashCode().
And hashCode method for color is from int java.awt.Color.hashCode(). So, It is related for use.
I get this error when i remove the substring, The method toHexString(int) in the type Integer is not applicable for the arguments (Color)
1

Since you did not mention what class the variable "colorpick" is, I'm going to assume it's ColorPicker (https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ColorPicker.html).

I don't think its hashcode() method is overriden to return the RGB value in hex.

The cause of your error is that black's RGB value is 0. Applying .toHexString() will only give you "0", a single character, hence the .substring() will not work. Other RGB values may also result in a string shorter than 6.

I assume you want the result to be always 6-digit; then you should pad the string with 0 from the start if it's shorter.

Edit ColorPicker.getValue() returns a Color object, not the RGB value. So you should use colorpick.getValue().getRGB() instead.

https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getRGB()

Edit2
With and without .getRGB():

Color c = Color.CYAN;
String s = Integer.toHexString(c.getRGB() & 0xFFFFFF).toUpperCase();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6 - s.length(); i++) {
    sb.append("0");
}
sb.append(s);
System.out.println(sb.toString());

Color c = Color.CYAN;
int rgbValue = (c.getRed() << 16) + (c.getGreen() << 8) + c.getBlue();
String s = Integer.toHexString(rgbValue & 0xFFFFFF).toUpperCase();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6 - s.length(); i++) {
    sb.append("0");
}
sb.append(s);
System.out.println(sb.toString());

3 Comments

As mentioned above, i get an error when i try to only use .getValue()
Is it normal that i cant find getRGB? all i can see is getRed(), get Blue() and getGreen()
According to Java API doc, .getRGB() is there since JDK1.0 (docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getRGB()).
0

use try catch function to handle it. if error hexa

1 Comment

This is not an answer. You should write comment under question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.