-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSWGradientFillAdditions.m
More file actions
156 lines (112 loc) · 4.26 KB
/
SWGradientFillAdditions.m
File metadata and controls
156 lines (112 loc) · 4.26 KB
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
//
// NSBezierPath (SWGradientAdditions).m
// This file is part of the "SWApplicationSupport" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 18/06/06.
// Copyright 2006 Samuel Williams. All rights reserved.
//
#import "SWGradientFillAdditions.h"
#import <CoreImage/CoreImage.h>
@implementation CIVector (SWConstructorsAddition)
+ (CIVector*) vectorWithSize: (NSSize)size {
return [[self class] vectorWithX:size.width Y:size.height];
}
+ (CIVector*) vectorWithPoint: (NSPoint)point {
return [[self class] vectorWithX:point.x Y:point.y];
}
- (float) lesserX: (CIVector*)other {
if (self.X < other.X)
return self.X;
else
return other.X;
}
- (float) lesserY: (CIVector*)other {
if (self.Y < other.Y)
return self.Y;
else
return other.Y;
}
- (float) lesserZ: (CIVector*)other {
if (self.Z < other.Z)
return self.Z;
else
return other.Z;
}
- (float) lesserW: (CIVector*)other {
if (self.W < other.W)
return self.W;
else
return other.W;
}
- (CIVector*) bottomLeft: (CIVector*)other {
return [CIVector vectorWithX:[self lesserX:other] Y:[self lesserY:other] Z:[self lesserZ:other] W:[self lesserW:other]];
}
- (CGPoint) toCGPoint {
return CGPointMake(self.X, self.Y);
}
@end
@implementation NSBezierPath (SWGradientAdditions)
- (void)fillGradientFrom: (CIVector*)startVector color:(NSColor*)inStartColor to: (CIVector*)endVector color:(NSColor*)inEndColor {
CIImage *gradientImage;
inStartColor = [inStartColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
inEndColor = [inEndColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
CIColor* startColor = [CIColor colorWithRed:inStartColor.redComponent green:inStartColor.greenComponent blue:inStartColor.blueComponent alpha:inStartColor.alphaComponent];
CIColor* endColor = [CIColor colorWithRed:inEndColor.redComponent green:inEndColor.greenComponent blue:inEndColor.blueComponent alpha:inEndColor.alphaComponent];
CIVector *bottomLeft = [startVector bottomLeft:endVector];
CIFilter* filter;
filter = [CIFilter filterWithName:@"CILinearGradient"];
[filter setValue:startColor forKey:@"inputColor0"];
[filter setValue:endColor forKey:@"inputColor1"];
[filter setValue:startVector forKey:@"inputPoint0"];
[filter setValue:endVector forKey:@"inputPoint1"];
gradientImage = [filter valueForKey:@"outputImage"];
[[NSGraphicsContext currentContext] saveGraphicsState];
CIContext* context = [NSGraphicsContext currentContext].CIContext;
//can be setClip, but this may increase the current clipping rect beyond that intended
[self addClip];
CGRect frameBounds = {
{0.0, 0.0},
self.bounds.size
};
CGRect gradientBounds = {
bottomLeft.toCGPoint,
self.bounds.size
};
[context drawImage:gradientImage inRect:gradientBounds fromRect:frameBounds];
[[NSGraphicsContext currentContext] restoreGraphicsState];
}
- (void) fillVerticalGradientTop: (NSColor*)top bottom: (NSColor*)bottom {
NSRect bounds = self.bounds;
NSPoint a = bounds.origin;
NSPoint b = NSMakePoint(bounds.origin.x, bounds.origin.y + bounds.size.height);
[self fillGradientFrom:[CIVector vectorWithPoint:a] color:top to:[CIVector vectorWithPoint:b] color:bottom];
}
- (void) fillHorizontalGradientLeft: (NSColor*)left right: (NSColor*)right {
NSRect bounds = self.bounds;
NSPoint a = bounds.origin;
NSPoint b = NSMakePoint(bounds.origin.x + bounds.size.width, bounds.origin.y);
[self fillGradientFrom:[CIVector vectorWithPoint:b] color:left to:[CIVector vectorWithPoint:a] color:right];
}
@end
@implementation SWGradientView
- (instancetype)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}
- (void)drawRect:(NSRect)rect {
NSRect viewBounds = self.bounds;
NSBezierPath *p = [NSBezierPath bezierPathWithRect:viewBounds];
NSColor *c = [[NSColor blueColor] colorWithAlphaComponent:0.1];
[p fillHorizontalGradientLeft:[NSColor grayColor] right:c];
NSBezierPath *line = [NSBezierPath bezierPath];
NSPoint a = {viewBounds.origin.x + viewBounds.size.width, viewBounds.origin.y};
NSPoint b = {viewBounds.origin.x + viewBounds.size.width, viewBounds.origin.y + viewBounds.size.height};
[line moveToPoint:a];
[line lineToPoint:b];
[[NSColor blackColor] setStroke];
[line stroke];
}
@end