使用逻辑bitshift的RGB值 [英] Using logical bitshift for RGB values

查看:249
本文介绍了使用逻辑bitshift的RGB值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是有点幼稚,当谈到按位逻辑和我有什么可能是一个简单的问题...基本上,如果我有这个(是动作,但可以在许多语言适用):

I'm a bit naive when it comes to bitwise logic and I have what is probably a simple question... basically if I have this (is ActionScript but can apply in many languages):

var color:uint = myObject.color;
var red:uint = color >>> 16;
var green:uint = color >>> 8 & 0xFF;
var blue:uint = color & 0xFF;

我想知道究竟是什么`&放大器;为0xFF被操作的绿色和蓝色。我明白与运算做,但为什么它需要(或者是一个好主意)在这里?

I was wondering what exactly the `& 0xFF' is doing to green and blue. I understand what an AND operation does, but why is it needed (or a good idea) here?

源出于此code在这里: http://alexgblog.com/?p=680

The source for this code was here: http://alexgblog.com/?p=680

鸭preciate的提示。 亚历克斯

Appreciate the tips. Alex

推荐答案

在RGB有8位红,8位绿,8位蓝。您存储3个字节的int,其中有4个字节是这样的:

In RGB you have 8 bits for Red, 8 bits for Green and 8 bits for Blue. You are storing the 3 bytes in an int, which has 4 bytes in this way:

  • 位从0-7(至少显著)为蓝色。
  • 从8-15(至少显著)位为绿色。
  • 从16-23(至少显著)对位红。

要提取他们需要右移位数的正确数目,以便把对应于要提取的INT的最低显著字节颜色的字节,然后把INT的其余部分分开的值在0这样才能让只字节值。最后一部分是通过使用与运算与掩模0xFF的完成。与门离开那里施加具有相同值整型的唯一字节,留下其余字节0

To extract them to separate values you need to right shift the correct number of bits in order to put the byte corresponding to the color you want to extract in the least significant byte of the int, and then put the rest of the int in 0 so as to let that byte value only. The last part is done by using the AND operation with mask 0xFF. The AND leaves the only byte of the int where it is applied with the same value, leaving the rest bytes in 0.

这是发生了什么:

var color:uint = myObject.color;

您有这样的颜色变量:0x00RRGGBB

You have the color variable like this: 0x00RRGGBB

var red:uint = color >>> 16;

右移16位色结果:0x000000RR,导致红色值

Right-shifting 16 bits color results in: 0x000000RR, resulting in the red value.

但对于:

var green:uint = color >>> 8 & 0xFF;

在右移色8位离开这个结果0x0000RRGG,但在这里我们只需要GG位左边,这样我们应用和操作的面具0xFF的或者更明确0x000000FF,为您展示知道,叶老比特值,其中,掩码为1和零点那里的掩码是0,这样做的结果 0x0000RRGG和放大器; 0x000000FF = 0x000000GG 这是绿色的值。这同样适用,以提取蓝色值

After right-shifting color 8 bits it leaves this result 0x0000RRGG, but here we only need the GG bits left so we apply the AND operation with the mask 0xFF or to be more clear 0x000000FF, as you show know AND leaves the old bits values where the mask is 1 and zeros there the mask is 0, so the result of doing 0x0000RRGG & 0x000000FF = 0x000000GG which is the value for green. The same is applied to extract the blue value.

这篇关于使用逻辑bitshift的RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