如何构建从字节数组的颜色? [英] How to construct colour from byte array?

查看:103
本文介绍了如何构建从字节数组的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林用非常简单的任务努力(当然,我这么认为)。
我有字节[4] 阵列重新像字节presents颜色值[0] =阿尔法
字节[1] =红等。
我这怎么能字节数组转换为实际颜色的对象?
感谢您的回答。

Im struggling with very simple task (well, i think so). I have byte[4] array that represents colour values like byte[0] = alpha, byte [1] = red and so on. How can I convert this byte array to an actual colour object? Thanks for an answer.

推荐答案

字节在Java中签署这样的积极作用只能容纳值,直到127,RGB上升到255所以你必须以补偿:

Bytes in Java are signed so the positive part can only hold values until 127, RGB goes up to 255. So you have to compensate for that:

byte b = (byte) 130;
int i = b & 0xFF;
System.out.println(i); //Prints 130 again

的int然后可以传递给Color构造。

The int can then be passed to the Color constructor.

编辑:完整的例子:

byte[] values = new byte[] {(byte) 130, (byte) 150, (byte) 200, (byte) 200};
Color color = Color.argb(values[0] & 0xFF, values[1] & 0xFF, values[2] & 0xFF, values[3] & 0xFF);
System.out.println(color + " alpha " + color.getAlpha());

这篇关于如何构建从字节数组的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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