Java - Red, Green, Blue to getRGB

筅森魡賤 提交于 2019-12-07 12:16:25

问题


By calling getRGB(int x, int y) with a BufferedImage object, one gets a single, negative number.

How can I convert three different values (a red, a green, and a blue) into this single, negative number?


回答1:


Using the Color class:

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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/13664011/java-red-green-blue-to-getrgb

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