Skip to content

Commit

Permalink
Added support for custom precision
Browse files Browse the repository at this point in the history
  • Loading branch information
guidedways committed Jan 14, 2022
1 parent 6926e79 commit af18962
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions Sources/SmoothGradient/SmoothGradientGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ public enum SmoothGradientInterpolation {
}

/// Define the number of intermediate colors to generate.
public enum SmoothGradientPrecision: Int {
case low = 1
case lowMedium = 3
case medium = 5
case mediumHigh = 7
case high = 9
public enum SmoothGradientPrecision {
case low
case lowMedium
case medium
case mediumHigh
case high
case custom(Int)

func precisionToCount() -> Int {
switch self {
case .low: return 1
case .lowMedium: return 3
case .medium: return 5
case .mediumHigh: return 7
case .high: return 9
case .custom(let value): return value
}
}
}

protocol RGBColorConvertible {
Expand All @@ -35,7 +47,7 @@ public struct SmoothGradientGenerator {
interpolation: SmoothGradientInterpolation = .hcl,
precision: SmoothGradientPrecision = .medium
) -> [RGBColor] {
let count = precision.rawValue
let count = precision.precisionToCount()
return interpolate(from: from, to: to, count: count, interpolation: interpolation)
}

Expand Down Expand Up @@ -68,7 +80,7 @@ public struct SmoothGradientGenerator {
) -> [LCHColor] {
switch interpolation {
case .hcl:
let count = precision.rawValue
let count = precision.precisionToCount()
return interpolate(from: from, to: to, count: count)
default:
return generateAsRGBColor(
Expand All @@ -93,7 +105,7 @@ public struct SmoothGradientGenerator {
) -> [HSLColor] {
switch interpolation {
case .hsl:
let count = precision.rawValue
let count = precision.precisionToCount()
return interpolate(from: from, to: to, count: count)
default:
return generateAsRGBColor(
Expand All @@ -118,7 +130,7 @@ public struct SmoothGradientGenerator {
) -> [HSBColor] {
switch interpolation {
case .hsb:
let count = precision.rawValue
let count = precision.precisionToCount()
return interpolate(from: from, to: to, count: count)
default:
return generateAsRGBColor(
Expand Down

0 comments on commit af18962

Please sign in to comment.