Skip to content

Commit 0be8104

Browse files
committed
swift-format files created in this PR
1 parent 34db69e commit 0be8104

File tree

12 files changed

+2212
-2167
lines changed

12 files changed

+2212
-2167
lines changed

Sources/SwiftPlot/Color/Color.swift

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public struct Color {
33
public var g: Float
44
public var b: Float
55
public var a: Float
6-
public init(_ r: Float, _ g: Float, _ b: Float, _ a: Float){
6+
public init(_ r: Float, _ g: Float, _ b: Float, _ a: Float) {
77
self.r = r
88
self.g = g
99
self.b = b
@@ -31,31 +31,32 @@ public struct Color {
3131
}
3232

3333
extension Color {
34-
34+
3535
/// Returns a `Color` whose RBGA components are generated by the given `RandomNumberGenerator`.
3636
///
3737
public static func random<RNG: RandomNumberGenerator>(using generator: inout RNG) -> Color {
38-
return Color(.random(in: 0...1.0, using: &generator),
39-
.random(in: 0...1.0, using: &generator),
40-
.random(in: 0...1.0, using: &generator),
41-
.random(in: 0...1.0, using: &generator))
38+
return Color(
39+
.random(in: 0...1.0, using: &generator),
40+
.random(in: 0...1.0, using: &generator),
41+
.random(in: 0...1.0, using: &generator),
42+
.random(in: 0...1.0, using: &generator))
4243
}
43-
44+
4445
/// Returns a `Color` whose RBGA components are generated by the system's default `RandomNumberGenerator`.
4546
///
4647
public static func random() -> Color {
4748
var generator = SystemRandomNumberGenerator()
4849
return Color.random(using: &generator)
4950
}
50-
51+
5152
/// Returns a `Color` whose RGB components are given by this color, and whose alpha component is `alpha`.
5253
///
5354
public func withAlpha(_ alpha: Float) -> Color {
5455
var color = self
5556
color.a = alpha
5657
return color
5758
}
58-
59+
5960
/// Returns a `Color` whose components are a distance `offset` between this and another color.
6061
///
6162
/// - parameters:
@@ -64,34 +65,36 @@ extension Color {
6465
/// A value of 0 always returns this color, and a value of 1 always returns `other`.
6566
///
6667
public func linearBlend(with other: Color, offset: Float) -> Color {
67-
return Color((other.r - r) * offset + r,
68-
(other.g - g) * offset + g,
69-
(other.b - b) * offset + b,
70-
(other.a - a) * offset + a)
68+
return Color(
69+
(other.r - r) * offset + r,
70+
(other.g - g) * offset + g,
71+
(other.b - b) * offset + b,
72+
(other.a - a) * offset + a)
7173
}
7274
}
7375

7476
#if canImport(CoreGraphics)
75-
import CoreGraphics
77+
import CoreGraphics
7678

77-
fileprivate let RGBColorSpace = CGColorSpaceCreateDeviceRGB()
79+
fileprivate let RGBColorSpace = CGColorSpaceCreateDeviceRGB()
7880

79-
public extension Color {
80-
@available(tvOS 13.0, watchOS 6.0, *)
81-
var cgColor : CGColor {
82-
if #available(OSX 10.15, iOS 13.0, *) {
83-
return CGColor(srgbRed: CGFloat(r),
84-
green: CGFloat(g),
85-
blue: CGFloat(b),
86-
alpha: CGFloat(a))
87-
} else {
88-
var tuple = (CGFloat(r), CGFloat(g), CGFloat(b), CGFloat(a))
89-
return withUnsafePointer(to: &tuple) { tupPtr in
90-
return tupPtr.withMemoryRebound(to: CGFloat.self, capacity: 4) { floatPtr in
91-
return CGColor(colorSpace: RGBColorSpace, components:floatPtr)!
81+
public extension Color {
82+
@available(tvOS 13.0, watchOS 6.0, *)
83+
var cgColor: CGColor {
84+
if #available(OSX 10.15, iOS 13.0, *) {
85+
return CGColor(
86+
srgbRed: CGFloat(r),
87+
green: CGFloat(g),
88+
blue: CGFloat(b),
89+
alpha: CGFloat(a))
90+
} else {
91+
var tuple = (CGFloat(r), CGFloat(g), CGFloat(b), CGFloat(a))
92+
return withUnsafePointer(to: &tuple) { tupPtr in
93+
return tupPtr.withMemoryRebound(to: CGFloat.self, capacity: 4) { floatPtr in
94+
return CGColor(colorSpace: RGBColorSpace, components: floatPtr)!
95+
}
9296
}
9397
}
9498
}
9599
}
96-
}
97100
#endif

Sources/SwiftPlot/Color/ColorMap.swift

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/// An object which maps a value in the range `0...1` to a `Color`.
32
///
43
/// This object wraps a conformer to `ColorMapProtocol`, and is used instead of `ColorMapProtocol` directly
@@ -8,11 +7,10 @@
87
///
98
public struct ColorMap: ColorMapProtocol {
109
var base: ColorMapProtocol
11-
10+
1211
public init(_ base: ColorMapProtocol) {
1312
// If base is already wrapped, do not re-wrap it.
14-
if let existing = base as? ColorMap { self = existing }
15-
else { self.base = base }
13+
if let existing = base as? ColorMap { self = existing } else { self.base = base }
1614
}
1715
public func colorForOffset(_ offset: Double) -> Color {
1816
let offset = min(max(offset, 0), 1)
@@ -35,8 +33,8 @@ extension ColorMapProtocol {
3533

3634
private struct ColorTransformer: ColorMapProtocol {
3735
var base: ColorMapProtocol
38-
var transform: (Color)->Color
39-
init(_ base: ColorMapProtocol, transform: @escaping (Color)->Color) {
36+
var transform: (Color) -> Color
37+
init(_ base: ColorMapProtocol, transform: @escaping (Color) -> Color) {
4038
self.base = base; self.transform = transform
4139
}
4240
func colorForOffset(_ offset: Double) -> Color {
@@ -45,13 +43,13 @@ private struct ColorTransformer: ColorMapProtocol {
4543
}
4644

4745
extension ColorMap {
48-
46+
4947
/// Returns a `ColorMap` whose output is transformed by the given closure.
5048
///
51-
public func withTransform(_ transform: @escaping (Color)->Color) -> ColorMap {
49+
public func withTransform(_ transform: @escaping (Color) -> Color) -> ColorMap {
5250
return ColorMap(ColorTransformer(base, transform: transform))
5351
}
54-
52+
5553
/// Returns a `ColorMap` whose output colors' alpha components are given by `alpha`.
5654
///
5755
public func withAlpha(_ alpha: Float) -> ColorMap {
@@ -63,7 +61,7 @@ extension ColorMap {
6361
public func lightened(by amount: Float) -> ColorMap {
6462
return withTransform { $0.linearBlend(with: .white, offset: amount) }
6563
}
66-
64+
6765
/// Returns a `ColorMap` whose output colors are darkened by the given `amount`.
6866
///
6967
public func darkened(by amount: Float) -> ColorMap {
@@ -75,8 +73,8 @@ extension ColorMap {
7573

7674
private struct ColorMapOffsetTransformer: ColorMapProtocol {
7775
var base: ColorMapProtocol
78-
var transform: (Double)->Double
79-
init(_ base: ColorMapProtocol, transform: @escaping (Double)->Double) {
76+
var transform: (Double) -> Double
77+
init(_ base: ColorMapProtocol, transform: @escaping (Double) -> Double) {
8078
self.base = base; self.transform = transform
8179
}
8280
func colorForOffset(_ offset: Double) -> Color {
@@ -88,11 +86,11 @@ private struct ColorMapOffsetTransformer: ColorMapProtocol {
8886
}
8987

9088
extension ColorMap {
91-
89+
9290
private func withOffsetTransform(_ transform: @escaping (Double) -> Double) -> ColorMap {
9391
return ColorMap(ColorMapOffsetTransformer(base, transform: transform))
9492
}
95-
93+
9694
/// Returns a `ColorMap` whose output at offset `x` is equal to this `ColorMap`'s output at `1 - x`.
9795
///
9896
public func reversed() -> ColorMap {
@@ -110,11 +108,11 @@ private struct SingleColorMap: ColorMapProtocol {
110108
}
111109

112110
extension ColorMap {
113-
111+
114112
/// Returns a `ColorMap` which always returns the same color.
115113
///
116114
public static func color(_ color: Color) -> ColorMap {
117-
return ColorMap(SingleColorMap(color: color))
115+
return ColorMap(SingleColorMap(color: color))
118116
}
119117
}
120118

@@ -132,7 +130,7 @@ public struct GradientStop {
132130

133131
private struct LinearGradient: ColorMapProtocol {
134132
var stops: [GradientStop]
135-
133+
136134
init(stops: [GradientStop]) {
137135
self.stops = stops.sorted { $0.position < $1.position }
138136
}
@@ -147,29 +145,29 @@ private struct LinearGradient: ColorMapProtocol {
147145
guard rightStopIdx > stops.startIndex else { return rightStop.color }
148146
let leftStop = stops[stops.index(before: rightStopIdx)]
149147
assert(leftStop.position <= offset)
150-
148+
151149
let distance = rightStop.position - leftStop.position
152150
guard distance > 0 else { return rightStop.color }
153-
151+
154152
let offset = (offset - leftStop.position) / distance
155153
return leftStop.color.linearBlend(with: rightStop.color, offset: Float(offset))
156154
}
157155
}
158156

159157
extension ColorMap {
160-
158+
161159
/// Returns a `ColorMap` whose output is a linear gradient with the given stops.
162160
///
163161
public static func linearGradient(_ stops: [GradientStop]) -> ColorMap {
164162
return ColorMap(LinearGradient(stops: stops))
165163
}
166-
164+
167165
/// Returns a `ColorMap` whose output is a linear gradient between the given colors.
168166
///
169167
public static func linearGradient(_ start: Color, _ end: Color) -> ColorMap {
170168
return ColorMap(LinearGradient(start: start, end: end))
171169
}
172-
170+
173171
/// A standard, five-color heat map.
174172
///
175173
public static let fiveColorHeatMap = ColorMap.linearGradient([
@@ -192,4 +190,3 @@ extension ColorMap {
192190
GradientStop(Color(1, 1, 1, 1), at: 1)
193191
])
194192
}
195-

0 commit comments

Comments
 (0)