-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNSColor_SKExtensions.m
202 lines (183 loc) · 9.42 KB
/
NSColor_SKExtensions.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// NSColor_SKExtensions.m
// Skim
//
// Created by Christiaan Hofman on 6/17/07.
/*
This software is Copyright (c) 2007-2010
Christiaan Hofman. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
- Neither the name of Christiaan Hofman nor the names of any
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "NSColor_SKExtensions.h"
@implementation NSColor (SKExtensions)
+ (NSColor *)keySourceListHighlightColor {
static NSColor *color = nil;
static NSColor *graphiteColor = nil;
if ([NSColor currentControlTint] == NSGraphiteControlTint) {
if (graphiteColor == nil)
graphiteColor = [[NSColor colorWithDeviceRed:24672.0/65535.0 green:29812.0/65535.0 blue:35466.0/65535.0 alpha:1.0] retain];
return graphiteColor;
} else {
if (color == nil)
color = [[NSColor colorWithDeviceRed:14135.0/65535.0 green:29298.0/65535.0 blue:48830.0/65535.0 alpha:1.0] retain];
return color;
}
}
+ (NSColor *)mainSourceListHighlightColor {
static NSColor *color = nil;
static NSColor *graphiteColor = nil;
if ([NSColor currentControlTint] == NSGraphiteControlTint) {
if (graphiteColor == nil)
graphiteColor = [[NSColor colorWithDeviceRed:37779.0/65535.0 green:41634.0/65535.0 blue:45489.0/65535.0 alpha:1.0] retain];
return graphiteColor;
} else {
if (color == nil)
color = [[NSColor colorWithDeviceRed:34695.0/65535.0 green:39064.0/65535.0 blue:48316.0/65535.0 alpha:1.0] retain];
return color;
}
}
+ (NSColor *)disabledSourceListHighlightColor {
static NSColor *color = nil;
static NSColor *graphiteColor = nil;
if ([NSColor currentControlTint] == NSGraphiteControlTint) {
if (graphiteColor == nil)
graphiteColor = [[NSColor colorWithDeviceRed:40606.0/65535.0 green:40606.0/65535.0 blue:40606.0/65535.0 alpha:1.0] retain];
return graphiteColor;
} else {
if (color == nil)
color = [[NSColor colorWithDeviceRed:40606.0/65535.0 green:40606.0/65535.0 blue:40606.0/65535.0 alpha:1.0] retain];
return color;
}
}
- (uint32_t)uint32HSBAValue {
NSColor *rgbColor = [self colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
if (rgbColor) {
CGFloat h = 0.0, s = 0.0, b = 0.0, a = 0.0;
[rgbColor getHue:&h saturation:&s brightness:&b alpha:&a];
union _ {
struct {
uint8_t h;
uint8_t s;
uint8_t b;
uint8_t a;
} hsba;
uint32_t uintValue;
} u;
u.hsba.h = (uint8_t)(h * 255);
u.hsba.s = (uint8_t)(s * 255);
u.hsba.b = (uint8_t)(b * 255);
u.hsba.a = (uint8_t)(a * 255);
return CFSwapInt32HostToBig(u.uintValue);
}
return 0;
}
- (NSComparisonResult)colorCompare:(NSColor *)aColor {
uint32_t value1 = [self uint32HSBAValue];
uint32_t value2 = [aColor uint32HSBAValue];
if (value1 < value2)
return NSOrderedAscending;
else if (value1 > value2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
+ (id)scriptingRgbaColorWithDescriptor:(NSAppleEventDescriptor *)descriptor {
if ([descriptor numberOfItems] > 0) {
CGFloat red, green, blue, alpha;
red = green = blue = (CGFloat)[[descriptor descriptorAtIndex:1] int32Value] / 65535.0f;
if ([descriptor numberOfItems] > 2) {
green = (CGFloat)[[descriptor descriptorAtIndex:2] int32Value] / 65535.0f;
blue = (CGFloat)[[descriptor descriptorAtIndex:3] int32Value] / 65535.0f;
}
if ([descriptor numberOfItems] == 2)
alpha = (CGFloat)[[descriptor descriptorAtIndex:2] int32Value] / 65535.0f;
else if ([descriptor numberOfItems] > 3)
alpha = (CGFloat)[[descriptor descriptorAtIndex:4] int32Value] / 65535.0f;
else
alpha= 1.0;
return [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
} else {
switch ([descriptor enumCodeValue]) {
case SKScriptingColorRed: return [NSColor redColor];
case SKScriptingColorGreen: return [NSColor greenColor];
case SKScriptingColorBlue: return [NSColor blueColor];
case SKScriptingColorYellow: return [NSColor yellowColor];
case SKScriptingColorMagenta: return [NSColor magentaColor];
case SKScriptingColorCyan: return [NSColor cyanColor];
case SKScriptingColorDarkRed: return [NSColor colorWithCalibratedRed:0.5 green:0.0 blue:0.0 alpha:1.0];
case SKScriptingColorDarkGreen: return [NSColor colorWithCalibratedRed:0.0 green:0.5 blue:0.0 alpha:1.0];
case SKScriptingColorDarkBlue: return [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.5 alpha:1.0];
case SKScriptingColorBanana: return [NSColor colorWithCalibratedRed:1.0 green:1.0 blue:0.5 alpha:1.0];
case SKScriptingColorTurquoise: return [NSColor colorWithCalibratedRed:1.0 green:0.5 blue:1.0 alpha:1.0];
case SKScriptingColorViolet: return [NSColor colorWithCalibratedRed:0.5 green:1.0 blue:1.0 alpha:1.0];
case SKScriptingColorOrange: return [NSColor orangeColor];
case SKScriptingColorDeepPink: return [NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.5 alpha:1.0];
case SKScriptingColorSpringGreen: return [NSColor colorWithCalibratedRed:0.0 green:1.0 blue:0.5 alpha:1.0];
case SKScriptingColorAqua: return [NSColor colorWithCalibratedRed:0.0 green:0.5 blue:1.0 alpha:1.0];
case SKScriptingColorLime: return [NSColor colorWithCalibratedRed:0.5 green:1.0 blue:0.0 alpha:1.0];
case SKScriptingColorDarkViolet: return [NSColor colorWithCalibratedRed:0.5 green:0.0 blue:1.0 alpha:1.0];
case SKScriptingColorPurple: return [NSColor purpleColor];
case SKScriptingColorTeal: return [NSColor colorWithCalibratedRed:0.0 green:0.5 blue:0.5 alpha:1.0];
case SKScriptingColorOlive: return [NSColor colorWithCalibratedRed:0.5 green:0.5 blue:0.0 alpha:1.0];
case SKScriptingColorBrown: return [NSColor brownColor];
case SKScriptingColorBlack: return [NSColor blackColor];
case SKScriptingColorWhite: return [NSColor whiteColor];
case SKScriptingColorGray: return [NSColor grayColor];
case SKScriptingColorDarkGray: return [NSColor darkGrayColor];
case SKScriptingColorLightGray: return [NSColor lightGrayColor];
case SKScriptingColorClear: return [NSColor clearColor];
default:
{
// Cocoa Scripting defines coercions from string to color for some standard color names
NSString *string = [descriptor stringValue];
if (string) {
NSColor *color = [[NSScriptCoercionHandler sharedCoercionHandler] coerceValue:string toClass:[NSColor class]];
// We should check the return value, because NSScriptCoercionHandler returns the input when it fails rather than nil, stupid
return [color isKindOfClass:[NSColor class]] ? color : nil;
}
}
}
return nil;
}
}
- (id)scriptingRgbaColorDescriptor;
{
CGFloat red, green, blue, alpha;
[[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
NSAppleEventDescriptor *descriptor = [NSAppleEventDescriptor listDescriptor];
[descriptor insertDescriptor:[NSAppleEventDescriptor descriptorWithInt32:round(65535 * red)] atIndex:1];
[descriptor insertDescriptor:[NSAppleEventDescriptor descriptorWithInt32:round(65535 * green)] atIndex:2];
[descriptor insertDescriptor:[NSAppleEventDescriptor descriptorWithInt32:round(65535 * blue)] atIndex:3];
[descriptor insertDescriptor:[NSAppleEventDescriptor descriptorWithInt32:round(65535 * alpha)] atIndex:4];
return descriptor;
}
- (NSString *)accessibilityValue {
static NSColorWell *colorWell = nil;
if (colorWell == nil)
colorWell = [[NSColorWell alloc] init];
[colorWell setColor:self];
return [colorWell accessibilityAttributeValue:NSAccessibilityValueAttribute];
}
@end