-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSGraphics_SKExtensions.m
193 lines (170 loc) · 8.66 KB
/
NSGraphics_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
//
// NSGraphics_SKExtensions.m
// Skim
//
// Created by Christiaan Hofman on 10/20/11.
/*
This software is Copyright (c) 2011
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 "NSGraphics_SKExtensions.h"
#import "NSGeometry_SKExtensions.h"
#import "NSColor_SKExtensions.h"
#import "NSUserDefaults_SKExtensions.h"
#import <Quartz/Quartz.h>
#import "SKStringConstants.h"
#if SDK_BEFORE_10_14
@interface NSAppearance (SKMojaveExtensions)
- (NSString *)bestMatchFromAppearancesWithNames:(NSArray *)names;
@end
@interface NSApplication (SKMojaveExtensions) <NSAppearanceCustomization>
@end
#endif
BOOL SKHasDarkAppearance() {
if (@available(macOS 10.14, *))
return [[[NSApp effectiveAppearance] bestMatchFromAppearancesWithNames:@[NSAppearanceNameAqua, NSAppearanceNameDarkAqua]] isEqualToString:NSAppearanceNameDarkAqua];
return NO;
}
void SKRunWithAppearance(id object, void (^code)(void)) {
if (@available(macOS 10.14, *)) {
NSAppearance *appearance = nil;
if ([object respondsToSelector:@selector(effectiveAppearance)]) {
appearance = [NSAppearance currentAppearance];
[NSAppearance setCurrentAppearance:[(id<NSAppearanceCustomization>)object effectiveAppearance]];
}
code();
if ([object respondsToSelector:@selector(effectiveAppearance)])
[NSAppearance setCurrentAppearance:appearance];
} else {
code();
}
}
#pragma mark -
void SKSetColorsForResizeHandle(CGContextRef context, BOOL active)
{
NSColor *color = [NSColor selectionHighlightInteriorColor:active];
CGContextSetFillColorWithColor(context, [color CGColor]);
color = [NSColor selectionHighlightColor:active];
CGContextSetStrokeColorWithColor(context, [color CGColor]);
}
void SKFillStrokeResizeHandle(CGContextRef context, NSPoint point, CGFloat lineWidth)
{
CGRect rect = CGRectMake(point.x - 3.5 * lineWidth, point.y - 3.5 * lineWidth, 7.0 * lineWidth, 7.0 * lineWidth);
CGContextFillEllipseInRect(context, rect);
CGContextStrokeEllipseInRect(context, rect);
}
void SKDrawResizeHandles(CGContextRef context, NSRect rect, CGFloat lineWidth, BOOL connected, BOOL active)
{
SKSetColorsForResizeHandle(context, active);
CGContextSetLineWidth(context, lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMinX(rect), NSMidY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMidX(rect), NSMaxY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMidX(rect), NSMinY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMaxX(rect), NSMidY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMinX(rect), NSMaxY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMinX(rect), NSMinY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMaxX(rect), NSMaxY(rect)), lineWidth);
SKFillStrokeResizeHandle(context, NSMakePoint(NSMaxX(rect), NSMinY(rect)), lineWidth);
if (connected) {
if (NSWidth(rect) > 14.0 * lineWidth) {
CGFloat minY = NSMinY(rect) - 0.5 * lineWidth;
CGFloat maxY = NSMaxY(rect) + 0.5 * lineWidth;
CGPoint points[8] = {
{NSMinX(rect) + 3.5 * lineWidth, maxY},
{NSMidX(rect) - 3.5 * lineWidth, maxY},
{NSMidX(rect) + 3.5 * lineWidth, maxY},
{NSMaxX(rect) - 3.5 * lineWidth, maxY},
{NSMinX(rect) + 3.5 * lineWidth, minY},
{NSMidX(rect) - 3.5 * lineWidth, minY},
{NSMidX(rect) + 3.5 * lineWidth, minY},
{NSMaxX(rect) - 3.5 * lineWidth, minY}};
CGContextStrokeLineSegments(context, points, 8);
}
if (NSHeight(rect) > 14.0 * lineWidth) {
CGFloat minX = NSMinX(rect) - 0.5 * lineWidth;
CGFloat maxX = NSMaxX(rect) + 0.5 * lineWidth;
CGPoint points[8] = {
{minX, NSMinY(rect) + 3.5 * lineWidth},
{minX, NSMidY(rect) - 3.5 * lineWidth},
{minX, NSMidY(rect) + 3.5 * lineWidth},
{minX, NSMaxY(rect) - 3.5 * lineWidth},
{maxX, NSMinY(rect) + 3.5 * lineWidth},
{maxX, NSMidY(rect) - 3.5 * lineWidth},
{maxX, NSMidY(rect) + 3.5 * lineWidth},
{maxX, NSMaxY(rect) - 3.5 * lineWidth}};
CGContextStrokeLineSegments(context, points, 8);
}
}
}
void SKDrawResizeHandlePair(CGContextRef context, NSPoint point1, NSPoint point2, CGFloat lineWidth, BOOL active)
{
SKSetColorsForResizeHandle(context, active);
CGContextSetLineWidth(context, lineWidth);
SKFillStrokeResizeHandle(context, point1, lineWidth);
SKFillStrokeResizeHandle(context, point2, lineWidth);
}
#pragma mark -
void SKDrawTextFieldBezel(NSRect rect, NSView *controlView) {
static NSTextFieldCell *cell = nil;
if (cell == nil) {
cell = [[NSTextFieldCell alloc] initTextCell:@""];
[cell setBezeled:YES];
}
[cell drawWithFrame:rect inView:controlView];
[cell setControlView:nil];
}
#pragma mark -
#define LR 0.2126
#define LG 0.7152
#define LB 0.0722
extern NSArray *SKColorEffectFilters(void) {
NSMutableArray *filters = [NSMutableArray array];
CIFilter *filter;
CGFloat sepia = [[NSUserDefaults standardUserDefaults] doubleForKey:SKSepiaToneKey];
if (sepia > 0.0) {
if ((filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues:@"inputIntensity", [NSNumber numberWithDouble:fmin(sepia, 1.0)], nil]))
[filters addObject:filter];
}
NSColor *white = [[NSUserDefaults standardUserDefaults] colorForKey:SKWhitePointKey];
if (white) {
if ((filter = [CIFilter filterWithName:@"CIWhitePointAdjust" keysAndValues:@"inputColor", [[CIColor alloc] initWithColor:white], nil]))
[filters addObject:filter];
}
if (SKHasDarkAppearance() && [[NSUserDefaults standardUserDefaults] boolForKey:SKInvertColorsInDarkModeKey]) {
// map the white page background to 45/255, or 30/255 with high contrast
CGFloat f = [[NSWorkspace sharedWorkspace] accessibilityDisplayShouldIncreaseContrast] ? 1.9337 : 1.8972;
// This is like CIColorInvert + CIHueAdjust, modified to map white to dark gray rather than black
// Inverts a linear luminocity with weights from the CIE standards
// see https://wiki.preterhuman.net/Matrix_Operations_for_Image_Processing and https://beesbuzz.biz/code/16-hsv-color-transforms
if ((filter = [CIFilter filterWithName:@"CIGammaAdjust" keysAndValues:@"inputPower", [NSNumber numberWithDouble:0.625], nil]))
[filters addObject:filter];
if ((filter = [CIFilter filterWithName:@"CIColorMatrix" keysAndValues:@"inputRVector", [CIVector vectorWithX:1.0-LR*f Y:-LG*f Z:-LB*f W:0.0], @"inputGVector", [CIVector vectorWithX:-LR*f Y:1.0-LG*f Z:-LB*f W:0.0], @"inputBVector", [CIVector vectorWithX:-LR*f Y:-LG*f Z:1.0-LB*f W:0.0], @"inputAVector", [CIVector vectorWithX:0.0 Y:0.0 Z:0.0 W:1.0], @"inputBiasVector", [CIVector vectorWithX:1.0 Y:1.0 Z:1.0 W:0.0], nil]))
[filters addObject:filter];
if ((filter = [CIFilter filterWithName:@"CIGammaAdjust" keysAndValues:@"inputPower", [NSNumber numberWithDouble:1.6], nil]))
[filters addObject:filter];
}
return filters;
}