Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>instead of a macro I wrote a category for this. It handles RGB(A) value strings with the formats <code>@"#17BAFA(FF)", @"#ABC(F)", @"17BAFA", @"0xABC",…</code></p> <p>As it uses message sending, rather than a macro call, it feels more natural in the context of Objective-C.</p> <p>Use it like <code>UIColor *color = [UIColor colorFromHexString:@"#DCDFE3"];</code></p> <p>afterwards you can check <code>if(!color) { // must be an invalid format</code></p> <pre><code>@implementation UIColor (Creation) +(UIColor *)_colorFromHex:(NSUInteger)hexInt { int r,g,b,a; r = (hexInt &gt;&gt; 030) &amp; 0xFF; g = (hexInt &gt;&gt; 020) &amp; 0xFF; b = (hexInt &gt;&gt; 010) &amp; 0xFF; a = hexInt &amp; 0xFF; return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:a / 255.0f]; } +(UIColor *)colorFromHexString:(NSString *)hexString { hexString = [hexString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if ([hexString hasPrefix:@"#"]) hexString = [hexString substringFromIndex:1]; else if([hexString hasPrefix:@"0x"]) hexString = [hexString substringFromIndex:2]; int l = [hexString length]; if ((l!=3) &amp;&amp; (l!=4) &amp;&amp; (l!=6) &amp;&amp; (l!=8)) return nil; if ([hexString length] &gt; 2 &amp;&amp; [hexString length]&lt; 5) { NSMutableString *newHexString = [[NSMutableString alloc] initWithCapacity:[hexString length]*2]; [hexString enumerateSubstringsInRange:NSMakeRange(0, [hexString length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { [newHexString appendFormat:@"%@%@", substring, substring]; }]; hexString = newHexString; } if ([hexString length] == 6) hexString = [hexString stringByAppendingString:@"ff"]; NSScanner *scanner = [NSScanner scannerWithString:hexString]; unsigned hexNum; if (![scanner scanHexInt:&amp;hexNum]) return nil; return [self _colorFromHex:hexNum]; } @end </code></pre> <hr> <p>When I pick the keyboards background color with Photoshop, the value is <code>"0xD9DCE0"</code>, not <code>"0xDCDFE3"</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload