Search code examples
javaimagecolorsrgbpixel

Reading RGB value from a pixel in Java


I´m trying to read the RGB values from a pixel that is drawn but I only get one value. When I try to get values for alpha, red, green and blue it always says "0" for every one except for blue where I actually get a value, and this value is always the same as "rgb". My code is shown here below and I would appreciate it if anyone of you can think of a solution.

int rgb = image.getRGB(250, 10);
rgb = rgb/-65793;
System.out.println(rgb);
Color färg = new Color(rgb, true);
int r = färg.getRed();
int g = färg.getGreen();
int b = färg.getBlue();
int a = färg.getAlpha();
System.out.println(r);
System.out.println(g);
System.out.println(b);
System.out.println(a);

What is printed is the following:

38
0
0
38
0

Solution

  • BufferedImage bi = ...;
    int x = ..;
    int y = ..;
    Color converted = new Color(bi.getRGB(x, y));
    

    You don't have to divide RGB value.