【问题标题】:How to convert RGB to RGBA using bit-manipulation如何使用位操作将 RGB 转换为 RGBA
【发布时间】:2015-12-04 22:29:21
【问题描述】:

我已经尝试了一天左右的时间来解决这个问题,但我对发生的事情有点困惑。

基本上我有这个方法可以将 BGR 整数转换为 RGB 并从中创建颜色

      Color c2 = new Color((BGRColorNumber & 0xFF),
    ((BGRColorNumber >> 8) & 0xFF),
    ((BGRColorNumber >> 16) & 0xFF));

颜色代码可以在这里找到http://www.endprod.com/colors/

这很好用,但我现在尝试将其作为 RGBA 整数而不是 RGB 来执行。我有点困惑 Alpha 如何适合 RGB 整数。似乎如果你只有 RGB 或 BGR 或任何你可以制作 alpha 1.0/255 的东西,或者还有更多的东西吗?

这是我一直在使用的代码

public class test 
{

public static void main (String[] args)

{

    test t = new test();


    System.out.println(t.asIntRGBA(4823790));

}

    public int asIntRGBA(int BGRColorNumber)
{



    int rgba = 
              (((int) (BGRColorNumber         & 0xFF) & 0xFF) << 16)
            | (((int) ((BGRColorNumber >> 8)  & 0xFF) & 0xFF) << 8 )
            | (((int) (BGRColorNumber >> 16)  & 0xFF) & 0xFF)
            | (((int) ((BGRColorNumber >> 24) & 0xFF) & 0xFF) << 24);

    Color c = new Color(rgba);

  Color c2 = new Color((BGRColorNumber & 0xFF),
    ((BGRColorNumber >> 8) & 0xFF),
    ((BGRColorNumber >> 16) & 0xFF));


  Color c3 = new Color(15637065);
  System.out.println((((int) ((BGRColorNumber >> 24) & 0xFF) & 0xFF) << 24) + " : " + c.getAlpha());
    System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
    System.out.println(String.format("#%02x%02x%02x%02x", c2.getRed(), c2.getGreen(), c2.getBlue(), c2.getAlpha()));
      System.out.println(String.format("#%02x%02x%02x%02x", c3.getRed(), c3.getGreen(), c3.getBlue(), c3.getAlpha()));
    return (rgba);
}

}

这是我的输出

0 : 255

“#ee9a49ff”

“#ee9a49ff”

“#ee9a49ff”

15637065

因此,如果我将其转换为颜色,然后输出 RGBA,它就可以工作,但我想为什么要浪费一个对象创建然后获取值,而我可以自己执行 colorInteger 和按位移位操作?

在 Alpha 通道上进行操作时,它给了我 00/0,但是当使用 Color.getAlpha();我得到了 ff/255....?

我也不是 100% 确定 > # 的值放入

在这种情况下,Alpha 似乎处于 24 的最高位置,所以我假设这最初是 ABGR????

当我切换到

        int rgba = 
              (((int) (BGRColorNumber         & 0xFF) & 0xFF) << 24)
            | (((int) ((BGRColorNumber >> 8)  & 0xFF) & 0xFF) << 16)
            | (((int) ((BGRColorNumber >> 16)  & 0xFF) & 0xFF) << 8)
            | (((int)  (BGRColorNumber >> 24) & 0xFF) & 0xFF);

System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));

我的输出是“#9a4900ff” 而不是“#ee9a49ff”

如果我这样做了

System.out.println(String.format("#%02x%02x%02x%02x",  ((rgba >> 24) & 0xFF),  ((rgba >> 16) & 0xFF),  ((rgba >> 8) & 0xFF), ((rgba >> 0) & 0xFF)));

我得到“#ee9a4900”,这基本上是正常的,除了 Alpha 的 00 错误,只是它现在正在窃听 trf....??????? 00 而不是 ee 并且 Alpha 现在是 ff 而不是 00?

所以我很困惑我到底做错了什么......? 任何建议表示赞赏!

谢谢!


