Skip to content

Commit

Permalink
Merge branch 'feature/CoreGraphicsAPI'
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Jun 16, 2017
2 parents d6e7641 + 0815fc6 commit 49edd6e
Show file tree
Hide file tree
Showing 25 changed files with 1,374 additions and 1,461 deletions.
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"repositoryURL": "https://github.com/PureSwift/CLCMS.git",
"state": {
"branch": "master",
"revision": "20d18d62ebc642e52758512cab59af06f2d3655e",
"revision": "4808ad3fa6d244eb5a328a1b3fe0439f5954a831",
"version": null
}
},
Expand All @@ -42,7 +42,7 @@
"repositoryURL": "https://github.com/PureSwift/Cairo.git",
"state": {
"branch": "master",
"revision": "5bf2c328f0437f5d5859bba55beae1472b156a29",
"revision": "9fa92e223d8f596e3c545f320f1235178dec79d2",
"version": null
}
},
Expand All @@ -51,7 +51,7 @@
"repositoryURL": "https://github.com/PureSwift/LittleCMS.git",
"state": {
"branch": "master",
"revision": "32871723c3bd58d1dc3532125bd2c21e5acec5e1",
"revision": "5bfebf596b080f8c7dc5f403f2f0784cef130906",
"version": null
}
}
Expand Down
105 changes: 0 additions & 105 deletions Sources/Silica/AffineTransform.swift

This file was deleted.

114 changes: 110 additions & 4 deletions Sources/Silica/CGAffineTransform.swift
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// Applications that store pixel data in memory using ARGB format must take care in how they read data.
/// If the code is not written correctly, it’s possible to misread the data which leads to colors or alpha that appear wrong.
/// The byte order constants specify the byte ordering of pixel formats.
public struct BitmapInfo {
public struct CGBitmapInfo {

/// The components of a bitmap are floating-point values.
public var floatComponents: Bool
Expand All @@ -33,9 +33,9 @@ public struct BitmapInfo {

// MARK: - Equatable

extension BitmapInfo: Equatable {
extension CGBitmapInfo: Equatable {

public static func == (lhs: BitmapInfo, rhs: BitmapInfo) -> Bool {
public static func == (lhs: CGBitmapInfo, rhs: CGBitmapInfo) -> Bool {

return lhs.floatComponents == rhs.floatComponents
&& lhs.alpha == rhs.alpha
Expand All @@ -45,7 +45,7 @@ extension BitmapInfo: Equatable {

// MARK: - Supporting Types

public extension BitmapInfo {
public extension CGBitmapInfo {

/// Alpha information that specifies whether a bitmap contains an alpha channel
/// and how the alpha channel is generated.
Expand Down
81 changes: 79 additions & 2 deletions Sources/Silica/CGColor.swift
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.
Loading

0 comments on commit 49edd6e

Please sign in to comment.