【问题标题】:Bit Shift and Bitwise operations to encode RGB values用于编码 RGB 值的位移和位操作
【发布时间】:2015-03-19 02:55:44
【问题描述】:

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

假设编码的算法是这样的:

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

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

【问题讨论】:

  • stackoverflow.com/questions/2615522/… 中的第二个答案只是反转操作
  • 是的,但是我的代码是24位的,为什么我需要将每个组件乘以0x000000FF?
  • 由于 alpha,整数是 32 位的,所以你可以将 alphaportion 设置为零。

标签: java bit-manipulation


【解决方案1】:
    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));

【讨论】:

  • 以及如何使用位移将单个 RGB 分量编码为整数值?
  • @Behnil 使用 (r
  • 实际上掩码是 int blueMask = 0xFF0000, greenMask = 0xFF00, redMask = 0xFF 并且移位 16 是在蓝色分量而不是红色分量上
  • @Behnil 是的,你说得对,我用它作为 RGB,但我看到你的原始问题将它作为 BGR,但最好将它作为大多数人所指的 RGB
  • 是的,如果我可以使用 RGB,我会使用 java.awt.Color。但我必须使用 BGR,因为这就是我连接的 RDBMS 中数据的存储方式。
【解决方案2】:

如果您只是想/从 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);

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

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

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

使用位操作(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));

11111111100000001000000010000000

按照评论中的链接完成解码。

【讨论】:

    【解决方案3】:

    这是我完成的一个模拟程序,可能会对您有所帮助。我很像 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);      
    }
    
    }
    

    【讨论】:

      【解决方案4】:
      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);
      }
      

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

      【讨论】:

        猜你喜欢
        • 2022-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        • 1970-01-01
        • 2013-01-27
        • 2021-03-09
        相关资源
        最近更新 更多