编辑:我意识到这可能是因为只有从 0 到 16 的 RGB 位置,而 24 本身并不存在,这就是为什么我用“

也许我只需将 255/ff 放入 24 的 Alpha 位置,然后收工。如果值是 00,那么我只需要让它不是那个:)。

我还是不明白为什么 Color.getAlpha() 返回的是 ff,而不是 00...?


编辑 2:

所以我最终解决了这个问题。

基本上我在 24 位没有任何东西,只有 0-23 被用完。这意味着最后 8 个 24-31 的值是 0。

我也意识到颜色给我 Alpha 的 ff/255 的原因,是因为我没有告诉颜色我有一个 Alpha 值,因此它给了它默认的 ff/255。

By doing Color c = new Color (#, true);

我能够打开 Alpha 值。

这是我解决问题的方法。

public class test 

{

public static void main (String[] args)
{

    test t = new test();


    System.out.println(t.asIntRGBA(4823790));

}

    public int asIntRGBA(int BGRColorNumber)
{



    int rgba = 
              (((int) (BGRColorNumber         & 0xFF) & 0xFF) << 24)
            | (((int) ((BGRColorNumber >> 8)  & 0xFF) & 0xFF) << 16 )
            | (((int) ((BGRColorNumber >> 16)  & 0xFF) & 0xFF) << 8)
            | ((int) (252 & 0xFF) & 0xFF);

    Color c = new Color(rgba, true);
   // Color c3 = Color.TRANSLUCENT;

  Color c2 = new Color((BGRColorNumber & 0xFF),
    ((BGRColorNumber >> 8) & 0xFF),
    ((BGRColorNumber >> 16) & 0xFF), 252);


  Color c3 = new Color(-51471799, true);
  System.out.println(((rgba >> 24) & 0xFF) + " | " +  c.getRed() +  " | " + c.getGreen() + " | " + c.getBlue() + " | " + c.getAlpha());
  //System.out.println(();
    System.out.println(String.format("#%02x%02x%02x%02x",((rgba >> 24) & 0xFF), ((rgba >> 16) & 0xFF), ((rgba >> 8) & 0xFF), ((rgba) & 0xFF)));
    System.out.println(String.format("#%02x%02x%02x%02x", c2.getRed(), c2.getGreen(), c2.getBlue(), c2.getAlpha()));
      System.out.println(String.format("#%02x%02x%02x%02x", c3.getRed(), c3.getGreen(), c3.getBlue(), c3.getAlpha()));
    return (rgba);
}

}

我将使用 >> 24 获取值更改为仅放置一个值(在本例中为 252)以进行测试。从技术上讲,它们都是 255。

这给了我正确的值,并且根据上面的页面对应于正确的十六进制值..

但是我遇到了问题

  System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));

正确

正确的输出:

“#ee9a49fc”

颜色的输出:

“#9a49fcee”

如果我将我从 rgba "-291878404" 得到的 int 值插入到颜色 c3 中,我得到的值与颜色 c 相同。我不确定出了什么问题,但我在让所有内容都相同时遇到了问题。

rgba

