IOS development -- what if ARGB/RGBA is used, ios -- argbrgba

Source: Internet
Author: User

IOS development -- what if ARGB/RGBA is used, ios -- argbrgba

I. Preface

During iOS development, we often use hexadecimal notation to represent the color value. In general, we use this notation: for example, #000000 represents the black color and # ffffff represents the white color, but what if we want transparency?

Next let's take a look at how to obtain the color values of argb/rgba.

2. First, let's take a look at the basic knowledge (it has nothing to do with iOS and is common)

1. hexadecimal color code

In short, a 6-digit hexadecimal value starting with # represents a color.

In rgb, red can be expressed as (255, 0, 0). Then 255/16 is converted to hexadecimal (is used for calculation here), that is, FF, 0, or 00, therefore, the red hexadecimal representation is # FF0000.

2. Shift left </shift right>

12 <2. What is the result of moving 12 to the left two places?

First, convert 12 to 2: 1100, and then move 1100 to the left, and then add 0 to the number of digits, that is, 0000, that is, 0.

12> 2, convert it to 1100, and then shift it to the right of 2 places. Fill in 0 for the remaining digits, that is, 0011, that is, 3.

3. Go to iOS and process argb

Put the following code directly:

+ (UIColor *) colorWithHextColorString :( NSString *) hexColorString {// here is alpha pass 1. In colorWithHextColorString alpha, alpha is modified to return [self colorWithHextColorString: hexColorString alpha: 1.0f];} // supports rgb, argb + (UIColor *) colorWithHextColorString :( NSString *) hexColorString alpha :( CGFloat) alphaValue {UIColor * result = nil; unsigned int colorCode = 0; unsigned char redByte, greenByte, blueByte; // exclude @ \ "I F ([hexColorString hasPrefix: @ "@ \" "]) {hexColorString = [hexColorString substringWithRange: NSMakeRange (2, hexColorString. length-3)];} // exclude # if ([hexColorString hasPrefix: @ "#"]) {hexColorString = [hexColorString substringFromIndex: 1];} if (nil! = HexColorString) {NSScanner * callback = [NSScanner scannerWithString: hexColorString]; (void) [Export scanHexInt: & colorCode]; // ignore error} redByte = (unsigned char) (colorCode> 16); greenByte = (unsigned char) (colorCode> 8); blueByte = (unsigned char) (colorCode ); // masks off high bits if ([hexColorString length] = 8) {// if it is 8 bits, then alpha alphaValue = (float) (unsigned char) (colorCode> 24)/0xff;} NSLog (@ "alpha: % f ---- r: % f ---- g: % f ---- B: % f", alphaValue, (float) redByte/0xff, (float) greenByte/0xff, (float) blueByte/0xff); result = [UIColor colorWithRed: (float) redByte/0xff green: (float) greenByte/0xff blue: (float) blueByte/0xff alpha: alphaValue]; return result ;}

For example, FF0000. The result is as follows:

AEFF0000 (AE: 10 stands for 174). The running result is as follows:

The argb parsing method is perfectly implemented.

Briefly introduces its implementation:

First, set the alpha value of the colorWithHextColorString: alpha: Method to 1, that is, opacity.

Then, in colorWithHextColorString: alpha:, first filter out other illegal characters of the passed hexColorString, such. Then, use NSScanner to scan the data and convert it into a 10-digit system.

Then, the values of red, green, and blue are obtained through the shift operation. If the value of hexColorString is 8 bits, it means that hexColorString contains the alpha value, and the value of alphaValue is obtained by 24 bits to the right.

Finally

+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;

Method to set its color.

What is hard to understand here is the shift operation. Let's give a brief introduction.

First, the hexColorString after @ \ "and # are excluded as AEFF0000, and then the colorCode of the scan result is: 2935947264. Then perform the shift operation (using the above method), set the result of colorCode> 16 to 44799 (in the console po colorCode> 16), and use the calculator to calculate the value. The output is as follows:

po (unsigned char)(colorCode>>16)

The result is \ xff, that is, the hexadecimal ff, that is, the red part of the hexColorString. Here, we only add an unsigned char, and it turns into a hexadecimal ff, which is a bit hard to understand (maybe Apple converted it internally) and then reused.

(float)(unsigned char)(colorCode>>16)/0xff

Get the float value and substitute it into the class method of UIColor for parsing. Finally, obtain the required color.

Note: For rgba, you can modify the shift slightly to implement rgba resolution.

Iv. Conclusion

That's simple. Contact us if you have any questions.

 

Related Article

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.