IOS Color Value Conversion dry

Source: Internet
Author: User

1, integer RGB set color

Convenience init (Red:int, Green:int, blue:int, alp:cgfloat =1.0) {assert (red>=0&& Red <=255,"Invalid Red Component") assert (green>=0&& Green <=255,"Invalid Green Component") assert (blue>=0&& Blue <=255,"Invalid Blue Component") Self.init (red:cgfloat (red)/255.0, Green:cgfloat (green)/255.0, Blue:cgfloat (blue)/255.0, Alpha:alp)}

2, 16 binary set color

convenience init (hexint:int) {      0xff80xff0xff

3. String setting color

convenience init (hexstring:string) {var hexstr= Hexstring.stringbytrimmingcharactersinset (Nscharacterset.whitespaceandnewlinecharacterset () asnscharacterset). uppercasestringif(Hexstr.hasprefix ("#") ) {hexstr= Hexstr.substringfromindex (HexStr.startIndex.advancedBy (1)} var hexint:uint32=0Nsscanner (string: hexstr). Scanhexint (&hexint) Self.init (Red:int (Hexint>> -) &0xFF), Green:int (Hexint >>8) &0xFF), Blue:int (Hexint &0xFF))    }}

Entire package:

extension Uicolor {//RGB integer Set colorConvenience init (Red:int, Green:int, blue:int, alp:cgfloat =1.0) {assert (red>=0&& Red <=255,"Invalid Red Component") assert (green>=0&& Green <=255,"Invalid Green Component") assert (blue>=0&& Blue <=255,"Invalid Blue Component") Self.init (red:cgfloat (red)/255.0, Green:cgfloat (green)/255.0, Blue:cgfloat (blue)/255.0, Alpha:alp)} //16 binary Set colorconvenience init (hexint:int) {Self.init (red: (Hexint>> -) &0xFF, Green: (Hexint >>8) &0xFF, Blue:hexint &0xFF)        }                     //string Setting colorconvenience init (hexstring:string) {var hexstr= Hexstring.stringbytrimmingcharactersinset (Nscharacterset.whitespaceandnewlinecharacterset () asnscharacterset). uppercasestringif(Hexstr.hasprefix ("#") ) {hexstr= Hexstr.substringfromindex (HexStr.startIndex.advancedBy (1)} var hexint:uint32=0Nsscanner (string: hexstr). Scanhexint (&hexint) Self.init (Red:int (Hexint>> -) &0xFF), Green:int (Hexint >>8) &0xFF), Blue:int (Hexint &0xFF))        }}

Application:

253  the  -  2530.9"#A94442 "   "#DCA7A7"0xdcdcdc0xdcdaaa)

IOS Color Conversion Dry Goods

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.