javascript - Solve the following code. . How to understand?
大家讲道理
大家讲道理 2017-05-19 10:15:40
0
2
458
color='#FFFFFF';
var colorVal = parseInt(color.slice(1), 16);
result = {
    red: colorVal >> 16 & 0xFF,
    green: colorVal >> 8 & 0xFF,
    blue: colorVal & 0xFF,
}
//{red:255,green:255,blue:255}

Understood>>Signed right shift, why do you need parseInt(,16)? >>Isn’t it right shift of binary data? . Then parseInt no matter what base it is converted into. The binary data should remain unchanged.
Why is it moved by 16 bits. . Isn't 'FFFFFF' 6bytes 48bit? .

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
左手右手慢动作

parseInt('hexStr', 16) This sentence means to convert a string containing hexadecimal numbers into an Int numerical type by parsing hexadecimal. Only numerical types can be shifted.

Ty80

Why parseInt(,16)

'FFFFFF' is just a string, and the right shift operation requires numbers.

Then parseInt no matter what base it is converted into. The binary data should remain unchanged.

Take 'FFFFFF' as an example. There is no F below hexadecimal, so it is NaN. The carry is different for values ​​above hexadecimal, so they are also different values.

Why mobile 16-bit

Each RGB color value is 0~255, which occupies 8 bits. To get the R value, shift right by 16 bits and remove G and B

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template