|
2 | 2 | // SequentialScale.swift |
3 | 3 | // |
4 | 4 |
|
| 5 | +import CoreGraphics |
5 | 6 | import Foundation |
6 | 7 |
|
7 | | -/// A type that maps values from a continuous input _domain_ to an output _range_. |
8 | | -public struct SequentialScale<InputType: ConvertibleWithDouble, OutputType>: Scale, CustomStringConvertible { |
| 8 | +/// A type that maps values from a continuous input _domain_ to a color. |
| 9 | +public struct SequentialScale<InputType: ConvertibleWithDouble>: CustomStringConvertible { |
| 10 | + /// The lower bound of the input domain. |
| 11 | + public let domainLower: InputType |
| 12 | + /// The upper bound of the input domain. |
| 13 | + public let domainHigher: InputType |
| 14 | + /// A Boolean value that indicates if the mapping from domain to range is inverted. |
| 15 | + public let reversed: Bool |
| 16 | + |
| 17 | + let interpolator: ArrayInterpolator |
| 18 | + |
| 19 | + /// Creates a new sequential scale that maps a _domain_ value to a color. |
| 20 | + /// - Parameters: |
| 21 | + /// - lower: The lowest value expected for the scale. |
| 22 | + /// - higher: The highest value expected for the scale. |
| 23 | + /// - reversed: A Boolean value that indicates the color range is reversed. |
| 24 | + /// - interpolator: The interpolator that provides the color range to map. |
| 25 | + public init(lower: InputType = 0, |
| 26 | + higher: InputType = 1, |
| 27 | + reversed: Bool = false, |
| 28 | + interpolator: ArrayInterpolator) |
| 29 | + { |
| 30 | + precondition(lower <= higher, "attempting to set an inverted domain: \(lower) to \(higher)") |
| 31 | + precondition(lower != higher, "attempting to set an empty domain: \(lower) to \(higher)") |
| 32 | + domainLower = lower |
| 33 | + domainHigher = higher |
| 34 | + self.reversed = reversed |
| 35 | + self.interpolator = interpolator |
| 36 | + } |
| 37 | + |
| 38 | + // MARK: - description |
| 39 | + |
| 40 | + public var description: String { |
| 41 | + "sequential[\(domainLower):\(domainHigher)]->\(type(of: interpolator)))" |
| 42 | + } |
| 43 | + |
| 44 | + // MARK: - scaling |
| 45 | + |
9 | 46 | /// Converts a value comparing it to the input domain, transforming the value, and mapping it between the range values you provide. |
10 | 47 | /// |
11 | 48 | /// - Parameter inputValue: The value to be scaled. |
12 | 49 | /// - Returns: A value within the bounds of the range values you provide, or `nil` if the value was dropped. |
13 | | - public func scale(_: InputType) -> OutputType? { |
14 | | - nil |
| 50 | + public func scale(_ x: InputType) -> CGColor { |
| 51 | + let clampedX: InputType |
| 52 | + if x < domainLower { |
| 53 | + clampedX = domainLower |
| 54 | + } else if x > domainHigher { |
| 55 | + clampedX = domainHigher |
| 56 | + } else { |
| 57 | + clampedX = x |
| 58 | + } |
| 59 | + let t = normalize(clampedX.toDouble(), lower: domainLower.toDouble(), higher: domainHigher.toDouble()) |
| 60 | + return interpolator.interpolate(t) |
15 | 61 | } |
16 | 62 |
|
17 | 63 | // MARK: - modifier functions |
18 | 64 |
|
19 | | - /// Returns a new scale with the domain set to the span of values you provide. |
| 65 | + /// Returns a new scale with the interpolator set to the instance you provide. |
| 66 | + /// - Parameter interpolator: An interpolator instance. |
| 67 | + /// - Returns: A copy of the scale with the domain values you provide. |
| 68 | + public func interpolator(_ interpolator: ArrayInterpolator) -> Self { |
| 69 | + Self(lower: domainLower, higher: domainHigher, reversed: reversed, interpolator: interpolator) |
| 70 | + } |
| 71 | + |
| 72 | + /// Returns a new scale with the domain set to the values you provide. |
| 73 | + /// - Parameters: |
| 74 | + /// - lower: The lower bound for the scale's domain. |
| 75 | + /// - higher: The upper bound for the scale's domain. |
| 76 | + /// - Returns: A copy of the scale with the domain values you provide. |
| 77 | + public func domain(lower: InputType, higher: InputType) -> Self { |
| 78 | + Self(lower: lower, higher: higher, reversed: reversed, interpolator: interpolator) |
| 79 | + } |
| 80 | + |
| 81 | + /// Returns a new scale with the domain set to the values you provide. |
| 82 | + /// - Parameters: |
| 83 | + /// - range: The range to apply as the scale's domain |
| 84 | + /// - Returns: A copy of the scale with the domain values you provide. |
| 85 | + public func domain(_ range: ClosedRange<InputType>) -> Self { |
| 86 | + Self(lower: range.lowerBound, higher: range.upperBound, reversed: reversed, interpolator: interpolator) |
| 87 | + } |
| 88 | + |
| 89 | + /// Returns a new scale with the domain set to span the values you provide. |
20 | 90 | /// - Parameter values: An array of input values. |
21 | | - public func domain(_: [InputType]) -> Self { |
22 | | - self |
| 91 | + public func domain(_ values: [InputType]) -> Self { |
| 92 | + domain(values, nice: true) |
23 | 93 | } |
24 | 94 |
|
25 | | - public var description: String { |
26 | | - "A SEQUENTIAL SCALE" |
| 95 | + /// Returns a new scale with the domain inferred from the list of values you provide. |
| 96 | + /// - Parameters: |
| 97 | + /// - values: The list of values to use to determine the scale's domain. |
| 98 | + /// - nice: A Boolean value that indicates whether to expand the domain to visually nice values. |
| 99 | + /// - Returns: A copy of the scale with the domain values you provide. |
| 100 | + public func domain(_ values: [InputType], nice: Bool) -> Self { |
| 101 | + guard let min = values.min(), let max = values.max() else { |
| 102 | + return self |
| 103 | + } |
| 104 | + if values.count == 1 || min == max { |
| 105 | + if nice { |
| 106 | + let bottom: Double = 0 |
| 107 | + let top = Double.niceVersion(for: max.toDouble(), trendTowardsZero: false) |
| 108 | + return domain(lower: InputType.fromDouble(bottom), higher: InputType.fromDouble(top)) |
| 109 | + } else { |
| 110 | + return domain(lower: 0, higher: max) |
| 111 | + } |
| 112 | + } else { |
| 113 | + if nice { |
| 114 | + let bottom = Double.niceMinimumValueForRange(min: min.toDouble(), max: max.toDouble()) |
| 115 | + let top = Double.niceVersion(for: max.toDouble(), trendTowardsZero: false) |
| 116 | + return domain(lower: InputType.fromDouble(bottom), higher: InputType.fromDouble(top)) |
| 117 | + } else { |
| 118 | + return domain(lower: min, higher: max) |
| 119 | + } |
| 120 | + } |
27 | 121 | } |
28 | 122 | } |
0 commit comments