Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

Hexadecimal (hex) and RGB (red-green-blue) are common color codes in graphics and design. Sometimes, it may be essential to convert hex to its equivalent RGB because it’s widely used in many digital applications.

In this tutorial, we’ll learn how to convert a hex color code to an equivalent RGB value in Java.

2. The Hex Color Code and RGB

The hexadecimal color code is made up of six string characters. Each character represents a value within 0 – 15 in hexadecimal notation (0-9 and A-F).

For example, the hex color code for Deep Saffron is FF9933.

RGB is a combination of red, green, and blue. It uses 8 bits each and has integer values between 0 and 255.

The RGB value for Deep Saffron is “255, 153, 51“.

To convert a hex color code to its RGB equivalent, it’s important to know that each pair of hex color codes denotes one of the red, green, and blue color parts. The first two characters represent the red part, the second two represent the green part, and the last two represent the blue part.

Furthermore, the hex color code is in base 16, while the RGB color values are in base 10. A hex color code must be converted to decimal to have a final representation of the RGB color.

3. Converting Hex to RGB

To begin with, let’s convert a hex color code to its RGB equivalent:

@Test
void givenHexCode_whenConvertedToRgb_thenCorrectRgbValuesAreReturned() {
    String hexCode = "FF9933";
    int red = 255;
    int green = 153;
    int blue = 51;
    
    int resultRed = Integer.valueOf(hexCode.substring(0, 2), 16);
    int resultGreen = Integer.valueOf(hexCode.substring(2, 4), 16);
    int resultBlue = Integer.valueOf(hexCode.substring(4, 6), 16);
        
    assertEquals(red, resultRed);
    assertEquals(green, resultGreen);
    assertEquals(blue, resultBlue);
}

In this example, we declare a variable hexCode of String type. The variable holds a hexadecimal color code. We split the hex code into its red, green, and blue components.

Also, we extract the components by taking substrings of the hex color code. To convert the hexadecimal values of red, green, and blue to their decimal value, we invoke the Integer.valueOf() method. Integer.valueOf() allows us to parse a String representation of a number in a specified base. The specified base in the example is 16.

Finally, we confirmed that the extracted RGB equivalent was our expected result.

4. Conclusion

In this article, we learned a simple way to convert a hex color code to its RGB equivalent. We used the Integer.valueOf() to convert a hexadecimal value to its equivalent decimal value.

As always, the complete source code for the example is available over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!