简体   繁体   English

Color.FromArgb如何将Int32作为参数?

[英]How can Color.FromArgb take Int32 as parameter?

The Color.FromArgb method takes Int32 as a parameter. Color.FromArgb方法Int32作为参数。 The value of Color.White is #FFFFFFFF as ARGB, which is 4.294.967.295 as decimal (way over int.MaxValue ). Color.White的值为ARFF#FFFFFFFF ,其为十进制的4.294.967.295 (超过int.MaxValue )。 What am I not understanding here? 我在这里不理解什么? How can the method take int as a parameter if valid ARGB values are above the maximum value of an int ? 如果有效ARGB值高于int的最大值,该方法如何将int作为参数?

不幸的是,由于Color.FromArgb采用int而不是uint ,因此需要对大于int.MaxValue的颜色使用unchecked关键字。

var white = Color.FromArgb(unchecked((int)0xFFFFFFFF));

Your confusion lies in signage. 你的困惑在于标牌。 Although Int32.MaxValue is equal to 2,147,483,647, that is signed. 虽然Int32.MaxValue等于2,147,483,647,但它已签名。

If you look at UInt32.MaxValue , that is unsigned and as you can see, the maximum value is 4,294,967,295. 如果你看一下UInt32.MaxValue ,那是无符号的,正如你所看到的,最大值是4,294,967,295。

You see, signed numbers, in binary, use the left most bit to determine if its a positive or negative number. 你看,有符号数字,二进制,使用最左边的位来确定它是正数还是负数。 Unsigned numbers, in binary, don't have a signed bit and make use of that last bit, giving you essentially double the storage capacity. 二进制的无符号数字没有有符号位并使用最后一位,使您的存储容量基本上翻倍。

i think part of the reason that the Color class uses Int32 instead of unsigned is because unsigned int's aren't CLS compliant, as stated in this SO Question 我认为Color类使用Int32而不是unsigned的部分原因是因为unsigned int不符合CLS,如本SO问题所述

The practical problem is that you want to enter a eight-digit hexadecimal number, but because the single-parameter version uses an int , rather than a uint , it is difficult to represent colours with an Alpha value above &7F. 实际问题是您要输入一个八位十六进制数字,但由于单参数版本使用的是int而不是uint ,因此很难用Alpha值高于&7F来表示颜色。 This is because an int uses one bit to represent the sign. 这是因为int使用一位来表示符号。

The easiest solution is to use the four-parameter version: 最简单的解决方案是使用四参数版本:

var whiteColor = Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF);

The byte-ordering of the 32-bit ARGB value is AARRGGBB. 32位ARGB值的字节顺序是AARRGGBB。 The most significant byte (MSB), represented by AA, is the alpha component value. 由AA表示的最高有效字节(MSB)是alpha分量值。 The second, third, and fourth bytes, represented by RR, GG, and BB, respectively, are the color components red, green, and blue, respectively. 分别由RR,GG和BB表示的第二,第三和第四字节分别是红色,绿色和蓝色。

http://msdn.microsoft.com/en-us/library/2zys7833(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/2zys7833(v=vs.110).aspx

It appears that the method breaks the int32 into 32 bits and converts that to AARRGGBB which is two nibbles (1 byte) for each parameter A, R, G, and B. 看起来该方法将int32分成32位并将其转换为AARRGGBB,对于每个参数A,R,G和B,它是两个半字节(1个字节)。

This works because each digit in FFFFFFFF in hexadecimal converts to a single nibble. 这是有效的,因为FFFFFFFF中的每个数字都以十六进制转换为单个半字节。 Each space equals 4 bits specifically. 每个空格特别等于4位。 So, this bit representation converts directly to 32 bits, which can be represented as a single int32. 因此,该位表示直接转换为32位,可以表示为单个int32。

To give just a little more detail: 再举几个细节:

The maximum value of a space in hexadecimal is F (or 15 in decimal). 十六进制空格的最大值是F(或十进制的15)。

