Skip to content

Commit c624212

Browse files
committed
updating across the board for swift 5.8 layout, and annotated access to CG system components
1 parent 2395a82 commit c624212

29 files changed

+245
-214
lines changed

Package.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import Foundation
55
import PackageDescription
66

7-
87
let package = Package(
98
name: "SwiftVizScale",
109
platforms: [
@@ -33,7 +32,7 @@ let package = Package(
3332
.product(name: "Collections", package: "swift-collections"),
3433
],
3534
swiftSettings: [
36-
.enableExperimentalFeature("StrictConcurrency")
35+
.enableExperimentalFeature("StrictConcurrency"),
3736
]
3837
),
3938
.target(

Sources/SwiftVizScale/BandScale.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public struct BandScale<CategoryType: Comparable, OutputType: BinaryFloatingPoin
6060
self.paddingOuter = paddingOuter
6161
self.domain = domain
6262
self.reversed = reversed
63-
if let from = from, let to = to {
63+
if let from, let to {
6464
precondition(from < to, "attempting to set an inverted or empty range: \(from) to \(to)")
6565
rangeLower = from
6666
rangeHigher = to
@@ -71,7 +71,7 @@ public struct BandScale<CategoryType: Comparable, OutputType: BinaryFloatingPoin
7171
}
7272

7373
// testing function only - verifies the scale is fully configured
74-
internal func fullyConfigured() -> Bool {
74+
func fullyConfigured() -> Bool {
7575
rangeLower != nil && rangeHigher != nil && domain.count > 0
7676
}
7777

@@ -139,7 +139,7 @@ public struct BandScale<CategoryType: Comparable, OutputType: BinaryFloatingPoin
139139

140140
// attributes of the scale when fully configured
141141

142-
internal func width() -> Double? {
142+
func width() -> Double? {
143143
guard let from = rangeLower, let to = rangeHigher else {
144144
return nil
145145
}
@@ -163,7 +163,7 @@ public struct BandScale<CategoryType: Comparable, OutputType: BinaryFloatingPoin
163163
}
164164
}
165165

166-
internal func step() -> Double? {
166+
func step() -> Double? {
167167
guard let width = width() else {
168168
return nil
169169
}
@@ -186,11 +186,10 @@ public struct BandScale<CategoryType: Comparable, OutputType: BinaryFloatingPoin
186186
// the value to be scaled isn't within the domain
187187
return nil
188188
}
189-
let doublePosition: Double
190-
if reversed {
191-
doublePosition = Double(domain.reversed().firstIndex(of: value)!)
189+
let doublePosition = if reversed {
190+
Double(domain.reversed().firstIndex(of: value)!)
192191
} else {
193-
doublePosition = Double(domain.firstIndex(of: value)!)
192+
Double(domain.firstIndex(of: value)!)
194193
}
195194
let startLocation = Double(paddingOuter) + (doublePosition * step)
196195
let stopLocation = startLocation + width
@@ -243,11 +242,10 @@ public struct BandScale<CategoryType: Comparable, OutputType: BinaryFloatingPoin
243242
}
244243
// calculate the closest index
245244
let rangeExtentWithoutOuterPadding = Double(upperRange) - Double(lowerRange) - 2 * Double(paddingOuter)
246-
let indexedRangeValue: Double
247-
if reversed {
248-
indexedRangeValue = (Double(upperRange) - Double(paddingOuter) - Double(location)) / rangeExtentWithoutOuterPadding
245+
let indexedRangeValue: Double = if reversed {
246+
(Double(upperRange) - Double(paddingOuter) - Double(location)) / rangeExtentWithoutOuterPadding
249247
} else {
250-
indexedRangeValue = (Double(location) - Double(paddingOuter)) / rangeExtentWithoutOuterPadding
248+
(Double(location) - Double(paddingOuter)) / rangeExtentWithoutOuterPadding
251249
}
252250
let rangeValueExpandedToCountDomain = indexedRangeValue * Double(domain.count - 1)
253251

@@ -307,10 +305,10 @@ public extension BandScale {
307305
/// - Parameter formatter: An optional formatter to convert the domain values into strings.
308306
func defaultTickValues(formatter: Formatter? = nil) -> [String] {
309307
domain.map { value in
310-
if let formatter = formatter {
311-
return formatter.string(for: value) ?? ""
308+
if let formatter {
309+
formatter.string(for: value) ?? ""
312310
} else {
313-
return String("\(value)")
311+
String("\(value)")
314312
}
315313
}
316314
}

Sources/SwiftVizScale/Color/CGColor+hex.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
// MARK: - From CGColor to String
8484

8585
func toHex(alpha: Bool = false) -> String? {
86-
guard let components = components, components.count >= 3 else {
86+
guard let components, components.count >= 3 else {
8787
return nil
8888
}
8989

Sources/SwiftVizScale/Color/LCH.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
// Color interpolation discussion:
3333
// https://www.alanzucconi.com/2016/01/06/colour-interpolation/
3434

35+
@MainActor
3536
static var lab = CGColorSpace(name: CGColorSpace.genericLab)!
3637

3738
/// Creates a Core Graphics color instance from individual components in the LCH color space.
3839
/// - Parameter components: A list of four components in the order: Luminance, Chroma, Hue, and Alpha.
40+
@MainActor
3941
public static func color(from components: [CGFloat]) -> CGColor {
4042
precondition(components.count == 4)
4143
var newComponents = components
@@ -56,6 +58,7 @@
5658
/// Returns a list of four components from a Core Graphics color instance, mapped into the LCH color space.
5759
///
5860
/// The components, in order, are Luminance, Chroma, Hue, and Alpha.
61+
@MainActor
5962
public static func components(from color: CGColor) -> [CGFloat] {
6063
// from https://mina86.com/2021/srgb-lab-lchab-conversions/
6164
// converting L,a*,b* to L,C,Hab (polar coordinate LAB color space)
@@ -95,6 +98,7 @@
9598
/// - color2: The second color.
9699
/// - t: A unit value between 0 and 1 representing the position between the first and second colors to return.
97100
/// - Returns: A color interpolated between the two colors you provide.
101+
@MainActor
98102
public static func interpolate(_ color1: CGColor, _ color2: CGColor, t: CGFloat) -> CGColor {
99103
precondition(t >= 0 && t <= 1)
100104
let components1 = LCH.components(from: color1)
@@ -109,22 +113,21 @@
109113
// by +/- 2*PI to get a shorter distance.
110114
let hue1 = components1[2]
111115
let hue2 = components2[2]
112-
let targetForInterpolation: CGFloat
113-
if hue1 > hue2 {
116+
let targetForInterpolation: CGFloat = if hue1 > hue2 {
114117
// If the value for hue2 is greater than hue1, add 2*PI to hue2 and compare, choosing
115118
// the shortest path for the target to which to interpolate.
116119
if abs(hue1 - hue2) > abs(hue1 - (hue2 + 2 * Double.pi)) {
117-
targetForInterpolation = hue2 + 2 * Double.pi
120+
hue2 + 2 * Double.pi
118121
} else {
119-
targetForInterpolation = hue2
122+
hue2
120123
}
121124
} else {
122125
// If the value for hue2 is less than hue1, subtract 2*PI to hue2 and compare, choosing
123126
// the shortest path for the target to which to interpolate.
124127
if abs(hue1 - hue2) > abs(hue1 - (hue2 - 2 * Double.pi)) {
125-
targetForInterpolation = hue2 + 2 * Double.pi
128+
hue2 + 2 * Double.pi
126129
} else {
127-
targetForInterpolation = hue2
130+
hue2
128131
}
129132
}
130133

0 commit comments

Comments
 (0)