【问题讨论】:

    标签: java colors bit-manipulation rgb bit-shift


    【解决方案1】:

    所以我遇到了一些麻烦,但我最终解决了问题。

    基本上我只能使用位 0-23,因为位 24-31 没有被提供任何数据。

    所以首先我必须将 Alpha 添加到位 24-31,然后我必须将所有内容从 ARGB 移位到正确的 RGBA 顺序。

    如果我尝试将所有内容转换为 RGBA 顺序,我会遇到问题,因为 24 中没有任何内容,并且由于某种原因,移位操作不喜欢正在发生的事情,所以我只执行了两次整数操作。

    public class test 
    {
    
    
    public static void main (String[] args)
        {
    
            test t = new test();
    
    
            t.asIntRGBA(4823790);
    
        }
    
        public int asIntRGBA(int BGRColorNumber)
        {
    
            /*  
             *  First set of bit-wise shifts take the BGR value and swaps it into RGB values:
             *  Bits 24-31 - Alpha (Added in through this operation)
             *  Bits 16-23 - Red  (Originally Bits 0-7)
             *  Bits  8-15 - Green
             *  Bits   0-7 - Blue (Originally Bits 16-23)
             *  
             *  Seeing as we want Red as 24-31, and Alpha as 0-7, we need to do another bit-wise routine.
             *  We first have to add bit data to 24-31, before being able to shift it around.
             *  If you try to shift bits over in one routine it causes jumbling of bit data that isn't correct.
             *
             */
    
            /* 
    
            *  The Bit-Wise shift of (value >> #) is taking the value at Bit #.
    
            *  While the Bit-Wise sift of (x << #) takes the data of x, and puts it into Bit #.
    
            */
    
            int rgba = (((int) ((BGRColorNumber      )& 0xFF) & 0xFF) << 16)
                    | (((int) ((BGRColorNumber >> 8 )& 0xFF) & 0xFF) << 8)
                    | (((int) ((BGRColorNumber >> 16)& 0xFF) & 0xFF)    )
                    | (((int) (25 & 0xFF) << 24));
    
    
    
           /*Once an Alpha value is stored in 24-31, you can now swap everything where they should be.
    
            * Since blue and red were swapped we now use (>> 16, not (>> 0/blank)).
            * As you can see we also swapped (<< 16 for << 24),
            * since now we are taking the value in Bits 16-23
            *                 and shifting them to Bits 24-31.
    
            * After this Bit-Wise shift we now have    
    
            *  Bits 24-31 - Red   (Originally Bits 16-23)
            *  Bits 16-23 - Green (Originally Bits 8-15)
            *  Bits  8-15 - Blue  (Originally Bits 0-7)
            *  Bits   0-7 - Alpha (Originally Bits 24-31)
    
            */
            int rgba2 = (((int) (rgba >> 16) & 0xFF) << 24)
                    | (((int) (rgba >> 8 ) & 0xFF) << 16)
                    | (((int) (rgba      ) & 0xFF) << 8)
                    | (((int) (rgba >> 24) & 0xFF));
    
    
    
            Color c = new Color(rgba, true);
    
            Color c2 = new Color((BGRColorNumber & 0xFF),
                                ((BGRColorNumber >> 8) & 0xFF),
                                ((BGRColorNumber >> 16) & 0xFF), 252);
    
    
            Color c3 = new Color(435067465, true);
            System.out.println(rgba);
            System.out.println(((rgba2 >> 24) & 0xFF) + " | " +  c.getRed() +  " | " + c.getGreen() + " | " + c.getBlue() + " | " + c.getAlpha());
          //System.out.println(String.format("#%02x%02x%02x%02x%02x",((rgba >> 24) & 0xFF), c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
    
    
          //System.out.println(String.format("#%02x%02x%02x%02x",((rgba >> 24) & 0xFF), ((rgba >> 16) & 0xFF), ((rgba >> 8) & 0xFF), ((rgba) & 0xFF)));
           //System.out.println(String.format("#%02x%02x%02x%02x", c2.getRed(), c2.getGreen(), c2.getBlue(), c2.getAlpha()));
            System.out.println(String.format("#%02x%02x%02x%02x",((rgba >> 16) & 0xFF), ((rgba >> 8) & 0xFF), ((rgba >> 0) & 0xFF), ((rgba >> 24 ) & 0xFF)));
            System.out.println(String.format("#%02x%02x%02x%02x",((rgba2 >> 24) & 0xFF), ((rgba2 >> 16) & 0xFF), ((rgba2 >> 8) & 0xFF), ((rgba2 >> 0 ) & 0xFF)));
            System.out.println(String.format("#%02x%02x%02x%02x", c.getRed(), c.getGreen(), c.getBlue(), c.getAlpha()));
            System.out.println(String.format("#%02x%02x%02x%02x", c3.getRed(), c3.getGreen(), c3.getBlue(), c3.getAlpha()));
    
            return (rgba);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 1970-01-01
      • 2020-03-11
      • 2012-02-28
      • 2011-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多