From b7c44133c05cd952a1a27fbb6da16d257bcbca69 Mon Sep 17 00:00:00 2001 From: Kirils Sivokozs Date: Mon, 8 Feb 2016 16:57:51 +0200 Subject: [PATCH 1/2] Added support for 3-symbol hex strings. --- Colours.m | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Colours.m b/Colours.m index 2b500ea..6999d2d 100755 --- a/Colours.m +++ b/Colours.m @@ -47,11 +47,28 @@ @implementation NSColor (Colours) + (instancetype)colorFromHexString:(NSString *)hexString { unsigned rgbValue = 0; + int base = 0; + hexString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""]; NSScanner *scanner = [NSScanner scannerWithString:hexString]; [scanner scanHexInt:&rgbValue]; - - return [[self class] colorWithR:((rgbValue & 0xFF0000) >> 16) G:((rgbValue & 0xFF00) >> 8) B:(rgbValue & 0xFF) A:1.0]; + + switch (hexString.length) { + case 3: + base = 4; + break; + case 6: + base = 16; + break; + default: + return nil; + } + + int r = 0xFF & (rgbValue >> base); + int g = 0xFF & (rgbValue >> base / 2); + int b = 0xFF & (rgbValue >> 0); + + return [[self class] colorWithR:r G:g B:b A:1.0]; } From eb6e08cf060c70887ea9164a87838cb7ed2ec280 Mon Sep 17 00:00:00 2001 From: Kirils Sivokozs Date: Tue, 23 Feb 2016 13:16:25 +0200 Subject: [PATCH 2/2] Reworked hex color parsing logics. 3-symbol hex works in swift. --- Colours.m | 17 ++++++++++------- Colours.swift | 20 +++++++++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Colours.m b/Colours.m index 6999d2d..2aa19bc 100755 --- a/Colours.m +++ b/Colours.m @@ -46,29 +46,32 @@ @implementation NSColor (Colours) #pragma mark - Color from Hex + (instancetype)colorFromHexString:(NSString *)hexString { - unsigned rgbValue = 0; + unsigned long long rgbValue = 0; int base = 0; + int mask = 0; hexString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""]; NSScanner *scanner = [NSScanner scannerWithString:hexString]; - [scanner scanHexInt:&rgbValue]; + [scanner scanHexLongLong:&rgbValue]; switch (hexString.length) { case 3: - base = 4; + base = 8; + mask = 0xF; break; case 6: base = 16; + mask = 0xFF; break; default: return nil; } - int r = 0xFF & (rgbValue >> base); - int g = 0xFF & (rgbValue >> base / 2); - int b = 0xFF & (rgbValue >> 0); + CGFloat r = (CGFloat)(mask & (rgbValue >> base)) / (CGFloat)mask; + CGFloat g = (CGFloat)(mask & (rgbValue >> (base / 2))) / (CGFloat)mask; + CGFloat b = (CGFloat)(mask & (rgbValue >> 0)) / (CGFloat)mask; - return [[self class] colorWithR:r G:g B:b A:1.0]; + return [[self class] colorWithRed:r green:g blue:b alpha:1.0]; } diff --git a/Colours.swift b/Colours.swift index daa63fe..6b6512c 100644 --- a/Colours.swift +++ b/Colours.swift @@ -40,12 +40,26 @@ public extension Color { // MARK: - Color from Hex/RGBA/HSBA/CIE_LAB/CMYK convenience init(hex: String) { var rgbInt: UInt64 = 0 + var base: UInt64 = 0 + var mask: UInt64 = 0 + let newHex = hex.stringByReplacingOccurrencesOfString("#", withString: "") let scanner = NSScanner(string: newHex) scanner.scanHexLongLong(&rgbInt) - let r: CGFloat = CGFloat((rgbInt & 0xFF0000) >> 16)/255.0 - let g: CGFloat = CGFloat((rgbInt & 0x00FF00) >> 8)/255.0 - let b: CGFloat = CGFloat(rgbInt & 0x0000FF)/255.0 + + switch (newHex.characters.count) { + case 3: + base = 8 + mask = 0xF + default: + base = 16 + mask = 0xFF + } + + let r = CGFloat(mask & (rgbInt >> base)) / CGFloat(mask); + let g = CGFloat(mask & (rgbInt >> (base / 2))) / CGFloat(mask); + let b = CGFloat(mask & (rgbInt >> 0)) / CGFloat(mask); + self.init(red: r, green: g, blue: b, alpha: 1.0) }