Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1 Comment
Oldest
Newest