4

Here is my code for GrayScale filter want to know about this

class GrayScale extends RGBImageFilter {
  @Override
    public int filterRGB(int x, int y, int rgb) {
    int a = rgb & 0xff000000;
    int r = (rgb >> 16) & 0xff;
    int g = (rgb >> 8) & 0xff;
    int b = rgb & 0xff;
    rgb = (r * 77 + g * 151 + b * 28) >> 8; 
    return a | (rgb << 16) | (rgb << 8) | rgb;
    }
}
user3066663
  • 63
  • 1
  • 1
  • 4

3 Answers3

14

The format of your input is 0xAARRGGBB where AA is the alpha (transparency), RR is the red, GG is the green, and BB is the blue component. This is hexadecimal, so the values range from 00 to FF (255).

Your question is about the extraction of the alpha value. This line:

int a = rgb & 0xFF000000

If you consider a value like 0xFFFFFFFF (white), AND will return whatever bits are set in both the original colour and the mask; so you'll get a value of 0xFF, or 255 (correctly).

ashes999
  • 9,925
  • 16
  • 73
  • 124
1

The

int a = rgb & 0xff000000;
...
return a | ...;

simply preserves the top 8 bits of rgb (which contain the alpha component).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Beside Ashes999 and NPE answers, if you want to know how it's treated internally in java, check out this post

Community
  • 1
  • 1
devBinnooh
  • 611
  • 3
  • 12