Repository navigation
Expand file tree
/
Copy pathWDColorWell.m
More file actions
162 lines (129 loc) · 4.21 KB
/
Copy pathWDColorWell.m
File metadata and controls
162 lines (129 loc) · 4.21 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
157
158
159
160
161
162
//
// WDColorWell.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2008-2013 Steve Sprang
//
#import "WDColor.h"
#import "WDColorWell.h"
#import "WDGradient.h"
#import "WDGradientStopIndicator.h"
#import "WDGradientStop.h"
#import "WDUtilities.h"
#import "UIView+Additions.h"
@implementation WDColorWell
@synthesize painter = painter_;
@synthesize barButtonItem = barButtonItem_;
@synthesize strokeMode = strokeMode_;
@synthesize gradientStopMode = gradientStopMode_;
@synthesize shadowMode = shadowMode_;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
return self;
}
- (void) setPainter:(id<WDPathPainter>)painter
{
if (painter == painter_) {
return;
}
painter_ = painter;
if (gradientStop_) {
gradientStop_.stop = [WDGradientStop stopWithColor:(WDColor *)painter_ andRatio:0];
}
if (shadowMode_) {
shadowView_.layer.shadowColor = ((WDColor *) painter_).CGColor;
} else {
[self setNeedsDisplay];
}
}
- (void) setShadowMode:(BOOL)shadowMode
{
shadowMode_ = shadowMode;
if (!shadowView_) {
shadowView_ = [[UIView alloc] initWithFrame:CGRectInset(self.bounds, 5, 5)];
shadowView_.opaque = NO;
shadowView_.backgroundColor = nil;
CALayer *layer = shadowView_.layer;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shadowView_.bounds cornerRadius:5];
layer.shadowPath = path.CGPath;
layer.shadowOffset = CGSizeZero;
layer.shadowRadius = 3;
layer.shadowColor = ((WDColor *) painter_).CGColor;
layer.shadowOpacity = 1;
[self addSubview:shadowView_];
shadowView_.sharpCenter = WDCenterOfRect(self.bounds);
}
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
if (shadowMode_) {
WDDrawCheckersInRect(ctx, rect, 7);
[[UIColor blackColor] set];
UIRectFrame(bounds);
return;
}
if (barButtonItem_) {
int inset = ceil((CGRectGetHeight(bounds) - CGRectGetWidth(bounds)) / 2);
bounds = CGRectInset(bounds, 0, inset);
}
int inset = barButtonItem_ ? 7 : 10;
CGRect hole = CGRectInset(bounds, inset, inset);
if (strokeMode_) {
CGContextSaveGState(ctx);
CGContextAddRect(ctx, bounds);
CGContextAddRect(ctx, hole);
CGContextEOClip(ctx);
}
if (painter_) {
[painter_ drawSwatchInRect:bounds];
} else {
[[UIColor whiteColor] set];
CGContextFillRect(ctx, bounds);
CGContextSaveGState(ctx);
CGContextClipToRect(ctx, bounds);
[[UIColor redColor] set];
CGContextSetLineWidth(ctx, 2.0f);
CGContextMoveToPoint(ctx, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGContextAddLineToPoint(ctx, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
CGContextStrokePath(ctx);
CGContextRestoreGState(ctx);
}
if (strokeMode_) {
CGContextRestoreGState(ctx);
[[UIColor blackColor] set];
UIRectFrame(hole);
}
[[UIColor blackColor] set];
UIRectFrame(bounds);
}
- (void) setGradientStopMode:(BOOL)gradient
{
if (gradient && !gradientStop_) {
gradientStop_ = [[WDGradientStopIndicator alloc] initWithStop:[WDGradientStop stopWithColor:(WDColor *)self.painter andRatio:0]];
gradientStop_.selected = YES;
[self addSubview:gradientStop_];
CGRect frame = gradientStop_.frame;
gradientStop_.sharpCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(self.bounds) + CGRectGetHeight(frame) / 2);
} else if (!gradient && gradientStop_) {
[gradientStop_ removeFromSuperview];
gradientStop_ = nil;
}
}
- (void) setStrokeMode:(BOOL)strokeMode
{
strokeMode_ = strokeMode;
if (strokeMode) {
self.backgroundColor = nil;
self.opaque = NO;
}
}
@end