This repository has been archived by the owner on Jul 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSColor+Extra.m
executable file
·129 lines (99 loc) · 4.67 KB
/
NSColor+Extra.m
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// NSColor+Hex.m
// PHPEdit
//
// Created by Akki on 4/2/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "NSColor+Extra.h"
@implementation NSColor (Extra)
- (NSString *) hexColor {
float r, g, b;
if ([[self colorSpaceName] isEqualToString:NSCalibratedWhiteColorSpace]) {
r = [self whiteComponent];
g = [self whiteComponent];
b = [self whiteComponent];
}
else if ([[self colorSpaceName] isEqualToString:NSCalibratedRGBColorSpace]
|| [[self colorSpaceName] isEqualToString:NSDeviceRGBColorSpace] || [[self colorSpaceName] isEqualToString:NSCustomColorSpace]) {
r = [self redComponent];
g = [self greenComponent];
b = [self blueComponent];
}
else {
return @"transparent";
}
return [NSString stringWithFormat:@"#%0.2X%0.2X%0.2X",
(int)(r * 255),
(int)(g * 255),
(int)(b * 255)];
}
- (NSInteger)intColor {
uint8_t r = (uint32_t)(MIN(1.0f, MAX(0.0f, [self redComponent])) * 0xff);
uint8_t g = (uint32_t)(MIN(1.0f, MAX(0.0f, [self greenComponent])) * 0xff);
uint8_t b = (uint32_t)(MIN(1.0f, MAX(0.0f, [self blueComponent])) * 0xff);
uint8_t alfa = (uint32_t)(MIN(1.0f, MAX(0.0f, [self alphaComponent])) * 0xff);
int value = (alfa << 24) | (b<< 16) | (g << 8) | r;
return value;
}
+ (NSColor *) colorWithHex:(NSString *)hexColor {
// Remove the hash if it exists
hexColor = [hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""];
int length = (int)[hexColor length];
bool triple = (length == 3);
NSMutableArray *rgb = [[NSMutableArray alloc] init];
// Make sure the string is three or six characters long
if (triple || length == 6) {
CFIndex i = 0;
UniChar character = 0;
NSString *segment = @"";
CFStringInlineBuffer buffer;
CFStringInitInlineBuffer((__bridge CFStringRef)hexColor, &buffer, CFRangeMake(0, length));
while ((character = CFStringGetCharacterFromInlineBuffer(&buffer, i)) != 0 ) {
if (triple) segment = [segment stringByAppendingFormat:@"%c%c", character, character];
else segment = [segment stringByAppendingFormat:@"%c", character];
if ((int)[segment length] == 2) {
NSScanner *scanner = [[NSScanner alloc] initWithString:segment];
unsigned number;
while([scanner scanHexInt:&number]){
[rgb addObject:[NSNumber numberWithFloat:(float)(number / (float)255)]];
}
segment = @"";
}
i++;
}
// Pad the array out (for cases where we're given invalid input)
while ([rgb count] != 3) [rgb addObject:[NSNumber numberWithFloat:0.0]];
return [NSColor colorWithCalibratedRed:[[rgb objectAtIndex:0] floatValue]
green:[[rgb objectAtIndex:1] floatValue]
blue:[[rgb objectAtIndex:2] floatValue]
alpha:1];
}
else {
NSException* invalidHexException = [NSException exceptionWithName:@"InvalidHexException"
reason:@"Hex color not three or six characters excluding hash"
userInfo:nil];
@throw invalidHexException;
}
}
- (NSColor *)invertedColor
{
float r, g, b, alfa;
if ([[self colorSpaceName] isEqualToString:NSCalibratedRGBColorSpace] || [[self colorSpaceName] isEqualToString:NSDeviceRGBColorSpace]) {
r = [self redComponent];
g = [self greenComponent];
b = [self blueComponent];
alfa = [self alphaComponent];
} else
return nil;
return [NSColor colorWithCalibratedRed:1-r green:1-g blue:1-b alpha:alfa];
}
- (CGColorRef)coreGraphicsColorWithAlfa:(CGFloat)alfa
{
NSColor *bufferColor = [self colorUsingColorSpaceName:NSDeviceRGBColorSpace];
if (alfa != -1)
return [NSColor colorWithCalibratedRed:[bufferColor redComponent] green:[bufferColor greenComponent] blue:[bufferColor blueComponent] alpha:alfa].CGColor;//CGColorCreateGenericRGB([bufferColor redComponent], [bufferColor greenComponent], [bufferColor blueComponent], alfa);
else
return [NSColor colorWithCalibratedRed:[bufferColor redComponent] green:[bufferColor greenComponent] blue:[bufferColor blueComponent] alpha:[bufferColor alphaComponent]].CGColor;;
}
@end