Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RBG char array to RGB int value in Java

Tags:

java

I have a char[] rbg containing :

  • rgb[0] : red value
  • rgb[1] : green value
  • rgb[2] : blue value

I want to use the method BufferedImage::setRGB(x, y, int rgb)

How can I proceed to convert the char array to the int value ?

Note : I use Java

Edit : I'm looking for the fastest solution

Thanks

like image 587
Matthieu Napoli Avatar asked Jan 22 '23 09:01

Matthieu Napoli


2 Answers

int value = ((255 & 0xFF) << 24) | //alpha
            (((int)rgb[0] & 0xFF) << 16) | //red
            (((int)rgb[1] & 0xFF) << 8)  | //green
            (((int)rgb[2] & 0xFF) << 0); //blue
like image 168
dogbane Avatar answered Jan 31 '23 00:01

dogbane


import java.awt.Color;

Color color = new Color(rgb[0],rgb[1],rgb[2]);
int rgb = color.getRGB();

ref : http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Color.html#getRGB%28%29

like image 20
RealHowTo Avatar answered Jan 31 '23 01:01

RealHowTo