tolo / InterfaCSS

The CSS-inspired styling and layout framework for iOS
MIT License
96 stars 10 forks source link

Support for RGBA hex string #48

Closed vitorhugomagalhaes closed 8 years ago

vitorhugomagalhaes commented 8 years ago

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,

tolo commented 8 years ago

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

vitorhugomagalhaes commented 8 years ago

Great !

Thanks for the quick feedback.