eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

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

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

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 – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

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.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

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

>> CHECK OUT THE COURSE

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.

eBook Jackson – NPI EA – 3 (cat = Jackson)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments