-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/CoreGraphicsAPI'
- Loading branch information
Showing
25 changed files
with
1,374 additions
and
1,461 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,121 @@ | ||
// | ||
// CGAffineTransform.swift | ||
// AffineTransform.swift | ||
// Silica | ||
// | ||
// Created by Alsey Coleman Miller on 5/9/16. | ||
// Created by Alsey Coleman Miller on 5/8/16. | ||
// Copyright © 2016 PureSwift. All rights reserved. | ||
// | ||
|
||
public extension CGAffineTransform { | ||
import Cairo | ||
import CCairo | ||
import struct Foundation.CGFloat | ||
import struct Foundation.CGPoint | ||
import struct Foundation.CGSize | ||
import struct Foundation.CGRect | ||
|
||
/// Affine Transform | ||
public struct CGAffineTransform { | ||
|
||
// MARK: - Properties | ||
|
||
public var a, b, c, d: CGFloat | ||
|
||
public var t: (x: CGFloat, y: CGFloat) | ||
|
||
// MARK: - Initialization | ||
|
||
public init(a: CGFloat, b: CGFloat, c: CGFloat, d: CGFloat, t: (x: CGFloat, y: CGFloat)) { | ||
|
||
self.a = a | ||
self.b = b | ||
self.c = c | ||
self.d = d | ||
self.t = t | ||
} | ||
|
||
init(a: CGFloat, b: CGFloat, c: CGFloat, d: CGFloat, tx: CGFloat, ty: CGFloat) { | ||
|
||
self.init(a: a, b: b, c: c, d: d, t: (tx, ty)) | ||
} | ||
} | ||
|
||
public static let identity = CGAffineTransform(a: 1, b: 0, c: 0, d: 1, t: (x: 0, y: 0)) | ||
} | ||
|
||
// MARK: - Geometry Math | ||
|
||
// Immutable math | ||
|
||
public protocol CGAffineTransformMath { | ||
|
||
func applying(_ transform: CGAffineTransform) -> Self | ||
} | ||
|
||
// Mutable versions | ||
|
||
public extension CGAffineTransformMath { | ||
|
||
@inline(__always) | ||
mutating func apply(_ transform: CGAffineTransform) { | ||
|
||
self = self.applying(transform) | ||
} | ||
} | ||
|
||
// Implementations | ||
|
||
extension CGPoint: CGAffineTransformMath { | ||
|
||
@inline(__always) | ||
public func applying(_ t: CGAffineTransform) -> CGPoint { | ||
|
||
return CGPoint(x: t.a * x + t.c * y + t.t.x, | ||
y: t.b * x + t.d * y + t.t.y) | ||
} | ||
} | ||
|
||
extension CGSize: CGAffineTransformMath { | ||
|
||
@inline(__always) | ||
public func applying( _ transform: CGAffineTransform) -> CGSize { | ||
|
||
var newSize = CGSize(width: transform.a * width + transform.c * height, | ||
height: transform.b * width + transform.d * height) | ||
|
||
if newSize.width < 0 { newSize.width = -newSize.width } | ||
if newSize.height < 0 { newSize.height = -newSize.height } | ||
|
||
return newSize | ||
} | ||
} | ||
|
||
// MARK: - Cairo Conversion | ||
|
||
extension CGAffineTransform: CairoConvertible { | ||
|
||
public typealias CairoType = Cairo.Matrix | ||
|
||
@inline(__always) | ||
public init(cairo matrix: CairoType) { | ||
|
||
self.init(a: CGFloat(matrix.xx), | ||
b: CGFloat(matrix.xy), | ||
c: CGFloat(matrix.yx), | ||
d: CGFloat(matrix.yy), | ||
t: (x: CGFloat(matrix.x0), y: CGFloat(matrix.y0))) | ||
} | ||
|
||
@inline(__always) | ||
public func toCairo() -> CairoType { | ||
|
||
var matrix = Matrix() | ||
|
||
matrix.xx = Double(a) | ||
matrix.xy = Double(b) | ||
matrix.yx = Double(c) | ||
matrix.yy = Double(d) | ||
matrix.x0 = Double(t.x) | ||
matrix.y0 = Double(t.y) | ||
|
||
return matrix | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,89 @@ | ||
// | ||
// CGColor.swift | ||
// Color.swift | ||
// Silica | ||
// | ||
// Created by Alsey Coleman Miller on 5/9/16. | ||
// Copyright © 2016 PureSwift. All rights reserved. | ||
// | ||
|
||
import struct Foundation.CGFloat | ||
import Cairo | ||
|
||
public struct CGColor: Equatable { | ||
|
||
// MARK: - Properties | ||
|
||
public var red: CGFloat | ||
|
||
public var green: CGFloat | ||
|
||
public var blue: CGFloat | ||
|
||
public var alpha: CGFloat | ||
|
||
// MARK: - Initialization | ||
|
||
public init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat = 1.0) { | ||
|
||
self.red = red | ||
self.green = green | ||
self.blue = blue | ||
self.alpha = alpha | ||
} | ||
|
||
public init(grey: CGFloat, alpha: CGFloat = 1.0) { | ||
|
||
self.red = grey | ||
self.green = grey | ||
self.blue = grey | ||
self.alpha = alpha | ||
} | ||
|
||
// MARK: - Singletons | ||
|
||
public static let clear = CGColor(red: 0, green: 0, blue: 0, alpha: 0) | ||
|
||
public static let black = CGColor(red: 0, green: 0, blue: 0) | ||
|
||
public static let white = CGColor(red: 1, green: 1, blue: 1) | ||
|
||
public static let red = CGColor(red: 1, green: 0, blue: 0) | ||
|
||
public static let green = CGColor(red: 0, green: 1, blue: 0) | ||
|
||
public static let blue = CGColor(red: 0, green: 0, blue: 1) | ||
} | ||
|
||
// MARK: - Equatable | ||
|
||
public func == (lhs: CGColor, rhs: CGColor) -> Bool { | ||
|
||
return lhs.red == rhs.red | ||
&& lhs.green == rhs.green | ||
&& lhs.blue == rhs.blue | ||
&& lhs.alpha == rhs.alpha | ||
} | ||
|
||
// MARK: - Internal Cairo Conversion | ||
|
||
internal extension Cairo.Pattern { | ||
|
||
convenience init(color: CGColor) { | ||
|
||
self.init(color: (Double(color.red), | ||
Double(color.green), | ||
Double(color.blue), | ||
Double(color.alpha))) | ||
|
||
assert(status.rawValue == 0, "Error creating Cairo.Pattern from Silica.Color: \(status)") | ||
} | ||
} | ||
|
||
// MARK: - CoreGraphics API | ||
|
||
public func CGColorCreateGenericGray(_ grey: CGFloat, _ alpha: CGFloat) -> CGColor { | ||
|
||
return CGColor(grey: grey, alpha: alpha) | ||
} | ||
} | ||
|
||
|
File renamed without changes.
File renamed without changes.
Oops, something went wrong.