Java 如何处理 alpha 通道?

How does Java handle alpha channel?

我的理解是,带有 alpha 值示例的 RGB 值可能是这个 0xffhexcode

但是,我无法理解的是 0xff0000ff(具有最大 alpha 值的纯蓝色)如何成为大于 Integer.MAX_VALUE 的整数值。 Java 中的下划线编码如何允许这种情况发生?

intInteger是有符号的,你使用的十六进制表示法是无符号的。

EG 尝试这样的事情:

System.out.printf( "%x%n",  -1 );

将输出:

ffffffff

实际上像素的范围是整数最大值的两倍,因为整数是有符号的,而像素是无符号整数。所以有些颜色有负整数值。例如。白色 = -1

这是一个有趣的问题,归结为整数文字的定义。如需完整参考,请参阅 https://docs.oracle.com/javase/specs/jls/se9/html/jls-3.html#jls-3.10.1

处的语言规范

问题的症结在于,对于用小数表示的整型字面量,只能表示正整数。使用十六进制文字,您可以表达正值和负值。具体来说,如果第一位打开,该值将为负数。

引用语言规范:

A decimal numeral is either the single ASCII digit 0, representing the integer zero, or consists of an ASCII digit from 1 to 9 optionally followed by one or more ASCII digits from 0 to 9 interspersed with underscores, representing a positive integer.

A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits interspersed with underscores, and can represent a positive, zero, or negative integer.

所以准确地说 0xffffffff 实际上并不大于 Integer.MAX_VALUE 因为它是负数(由于前导位打开)。太添加了乍一看不太对劲的地方可以试试:

    System.out.println(Integer.MAX_VALUE == 0x7fffffff);
    System.out.println(Integer.MIN_VALUE == 0x80000000);

这两条线都将输出 true。