Skip to content

Commit 538b688

Browse files
committed
- Revert unintentional change to QuartzRenderer (rebase error? idk)
- Fix quadratic behaviour in SVGRenderer - Add documentation to new members in Color (and add a generic RandomNumberGenerator version).
1 parent be2d516 commit 538b688

File tree

3 files changed

+39
-28
lines changed

3 files changed

+39
-28
lines changed

Sources/QuartzRenderer/QuartzRenderer.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public class QuartzRenderer: Renderer {
4343
height: Int(imageSize.height))
4444
context.setFillColor(Color.white.cgColor)
4545
context.fill(rect)
46-
self.context.setShouldSmoothFonts(false)
47-
// context.setShouldAntialias(false)
4846
}
4947
}
5048

@@ -66,8 +64,6 @@ public class QuartzRenderer: Renderer {
6664
self.context.setAllowsFontSmoothing(true)
6765
self.context.setShouldSmoothFonts(fontSmoothing)
6866
self.isExternalContext = false
69-
self.context.setShouldSmoothFonts(false)
70-
// context.setShouldAntialias(false)
7167
}
7268

7369
/// Creates a renderer with the given external context and dimensions..

Sources/SVGRenderer/SVGRenderer.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,11 @@ public class SVGRenderer: Renderer{
220220
}
221221

222222
public func getTextLayoutSize(text: String, textSize size: Float) -> Size {
223-
var width: Float = 0
224-
let scaleFactor = size/100.0
225-
226-
for i in 0..<text.count {
227-
let index = text.index(text.startIndex, offsetBy: i)
228-
guard let asciiVal = text[index].ascii else { continue }
229-
width = width + Float(Self.LCARS_CHAR_SIZE_ARRAY[Int(asciiVal)])
223+
let width = text.reduce(into: Float(0)) { width, character in
224+
guard let asciiVal = character.ascii else { return }
225+
width += Float(Self.LCARS_CHAR_SIZE_ARRAY[Int(asciiVal)])
230226
}
231-
227+
let scaleFactor = size/100.0
232228
return Size(width: width*scaleFactor + 25, height: size)
233229
}
234230

Sources/SwiftPlot/Color.swift

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,43 @@ public struct Color {
3131
}
3232

3333
extension Color {
34-
35-
public func withAlpha(_ alpha: Float) -> Color {
36-
var color = self
37-
color.a = alpha
38-
return color
39-
}
40-
41-
public func linearBlend(with other: Color, offset: Float) -> Color {
42-
return Color(
43-
(other.r - r) * offset + r,
44-
(other.g - g) * offset + g,
45-
(other.b - b) * offset + b,
46-
(other.a - a) * offset + a)
47-
}
4834

35+
/// Returns a `Color` whose RBGA components are generated by given `RandomNumberGenerator`.
36+
///
37+
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))
42+
}
43+
44+
/// Returns a `Color` whose RBGA components are generated by the system's default `RandomNumberGenerator`.
45+
///
4946
public static func random() -> Color {
50-
return Color(.random(in: 0...1.0), .random(in: 0...1.0),
51-
.random(in: 0...1.0), .random(in: 0...1.0))
47+
var generator = SystemRandomNumberGenerator()
48+
return Color.random(using: &generator)
49+
}
50+
51+
/// Returns a `Color` whose RGB components are given by this color, and whose alpha component is `alpha`.
52+
///
53+
public func withAlpha(_ alpha: Float) -> Color {
54+
var color = self
55+
color.a = alpha
56+
return color
57+
}
58+
59+
/// Returns a `Color` whose components are a distance `offset` between this and another color.
60+
///
61+
/// - parameters:
62+
/// - other: The color to blend with.
63+
/// - offset: The fractional distance between this color and `other`, between 0 and 1.
64+
/// A value of 0 always returns this color, and a value of 1 always returns `other`.
65+
///
66+
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)
5271
}
5372
}
5473

0 commit comments

Comments
 (0)