[ActionScript 3.0] colors in Flash

Source: Internet
Author: User
Tags binary to decimal

In Flash, a color is a string of special numbers, a color value can be any number from 0 to 16,777,215, which is the 24-bit (bit) color. You might ask why there are 16,777,216 (256*256*256) color values, because Flash uses RGB color values, which means that each color can be made up of red (red), green, Blue (blue) three colors. Each of the three synthetic colors is a number from 0 to 255, so there are 256 possible depths for each of the red and yellow blue, and the result will be about 16.78 million colors.

This system is called 24-bit color because it uses 8 bits (0 or 1) to represent 256 values. 8-bit times 3 (red, yellow, blue) means that 24 bits are required to represent 16.78 million color values. We'll also learn about the 32-bit color system, which has an additional 8-bit value to represent transparency (alpha). It's hard to imagine what a color with a value of 11,273,634 is. As a result, developers typically use a different numeric representation of the system: hexadecimal. If you have used colors in HTML, then this is not a strange thing for you, but let's learn the basics anyway.

Use hexadecimal to represent color values

       16 Binary (hexadecimal, abbreviated HEX), based on 16, each digit is 0 to 15 of any number, while the decimal is based on 10, Each digit is a number from 0 to 9. Since there are no numbers that can represent 10 to 15, the first six letters of the alphabet, a through F, are borrowed to represent them. Thus, each 16 binary can be one of 0 to F (in Flash, the hexadecimal number is not case sensitive, and a to f or a to f are used). Use the 16 decimal number in the HTML, and add # as a prefix to identify it. As with other languages, in ActionScript, 0x is used as a prefix. For example, the hexadecimal 0xA is equal to the decimal 10, and 0xF equals 15,0x10 equals 16. In decimal, each digit is 10 times times the number of one on its right, such as 243 is 100 times times 2, 4 is 10 times and 3 times more. In hexadecimal, each bit is 16 times times the number of its right one, as 0X2B3 represents 256 times times for 2, 16 times times for B (or 11), and 3 times more.        for 24-bit, it is equal to 0xFFFFFF, in addition, these 6 hexadecimal numbers can be divided into three parts. The first part represents the red, the second part represents the green, the last two represent blue, and are symbolically recorded as 0xRRGGBB.        remember that each synthetic color can be a value from 0 to 255 (hexadecimal: 0x00 to 0xFF). Therefore, red can be expressed as 0xFF0000, which represents pure red, because it is 0 green and blue is 0. Similarly, 0x0000ff represents pure blue.

