Java - Red, Green, Blue to getRGB

ぐ巨炮叔叔 提交于 2019-12-05 18:25:27

Using the Color class:

new Color(r, g, b).getRGB()

BufferedImage ends up delegating to java.awt.image.ColorModel which uses the following code:

public int getRGB(Object inData) {
    return (getAlpha(inData) << 24)
        | (getRed(inData) << 16)
        | (getGreen(inData) << 8)
        | (getBlue(inData) << 0);
}

Modifying this to suit your needs is a trivial exercise.

Xar E Ahmer

JB Nizet's answer is great, but it can be really slow when creating new objects of type 'Color' thousands of times. The simplest way would be:

int rgb = (red << 16 | green << 8 | blue)

As answered by ByteBit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!