Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert int color to int components

Tags:

java

colors

I obtain pixel color by

int color = image.getRGB(x,y);

then i want to acquire red, green, blue components separately. How to do that? Maybe using some bitmask?

int green = color&0x00ff00;

apparently not working... :(

like image 926
Kamil Avatar asked Dec 05 '10 19:12

Kamil


4 Answers

To get color components you can use:

import android.graphics.Color;

        int r = Color.red(intColor);
        int g = Color.green(intColor);
        int b = Color.blue(intColor);
        int a = Color.alpha(intColor);
like image 131
Alex Kucherenko Avatar answered Nov 09 '22 20:11

Alex Kucherenko


int value = image.getRGB(x,y);
R = (byte)(value & 0x000000FF);
G = (byte)((value & 0x0000FF00) >> 8);
B = (byte)((value & 0x00FF0000) >> 16);
A = (byte)((value & 0xFF000000) >> 24);

May need to flip the R, A, or B around.

like image 28
zezba9000 Avatar answered Nov 09 '22 20:11

zezba9000


You forgot to shift the byte to the right:

int green = (color & 0x00ff00) >> 8;
like image 3
codymanix Avatar answered Nov 09 '22 20:11

codymanix


You can use Color constructor and pass the given integer and hasalpha=true:

Color color = new Color(image.getRGB(x,y), true);

getRGB returns the color of type TYPE_INT_ARGB which means it has an alpha channel.

like image 2
khachik Avatar answered Nov 09 '22 19:11

khachik