The maximum value of 4 bits ( 1 nibble) is (8, 4, 2, 1) which is also 15. 4位(1个半字节)的最大值是(8,4,2,1),也是15。

So, FFFFFFFF = 1111 1111 1111 1111 1111 1111 1111 1111 which is then represented as an int32 . 因此,FFFFFFFF = 1111 1111 1111 1111 1111 1111 1111 1111,然后表示为int32。

AS @icemanind pointed out, the first bit is reserved for the sign (+ or -), and therefore limits the numeric value of the int32 to 2,147,483,647. AS @icemanind指出,第一位是为符号(+或 - )保留的,因此将int32的数值限制为2,147,483,647。

It's not the numeric value, but the bit values that are important for this method. 它不是数值,而是对此方法很重要的位值。

According to the MSDN page for Color.FromArgb Method (Int32) , you don't pass in the actual int value for the color. 根据Color.FromArgb方法(Int32)的MSDN页面,您不传递颜色的实际int值。 For example, to get red you would call Color.FromArgb(0x78FF0000) . 例如,要获得红色,您将调用Color.FromArgb(0x78FF0000) So, for white, you should just need to call Color.FromArgb(0xFFFFFFFF) . 所以,对于白色,你应该只需要调用Color.FromArgb(0xFFFFFFFF)

A Color is made up of four important fields, A (alpha), R (red), G (green), and B (blue). Color由四个重要字段组成, A (alpha), R (红色), G (绿色)和B (蓝色)。 Each of these is eight bits. 每个都是8位。 four eight-bit values fit into an int32. 四个八位值适合int32。 Although the MSB may be the sign bit, that is ignored. 虽然MSB可能是符号位,但是会被忽略。

0xFFFFFFFF may be a negative number when expressed as an int , but it's white as a color. 当表示为int0xFFFFFFFF可以是负数,但是它是白色的颜色。

It doesn't matter. 没关系。

#FFFFFFFF is 11111111111111111111111111111111 in binary. #FFFFFFFF是二进制的11111111111111111111111111111111。

In decimal it is 4.294.967.295 if you're using unsigned ints . 如果你使用无符号整数 ,则在十进制中它是4.294.967.295。 If you're using signed ints, it is interpreted as -1. 如果您使用的是有符号整数,则将其解释为-1。

But the actual decimal value doesn't matter; 但实际的十进制值无关紧要; what matters is the value of the individual bits. 重要的是各个位的价值。

A signed int can still store 4.294.967.295 values, just half of them are negative. signed int仍然可以存储4.294.967.295值,其中只有一半是负数。 The bits are the same. 这些位是相同的。

您可以怀疑0x00FFFFFF-0x01000000编译器将正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Color.FromArgb(Int32,Int32,Int32)超出范围 - Out of range in Color.FromArgb(Int32,Int32,Int32) 如何使用 Int Array To Color.fromargb 并创建新图像 c# - How To Use Int Array To Color.fromargb And Create A New Image c# Color.FromArgb(...); 安全信息 - Color.FromArgb(…); security message Windows Phone 8阵列设置Color.FromArgb - Windows phone 8 array to set Color.FromArgb 给定一个可以为空的int时返回Color.FromArgb的最佳实践C# - Best practices for returning a Color.FromArgb when given an int that may be null C# 不能在 DataGridView 单元格 BackColor 中使用 Color.FromArgb - Cannot use Color.FromArgb in DataGridView cell BackColor 如果Int32只是int的别名,那么Int32类如何使用int? - If Int32 is just an alias for int, how can the Int32 class use an int? C#无模式对话框:如何从无模式对话框返回3个不同的值到主窗体,并将它们放在一起用作Color.FromARGB? - C# Modeless Dialog: How to return 3 different values from modeless dialog to main form and put them together to use as Color.FromARGB? 如何区分类型:Int32 []和Int32 [*]? - How to differentiate the types: Int32[] & Int32[*]? 如何检查Double是否可以转换为Int32? - How to check if a Double can be converted into a Int32?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM