How to Convert HEX to RGB in Java

Written by mike wallace
  • Share
  • Tweet
  • Share
  • Email

The Java programming language has a rich library that specialises in graphical user interfaces. You can use this library to create colourful applications. Java encodes colours as a set of three numbers that represent the intensity of the colours red, green and blue. Each colour has 255 different possible values. Colour 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 colour codes to RGB colour codes.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • Java Software Development Kit with NetBeans Integrated Development Environment (IDE) Bundle (see Resource for link)

Show MoreHide

Instructions

  1. 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. 2

    Import the colour class by adding this line to the top of your source code file:

    import java.awt.Color;

  3. 3

    Declare a new Java Color object and assign it some arbitrary hexadecimal colour codes. This declaration must go within the curly brackets of the main function. A declaration of a Color object may look something like this:

    Colour c = new Color(0xFF, 0x34, 0xAA);

  4. 4

    Get the RGB colour 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. 5

    Print out the variable RGB using this next statement:

            System.out.println("RGB: " + rgb);
    
  6. 6

    Execute the program by pressing "F6." The output looks like this:

    RGB: -52054

Don't Miss

References

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.