Java 用于对 RGB 值进行编码的位移位和按位运算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19277010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:38:44  来源:igfitidea点击:

Bit Shift and Bitwise operations to encode RGB values

javabit-manipulation

提问by Behnil

I would like to encode the RGB color to a single integer value.

我想将 RGB 颜色编码为单个整数值。

Let's say the algorithm for encoding is like this:

假设编码算法是这样的:

int code = (blue * 256 * 256) + (green * 256) + red

How can encode/decode RGB components to/from the code using bit shift and/or bitwise operators?

如何使用位移和/或按位运算符将 RGB 分量编码/解码为代码?

采纳答案by Dev Blanked

    int blueMask = 0xFF0000, greenMask = 0xFF00, redMask = 0xFF;
    int r = 12, g = 13, b = 14;
    int bgrValue = (b << 16) + (g << 8) + r;
    System.out.println("blue:" + ((bgrValue & blueMask) >> 16));
    System.out.println("red:" + ((bgrValue & redMask)));
    System.out.println("green:" + ((bgrValue & greenMask) >> 8));

回答by arynaq

If you simply want to/from RGB conversion and don't care how I would suggest using java.awt.Color

如果您只是想进行 RGB 转换并且不关心我建议如何使用 java.awt.Color

int r = 255; //red
int g = 255; //green
int b = 255; //blue
int a = 255; //alpha
Color c = new Color(r,g,b,a);

The using the getRGBmethod and getRed, getBlue, getGreen methods

使用getRGB方法和 getRed、getBlue、getGreen 方法

int RGB = c.getRGB();
int red = c.getRed();
int blue = c.getBlue();
int green = c.getGreen();

Alternatively you can construct a color object using the Color(r,g,b)constructor, it will have default 255 alpha.

或者,您可以使用构造函数构造一个颜色对象Color(r,g,b),它将具有默认的 255 alpha。

With bit operations (ARGB, 32 bit colorspace). Constructing the RGB color:

使用位操作(ARGB,32 位色彩空间)。构建RGB颜色:

int alpha = 255;    
int red = 128;
int green = 128;
int blue = 128;
int RGB = (alpha << 24);
RGB = RGB | (red << 16);
RGB = RGB | (green << 8);
RGB = RGB | (blue);

System.out.println(Integer.toBinaryString(RGB));

Out 11111111100000001000000010000000

出去 11111111100000001000000010000000

Decoding is done as in the link in the comment.

解码是在评论中的链接中完成的。

回答by Fiki

Here is a mock program that I've done up that might assist you. I approached the conversion much like Dev Blanked based off an old program I did, but he answered while I was putting the program together. Since I did the work anyways, figured I'd share in case it helped in any way.

这是我完成的一个模拟程序,可能会对您有所帮助。我根据我做的一个旧程序来处理转换,就像 Dev Blanked 一样,但他在我把程序放在一起时回答了。由于无论如何我都做了这项工作,我想我会分享以防它以任何方式提供帮助。

import java.util.Scanner;
import java.math.*;

public class RGB{

public static void main(String[]args){
    Scanner scan = new Scanner(System.in);
    int code; //Code for the color
    int red, green, blue; //Individual colors
    int rMask = 0xFF0000, gMask = 0xFF00, bMask = 0xFF; //Masks for the colors

    //Take input
    System.out.println("Please enter the red color. Range [0, 255] only please.");
    red = scan.nextInt();
    System.out.println("Please enter the green color. Range [0, 255] only please.");
    green = scan.nextInt();
    System.out.println("Please enter the blue color. Range [0, 255] only please.");
    blue = scan.nextInt();

    //Generate code based on Behnil's way.
    code = 0;
    code += (int) (red * Math.pow(2, 16));
    code += (int) (green * Math.pow(2, 8));
    code += (int) (blue * Math.pow(2,0));
    System.out.println("The code is " + code + ".");

    //Clear values
    red = 0;
    green = 0;
    blue = 0;

    //Obtain values.
    red = (code & rMask) >> 16;
    green = (code & gMask) >> 8;
    blue = (code & bMask);

    System.out.println("Your red value is: " + red);
    System.out.println("Your green value is: " + green);
    System.out.println("Your blue value is: " + blue);      
}

}

回答by iLikeCookiesQ

public static void main(String[] args){
    int red = 111;
    int green = 222;
    int blue = 121;

    int code = red*256*256 + green*256 + blue;

    blue = code%256;
    green = (code%(256*256) - blue)/256;
    red = (code - blue - green*256)/(256*256); 

    System.out.println("" + red + green + blue);
}

this outputs 111222121 as intended. This is the way i fixed it but i am not sure if the pro's agree with this as it might be slower than using bitshifts

这将按预期输出 111222121。这是我修复它的方式,但我不确定专业人士是否同意这一点,因为它可能比使用移位慢