tolo / InterfaCSS

The CSS-inspired styling and layout framework for iOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for RGBA hex string

vitorhugomagalhaes opened this issue · comments

Hi,

First of all, great library :)

I realised that there is no support for RGBA hex string although, with the following code it should be easy to support it.

+ (UIColor*) iss_colorWithHexString:(NSString*)hex {
    hex = [hex iss_trim];
    if ( hex.length == 6 ) {
        NSScanner* scanner = [NSScanner scannerWithString:hex];
        unsigned int cc = 0;

        if( [scanner scanHexInt:&cc] ) {
            NSInteger r = (cc >> 16) & 0xFF;
            NSInteger g = (cc >> 8) & 0xFF;
            NSInteger b = cc & 0xFF;
            return [self iss_colorWithR:r G:g B:b];
        }
    } else if ( hex.length == 8 ) {
        NSScanner* scanner = [NSScanner scannerWithString:hex];
        unsigned int cc = 0;

        if( [scanner scanHexInt:&cc] ) {
            NSInteger r = (cc >> 24) & 0xFF;
            NSInteger g = (cc >> 16) & 0xFF;
            NSInteger b = (cc >> 8)  & 0xFF;
            NSInteger a = cc & 0xFF;
            return [self iss_colorWithR:r G:g B:b A:a];
        }
    }
    return [UIColor magentaColor];
}

Could you integrate it into the master branch or do you want me to raise a pull request ?

BR,

Thanks! :)

Great suggestion - I've added support for alpha component in hex strings now, and also support for compact hex strings (3 or 4 hex digits, 4 bits per channel).

Cheers,
Tobias

Great !

Thanks for the quick feedback.