Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css style hex strings and Color.decode

Tags:

this article suggests you can use Color c = Color.decode("FF0096"); however this understandably throws an exception

Caused by: java.lang.NumberFormatException: For input string: "FF0096"     at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)     at java.lang.Integer.parseInt(Integer.java:449)     at java.lang.Integer.valueOf(Integer.java:528)     at java.lang.Integer.decode(Integer.java:958)     at java.awt.Color.decode(Color.java:707) 

What is the best way of converting a String in the format of "#FF0096" or "FF0096" into a java awt Color?

like image 636
pstanton Avatar asked Feb 26 '10 07:02

pstanton


People also ask

How do you write the colors when using the hex in CSS?

A hexadecimal color is specified with: #RRGGBB, where the RR (red), GG (green) and BB (blue) hexadecimal integers specify the components of the color.

Can you use hex codes in CSS?

The most common way to specify colors in CSS is to use their hexadecimal (or hex) values. Hex values are actually just a different way to represent RGB values. Instead of using three numbers between 0 and 255, you use six hexadecimal numbers. Hex numbers can be 0-9 and A-F.

Can I use hex with Alpha?

Adding an Alpha Value to CSS Hex CodesUsing an alpha value to update a color's transparency will change the hex code format from #RRGGBB to #RRGGBBAA (where alpha is A ). The first six values (the red, green, and blue ones) remain the same.

How do I find the hex code of a color?

Hex color codes start with a pound sign or hashtag (#) and are followed by six letters and/or numbers. The first two letters/numbers refer to red, the next two refer to green, and the last two refer to blue. The color values are defined in values between 00 and FF (instead of from 0 to 255 in RGB).


2 Answers

Color c = Color.decode("0xFF0096"); 

or

Color c = Color.decode("#FF0096"); 

or

Color c = new Color(0xFF0096); 
like image 65
Matthew Flaschen Avatar answered Oct 10 '22 13:10

Matthew Flaschen


The Color.decode method throws the NumberFormatException if the specified string cannot be interpreted as a decimal, octal, or hexidecimal integer

The string "FF0096" without the prefix of 0 or 0x will be interpreted as a base 10 representation which does not work.

like image 39
codaddict Avatar answered Oct 10 '22 13:10

codaddict