Programmermatt
Programmermatt

Reputation: 395

how to get separate color components java?

Say I have int color... how do i find each red, green, and blue component?

so far I have this piece of code:-

int red = rgb & 0xFF0000;

int green = rgb & 0x00FF00;

int blue = rgb & 0x0000FF;

I'm stuck with this. Thanks.

Upvotes: 0

Views: 2796

Answers (3)

Mukul Goel
Mukul Goel

Reputation: 8467

create objects of class Color to store your color and the class provides methods like getBlue() , getRed() , getGreen() to retrieve respective component

the class provides a constructor that takes color as int , use that and then the above methods.

refer here for detai

color api

Upvotes: 4

Abhishek kumar
Abhishek kumar

Reputation: 2676

Let color(int rgb) The first 8 bits of the integer argument are ignored while the last 24 bits define the color -- 8 bits for red, 8 for green, and the last 8 bits for blue. Ex:color bit pattern be 00000000000000001111111100000000. represents pure green. http://cs.roanoke.edu/~cpsc/Fall2011/CPSC120A/lab4/lab4in.html

Upvotes: 0

trumank
trumank

Reputation: 2794

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

int green = (rgb >> 8) & 0xFF;

int blue = rgb & 0xFF;

Upvotes: 2

Related Questions