Format of TYPE_INT_RGB and TYPE_INT_ARGB

前端 未结 3 1202
情歌与酒
情歌与酒 2020-12-13 20:47

Could anyone explain for me how java stores color in TYPE_INT_RGB and TYPE_INT_ARGB ?
Do these lines of code work properly for calculating red, green and blue ?

相关标签:
3条回答
  • 2020-12-13 21:04

    The TYPE_INT_ARGB represents Color as an int (4 bytes) with alpha channel in bits 24-31, red channels in 16-23, green in 8-15 and blue in 0-7.

    The TYPE_INT_RGB represents Color as an int (4 bytes) int the same way of TYPE_INT_ARGB, but the alpha channel is ignored (or the bits 24-31 are 0).

    Look the javadoc of java.awt.Color and java.awt.image.BufferedImage.

    0 讨论(0)
  • 2020-12-13 21:14

    These are constant values that indicate the color model of an instance of BufferedImage. These values do not themselves store the color.

    0 讨论(0)
  • 2020-12-13 21:21

    You are correct for TYPE_INT_RGB. The equivalent TYPE_INT_ARGB code would be:

    int rgb = rgbColor.getRGB(); //always returns TYPE_INT_ARGB
    int alpha = (rgb >> 24) & 0xFF;
    int red =   (rgb >> 16) & 0xFF;
    int green = (rgb >>  8) & 0xFF;
    int blue =  (rgb      ) & 0xFF;
    

    Spelling out the color elements for the bytes from most significant to least significant, you get ARGB, hence the name.

    0 讨论(0)
提交回复
热议问题