Course – LS – All

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

>> CHECK OUT THE COURSE

1. Introduction

The RGB color model is widely used in various applications and devices because it aligns well with how electronic displays work. ‘R’ in RGB stands for Red, ‘G’ for Green, and ‘B’ for Blue. Combining these three primary colors at varying intensities can produce a broad spectrum of colors.

In programming languages, including Java, a common practice is to represent an RGB color as a single integer, packing the three color components and, sometimes, an alpha (transparency) component into a 32-bit integer.

In this tutorial, we’ll look into methods of moving between these representations.

2. RGB Integer Representation

In a 32-bit integer representation of an RGB color, each color component is typically allocated 8 bits. The highest 8 bits are often used for the alpha channel (representing transparency), followed by red, green, and blue. The structure looks like this:

  • Bits 24-31: Alpha (A)
  • Bits 16-23: Red (R)
  • Bits 8-15: Green (G)
  • Bits 0-7: Blue (B)

3. RGB to Integer Conversion

We can create an integer representing an (A)RGB color by bitwise shifting individual components to their desired positions:

int alpha = 255; // Fully opaque
int red = 100;   
int green = 150; 
int blue = 200;  

int argb = (alpha << 24) | (red << 16) | (green << 8) | blue;

If we’re working in a context where transparency isn’t needed, we might want to omit the alpha channel:

int rgb = (red << 16) | (green << 8) | blue;

3.1. Converting with Clamping

Using that knowledge, we can write a function that takes RGB values and returns an integer representation. First, we can implement clamping to limit the input values to the appropriate range. Quitely keeping values in desired ranges can be very convenient while working with color transformations:

int clamp(int value, int min, int max) {
    return Math.max(min, Math.min(max, value));
}

Next, let’s implement a function that will use the clamp function to convert RGB values to integers:

int rgbToInt(int alpha, int red, int green, int blue) {
    alpha = clamp(alpha, 0, 255);
    red = clamp(red, 0, 255);
    green = clamp(green, 0, 255);
    blue = clamp(blue, 0, 255);
    return (alpha << 24) | (red << 16) | (green << 8) | blue;
}

We can also implement a version without an alpha channel:

int rgbToInt(int red, int green, int blue) {
    red = clamp(red, 0, 255);
    green = clamp(green, 0, 255);
    blue = clamp(blue, 0, 255);
    return (red << 16) | (green << 8) | blue;
}

Finally, we can use the created function to convert RGB values:

assertEquals(0x00ABCDEF, rgbToInt(171, 205, 239))

4. RGB to Integer Conversion

Extracting RGB components from an integer representation is as easy as shifting bits to bring the desired part to the lowest 8 bits and then masking off redundant higher bits. For example, to extract the red channel, we need to shift the integer value 16 bits to the right:

int red = (argb >> 16)

Now, the lowest 8 bits represent our red value, but we still need to mask the rest. We can do that by using bitwise and operator:

int red = (argb >> 16) & 0xFF;

We can think about 0xFF number (255 in decimal) as a bit mask that has the first 8 bits equal 1 and all of the rest bits equal 0. So, in conjunction with the and operator, it’ll discard all but the first 8 bits. We can extract the rest of the RGB components in a similar fashion:

int alpha = (argb >> 24) & 0xFF;
int red = (argb >> 16) & 0xFF;
int green = (argb >> 8) & 0xFF;
int blue = argb & 0xFF;

5. Color Transformations

We can use the RGB color representation in various transformations to make the RGB color representation more practical.

5.1. Brightness Adjustment

Adjusting the brightness involves scaling the RGB components. Because the alpha channel has nothing to do with the brightness, we won’t scale it:

float scale = 0.8f; // darken by 20%

red = (int)(red * scale);
green = (int)(green * scale);
blue = (int)(blue * scale);

int newArgb = (alpha << 24) | (red << 16) | (green << 8) | blue;

5.2. Grayscale Conversion

To perform grayscale conversion, we’ll set all three color components to the same value. We can use the weighted average of the original colors:

int average = (int)(red * 0.299 + green * 0.587 + blue * 0.114);

int grayscaleArgb = (alpha << 24) | (average << 16) | (average << 8) | average;

Similarly to the previous example, the alpha channel is not affected because it doesn’t contain color information. We achieve different results by using different weights for each of the color components.

5.3. Color Inversion

We can also invert a color by flipping its RGB components. To do that, we need to subtract the value of each color component from 255:

red = 255 - red;
green = 255 - green;
blue = 255 - blue;

int invertedArgb = (alpha << 24) | (red << 16) | (green << 8) | blue;

6. Summary

In this article, we learned how to use bitwise operations to move between RGB and integer representations. We also look at examples of how RGB representation can be used for various color transformations.

As always, all the code samples shown in this tutorial are available over on GitHub.
Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.