转换RGBA值到在Javascript中有一个整数 [英] Converting rgba values into one integer in Javascript

查看:246
本文介绍了转换RGBA值到在Javascript中有一个整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经可以转换32位整数到他们的RGBA值是这样的:

I can already convert 32bit integers into their rgba values like this:

pixelData[i] = {
        red: pixelValue >> 24 & 0xFF,
        green: pixelValue >> 16 & 0xFF,
        blue: pixelValue >> 8 & 0xFF,
        alpha: pixelValue & 0xFF
    };

但我真的不知道该如何扭转这种局面。

But I don't really know how to reverse it.

推荐答案

要扭转这种局面,你只需要字节组合成一个整数。
只需使用左移位,并添加它们,它会工作。

To reverse it, you just have to combine the bytes into an integer. Simply use left-shift and add them, and it will work.

var rgb = (red << 24) + (green << 16) + (blue << 8) + (alpha);

另外,使其更安全,你可以先和他们每个人0xFF的:

Alternatively, to make it safer, you could first AND each of them with 0xFF:

var r = red & 0xFF;
var g = green & 0xFF;
var b = blue & 0xFF;
var a = alpha & 0xFF;

var rgb = (r << 24) + (g << 16) + (b << 8) + (a);

(你可以使用按位OR | 而不是 + 在这里,结局将是相同的)。

(You may use bitwise OR | instead of + here, the outcome will be the same).

这篇关于转换RGBA值到在Javascript中有一个整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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