forked from qnblackcat/iSponsorBlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorFunctions.h
37 lines (30 loc) · 1.32 KB
/
colorFunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
//https://stackoverflow.com/a/26341062
static NSString *hexFromUIColor(UIColor *color) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
CGFloat r = components[0];
CGFloat g = components[1];
CGFloat b = components[2];
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(r * 255),
lroundf(g * 255),
lroundf(b * 255)];
}
static CGFloat colorComponentFrom(NSString *string, NSUInteger start, NSUInteger length) {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
static UIColor *colorWithHexString(NSString *hexString) {
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
CGFloat alpha, red, blue, green;
// #RGB
alpha = 1.0f;
red = colorComponentFrom(colorString,0,2);
green = colorComponentFrom(colorString,2,2);
blue = colorComponentFrom(colorString,4,2);
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}