Take 11,273,634 For example, convert it to 16 (we'll introduce a simple method later), and the result is 0XAC05A2, which can be decomposed into red (red) = Ac,green (green) = 05,blue (blue) = A2. It can be seen that the red (red) and blue (blue) values are higher, and the green is almost no, we can guess this color is probably purple, which is not seen in the decimal number. Note that in ActionScript, you can use either of the binary representations, and use color values in a function to use both decimal and hexadecimal. For Flash, 11,273,634 and 0XAC05A2 are a number, but for poor humans the latter notation is easier to read. So how to convert between the two, it is very easy to convert 16 binary to decimal. As long as you output this hexadecimal number, the trace function automatically converts it to decimal. Trace (0XAC05A2); Convert decimal to hexadecimal to use the toString (16) function, such as: Trace ((11273634). ToString (16));

The output is AC05A2, if you want to use this number, do not forget to add 0x.

Transparency and 32-bit color

As mentioned earlier, in addition to the 24-bit color, there are 32-bit color, more than 8 bits to represent transparency. Just as the angle system is the same as the Radian system (chapter III), as 3 is somewhat mixed in the use of 24 and 32-bit colors. The drawing API for as 3 is largely based on Flash MX (Flash 6), in short, the drawing API functions use a special parameter to specify transparency, so the 24-bit color is also used. In addition, the BitmapData class is added from Flash 8 and uses 32-bit color. If you have questions about which color system a function uses, see the ActionScript reference manual.

We can use hexadecimal to represent a color value in a format such as 0xRRGGBB. Similarly, the 32-bit color is the same, in the form of 0xAARRGGBB, where AA represents transparency. Therefore, 0xFFFFFFFF represents an opaque white, 0x00ffffff represents a completely transparent white, while the 0X80FFFFFF represents approximately 50% transparent white.

New numeric types: int and UINT

In previous versions of ActionScript, there was only one numeric type number, which could represent a positive integer, a negative integer, or a floating point (or 0). We are used to this kind of freedom, but now the two more types of numbers can make our code clearer.

The first newly added numeric type is int (integer), which can be a positive integer or a negative integer or zero. If we declare a floating-point value to be of type int, the fractional part is automatically removed. For example, declare 1.9 as int, and the result is 1. Therefore, when we determine that only integers are used, the variable is declared as int, and the variable used for counting in the loop should generally be int. In the following code, the I variable never gets a floating-point value, so using the int type has meaning.

for (var i:int = 0; i <; i++) {//does something in here!}

The second new type is uint (unsigned integer), and "unsigned" means that there is no plus or minus (-+) sign and is always positive. A 32-bit color value is always stored as a uint type in as 3, because unsigned integers can retain more values than (signed) integers. Both int and UINT can store 32 digits, which is greater than 4 billion, but int has a special bit to store the symbol (+ +), so there are only 31 digits (greater than 2 billion), so you can mark positive or negative numbers. Therefore, declaring a positive 32-bit color value with the int type is too big! What if it's used? Let's try it out:

var color1:int = 0xFFFFFFFF; Trace (Color1);//-1 var color2:uint = 0xFFFFFFFF; Trace (COLOR2);//4294967295

The value of 0xFFFFFFFF is equivalent to 4,294,967,295 in decimal, because this value is too large for int, so the result is "reversed" and turned into a-1! Of course, this is not the result we expect. If you use the UINT type, there is no problem. Therefore, because the color values are always positive and may exceed the range of the int, use UINT to store them.

Color synthesis

It is a common problem how to make the color values of red, green and blue a valid color value. Suppose there are three variables red,green,blue, each of which holds a number between 0 and 255. Here's the formula: Color24 = red<<16 | green<<8 | Blue After adding transparency, create a 32-bit color value with the following formula: Color32 = Alpha << 24 | Red << 16 | Green << 8 | Blue

There are two bit operators here, which you may not have been exposed to before. Bit operations are operations on binary (0 or 1), and for 24-bit colors, if each bit of the color value is listed, a string of 24 0 or 1 strings is obtained. The hexadecimal 0xRRGGBB is decomposed into binary after this:

RRRRRRRRGGGGGGGGBBBBBBBB, we see a 8-bit red,8 bit green,8 bit blue, which means that 8-bit binary number equals 256.

In the color synthesis formula, the first bitwise operator is <<, which is a bitwise left shift operator that moves the binary values to the left. For example, a red value of 0xFF or 255 can be represented by a binary representation of: 11111111 moves it 16 bits to the left, the result is: 111111110000000000000000 in 24-bit color, it represents red, converted to 16 after the 0xFF0000, is pure red. Below, suppose you have a green value of 0x55 (decimal 85) and a binary representation of: 01010101 moves it 8 bits to the left, and the result is: 000000000101010100000000 in this way, the 8 digits are completely moved to the range of the green value.

Finally, suppose a blue value is 0xf3 (decimal 243), and the binary is represented as: 11110011. Because they are all in the blue range, there is no need to move it. So we have a total of three groups: 111111110000000000000000 000000000101010100000000 000000000000000011110011 You can simply add them up and become a 24-digit number, but There is also a better and faster method: use or (or) operation, the symbol is |. It compares each bits of the two sets of numbers, and if one of the two has a number of 1, then the result is 1, and if two numbers are 0, the result is 0. You can use the or (or) operation to add the value of the Red,green,blue, or you can say "if there is a number equal to 1 in these two or two numbers, then the result is 1". The end result is: 111111110101010111110011 converting this number to hexadecimal is equal to 0xff55f3. Of course, we can't see these bits, nor do we deal with these 0 or 1, just learn this: var color24:number = 0xFF << 16 | 0x55 << 8 | 0xf3; Decimal notation is: var color24:number = 255 << 16 | << 8 | 243; Flash doesn't care whether people are using decimal or hexadecimal numbers.

Similarly, you can convert all of the red,green,blue values to a 16-binary string, concatenate them into a long string, and then convert them to hexadecimal numbers. However, it can be cumbersome to do so, and using string manipulation can be very slow. Conversely, using binary operations is the quickest operation in ActionScript because they are low-level operations.

For 32 digits, the truth is the same, add 8-bit alpha (transparency) channel and move it to the left by 24 bits. For example, there is a set of 32 digits of 0XFFFF55F3, which moves the alpha value 24 bits to the left, the result is as follows: 11111111111111110101010111110011 the first 8 digits represent transparency, followed by the same Red,green,blue value as before.

Get color values

If there is such a number 0xff55f3, the value from which to extract the Red,green,blue. See the formula below, first 24-bit color: red = Color24 >> 16; Green = Color24 >> 8 & 0xFF; Blue = Color24 & 0xFF;

A sentence to see. First, you might guess that >> is a bitwise right-shift operator that moves bits to the right. If these bits move too far to the right, then the numbers disappear and there are no numbers. The following starts with red: 111111110101010111110011 moves the color value 16 bits to the right, the result is as follows: 11111111, or 0xFF (255) for green, move 8 bits to the right, the result is as follows: 1111111101010101

The value of blue has been reached, but the red value is left aside. Here is the place to use the (and) operator, the same as the (OR) operator, is the comparison of the two sets of values, it can be interpreted as "two number compared, if two are 1 then the result is 1, if one of them is 0, then the result is 0". We compare it with 0xFF: 1111111101010101 0000000011111111

Because all the red digits are compared to 0, they all have a result of 0, and the result is only 1 if the two numbers are 1 o'clock, so the results are as follows: 0000000001010101

For blue, you do not need to do a right-shift operation, just let it and 0xFF execute with (and) operation. For 32-bit color, the method is the same, just a little bit of change: Alpha = Color32 >> 24; Red = Color32 >> & 0xFF; Green = Color32 >> 8 & 0xFF; Blue = Color32 & 0xFF;

Here, the value of getting alpha needs to move 24 bits to the right.

For example:

var c:uint=0x23fff3; Trace ((c>>16). ToString (16));//Gets the red value of the trace ((c>>8&0xff). ToString (16));//Gets the green value FF trace (c &0XFF). toString (16))//Get Blue value F3

[ActionScript 3.0] colors in Flash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.