Skip to content

Commit

Permalink
Add to Rgba export to array and tuple; Add init from tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyvan committed May 5, 2024
1 parent 31efdcb commit f04aa4c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Sources/geometrize/Rgba.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public struct Rgba {
self.a = a
}

public init(_ tuple: (r: UInt8, g: UInt8, b: UInt8, a: UInt8)) {
self.r = tuple.r
self.g = tuple.g
self.b = tuple.b
self.a = tuple.a
}

public init(_ array: [UInt8]) {
assert(array.count == 4)
self.r = array[0]
Expand All @@ -23,6 +30,14 @@ public struct Rgba {
self.a = array[3]
}

public init(_ array: ContiguousArray<UInt8>) {
assert(array.count == 4)
self.r = array[0]
self.g = array[1]
self.b = array[2]
self.a = array[3]
}

public init(_ slice: ArraySlice<UInt8>) {
assert(slice.count == 4)
self.r = slice[slice.startIndex]
Expand Down Expand Up @@ -74,6 +89,10 @@ public struct Rgba {
)
}

public var asArray: [UInt8] { [r, g, b, a] }

public var asTuple: (UInt8, UInt8, UInt8, UInt8) { (r, g, b, a) }

}

extension Rgba: Equatable {}
12 changes: 12 additions & 0 deletions Tests/geometrizeTests/RgbaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ final class RgbaTests: XCTestCase {
)
}

func testArray() {
let array: [UInt8] = [1, 2, 3, 4]
let rgba = Rgba(array)
XCTAssertEqual(rgba.asArray, array)
}

func testTuple() {
let tuple = (UInt8(1), UInt8(2), UInt8(3), UInt8(4))
let rgba = Rgba(tuple)
XCTAssertTrue(rgba.asTuple == tuple)
}

}

0 comments on commit f04aa4c

Please sign in to comment.