How to Convert HEX to RGB in Java
The Java programming language has a rich library that specializes in graphical user interfaces. You can use this library to create colorful applications. Java encodes colors as a set of three numbers that represent the intensity of the colors red, green and blue. Each color has 255 different possible values. Color codes are occasionally listed in hexadecimal instead of decimal. You can declare Java Color objects using either a decimal or hexadecimal, and then get the RGB values. This allows you to easily convert hexadecimal color codes to RGB color codes.
Things You'll Need
- Java Software Development Kit with NetBeans Integrated Development Environment (IDE) Bundle (see Resource for link)
Instructions
-
-
1
Load the NetBeans IDE by clicking on its program icon. When the program loads, navigate to "New/New Project" and select "Java Application" from the list on the right side of the screen. A new source code file appears in the NetBeans text editor. The source code file contains an empty main function.
-
2
Import the color class by adding this line to the top of your source code file:
import java.awt.Color;
-
-
3
Declare a new Java Color object and assign it some arbitrary hexadecimal color codes. This declaration must go within the curly brackets of the main function. A declaration of a Color object may look something like this:
Color c = new Color(0xFF, 0x34, 0xAA);
-
4
Get the RGB color code for the Java Color object "c" and store it in an integer variable named RGB. You can do this by writing the following statement:
int rgb = c.getRGB();
-
5
Print out the variable RGB using this next statement:
System.out.println("RGB: " + rgb);
-
6
Execute the program by pressing "F6." The output looks like this:
RGB: -52054
-
1