-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGradientView.swift
180 lines (142 loc) · 5.72 KB
/
GradientView.swift
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
// GradientView.swift
//
// Copyright (c) 2015 Alexander Edge
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
import QuartzCore
#if os(OSX)
import AppKit
typealias Color = NSColor
typealias Image = NSImage
typealias View = NSView
#else
import UIKit
typealias Color = UIColor
typealias Image = UIImage
typealias View = UIView
#endif
extension Color {
class func randomComponent() -> CGFloat {
return CGFloat(arc4random_uniform(255)) / 255.0
}
class func randomColour() -> Color {
return Color(red: randomComponent(), green: randomComponent(), blue: randomComponent(), alpha: 1.0)
}
func colorByShiftingHue(shift : CGFloat) -> Color {
assert(shift < 1.0)
var hue : CGFloat = 0
var saturation : CGFloat = 0
var brightness : CGFloat = 0
var alpha : CGFloat = 0
self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
hue += shift
if hue > 1 {
hue -= 1
}
return Color(hue:hue, saturation: saturation, brightness: brightness, alpha: alpha);
}
}
class GradientView : View {
var colors : Array<CGColor> = [Color.randomColour().CGColor,Color.randomColour().CGColor]
private func refreshDisplay() {
#if os(OSX)
self.needsDisplay = true
#else
self.setNeedsDisplay()
#endif
}
private func currentContext() -> CGContextRef {
#if os(OSX)
return NSGraphicsContext.currentContext()!.CGContext
#else
return UIGraphicsGetCurrentContext()
#endif
}
var rotation : CGFloat = 0.0 {
didSet(newRotation) {
refreshDisplay()
}
}
#if os(iOS)
override func willMoveToSuperview(newSuperview: View?) {
super.willMoveToSuperview(newSuperview);
self.changeGradient(false);
}
override func canBecomeFirstResponder() -> Bool {
return true
}
#endif
func changeGradient(animated : Bool) {
var colors = Array<CGColor>()
for _ in 1...2 {
colors.append(Color.randomColour().CGColor)
}
self.colors = colors
self.rotation = GradientView.randomAngle()
refreshDisplay()
}
func colorAtPosition(position : Int) -> Color {
return Color(CGColor: self.colors[position])!
}
func setColor(colour:Color, atPosition position: Int) {
self.colors[position] = colour.CGColor
refreshDisplay()
}
override func drawRect(rect: CGRect) {
let ctx = currentContext()
var startPoint = GradientView.startPointForAngle(self.rotation)
startPoint.x *= CGRectGetWidth(rect);
startPoint.y *= CGRectGetHeight(rect);
var endPoint = GradientView.endPointForAngle(self.rotation)
endPoint.x *= CGRectGetWidth(rect);
endPoint.y *= CGRectGetHeight(rect);
let colorSpace = CGColorSpaceCreateDeviceRGB()
let gradient = CGGradientCreateWithColors(colorSpace, self.colors, [0.0,1.0])
let options : CGGradientDrawingOptions = UInt32(kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation)
CGContextDrawLinearGradient(ctx, gradient, startPoint, endPoint, options)
}
func colourAtPoint(point: CGPoint) -> Color {
#if os(iOS)
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let pixelData = UnsafeMutablePointer<UInt8>.alloc(1)
let ctx = CGBitmapContextCreate(pixelData, 1, 1, 8, 4, CGColorSpaceCreateDeviceRGB(), bitmapInfo)
CGContextTranslateCTM(ctx, -point.x, -point.y)
self.layer.renderInContext(ctx)
let red = CGFloat(pixelData[0])
let green = CGFloat(pixelData[1])
let blue = CGFloat(pixelData[2])
let alpha = CGFloat(pixelData[3])
let color = UIColor(red:CGFloat(red / CGFloat(255)),green:CGFloat(green / CGFloat(255)),blue:CGFloat(blue / CGFloat(255)),alpha:1)
return color
#else
return Color.whiteColor()
#endif
}
class func startPointForAngle(angle : CGFloat) -> CGPoint {
return CGPointMake(0.5 + (sin(angle) / 2.0), 0.5 - cos(angle) / 2.0)
}
class func endPointForAngle(angle : CGFloat) -> CGPoint {
return CGPointMake(0.5 - (sin(angle) / 2.0), 0.5 + cos(angle) / 2.0)
}
class func randomAngle() -> CGFloat {
// between -pi/2 and +pi/2
return CGFloat(arc4random_uniform(1000)) / 1000.0 * CGFloat(M_PI) - CGFloat(M_PI_2)
}
}