Skip to content

Commit

Permalink
Fix formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
rismay-luassat committed Jun 25, 2024
1 parent 4da5a6f commit 8b2595d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 86 deletions.
16 changes: 9 additions & 7 deletions Sources/WrkstrmColor/GradientDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ extension Gradient {
switch delta.type {
case .static:
delta.range.lowerBound

case .increasing:
delta.range.lowerBound + delta.magnitude * ratio

case .decreasing:
delta.range.upperBound - delta.magnitude * ratio
}
Expand Down Expand Up @@ -103,13 +105,13 @@ extension Gradient {
// Calculate the middle values based on the delta types and magnitudes.
let a =
(aRange.type == .decreasing ? aRange.range.upperBound : aRange.range.lowerBound) + aRange
.magnitude / 2
.magnitude / 2
let b =
(bRange.type == .decreasing ? bRange.range.upperBound : bRange.range.lowerBound) + bRange
.magnitude / 2
.magnitude / 2
let c =
(cRange.type == .decreasing ? cRange.range.upperBound : cRange.range.lowerBound) + cRange
.magnitude / 2
.magnitude / 2

return S.scaled(newComponents: (a, b, c))
}
Expand All @@ -133,8 +135,8 @@ public struct GradientDescriptor<S: Scalable>: Gradient {
count: Int,
aRange: Delta<S.Value>,
bRange: Delta<S.Value>,
cRange: Delta<S.Value>)
{
cRange: Delta<S.Value>
) {
self.count = count
self.aRange = aRange
self.bRange = bRange
Expand All @@ -151,8 +153,8 @@ public struct ContrastGradientDescriptor<S: Scalable & RGBEncodable>: Gradient {
minContrast: S.Value,
aRange: Delta<S.Value>,
bRange: Delta<S.Value>,
cRange: Delta<S.Value>)
{
cRange: Delta<S.Value>
) {
self.minContrast = minContrast
self.aRange = aRange
self.bRange = bRange
Expand Down
18 changes: 11 additions & 7 deletions Sources/WrkstrmColor/Math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ typealias MType<Value: BinaryFloatingPoint> =
(
R: Components<Value>,
G: Components<Value>,
B: Components<Value>)
B: Components<Value>
)

typealias MInvType<Value: BinaryFloatingPoint> =
(
X: Components<Value>,
Y: Components<Value>,
Z: Components<Value>)
Z: Components<Value>
)

/// Generic Vector type.
typealias Vector<Value: BinaryFloatingPoint> = (Value, Value)
Expand All @@ -36,15 +38,17 @@ enum Constant {
(
R: Components(3.240_969_941_904_521_4, -1.537_383_177_570_093_5, -0.498_610_760_293_003_28),
G: Components(-0.969_243_636_280_879_83, 1.875_967_501_507_720_7, 0.041_555_057_407_175_613),
B: Components(0.055_630_079_696_993_609, -0.203_976_958_888_976_57, 1.056_971_514_242_878_6))
B: Components(0.055_630_079_696_993_609, -0.203_976_958_888_976_57, 1.056_971_514_242_878_6)
)
}

// Components<Double>
static func mInv<Value: ComponentValue>() -> MInvType<Value> {
(
X: Components(0.412_390_799_265_959_48, 0.357_584_339_383_877_96, 0.180_480_788_401_834_29),
Y: Components(0.212_639_005_871_510_36, 0.715_168_678_767_755_93, 0.072_192_315_360_733_715),
Z: Components(0.019_330_818_715_591_851, 0.119_194_779_794_625_99, 0.950_532_152_249_660_58))
Z: Components(0.019_330_818_715_591_851, 0.119_194_779_794_625_99, 0.950_532_152_249_660_58)
)
}

// Hard-coded D65 standard illuminant
Expand Down Expand Up @@ -84,7 +88,7 @@ func getBounds<Value: ComponentValue>(lightness: Value) -> [Vector<Value>] {
let (m1, m2, m3) = value as! Components<Value> // swiftlint:disable:this identifier_name
// swiftlint:disable:previous identifier_name
let targets: [Value] = [0.0, 1.0]
targets.forEach { target in
for target in targets {
let top1: Value = (284_517 * m1 - 94_839 * m3) * sub2
let multiple: Value = (838_422 * m3 + 769_860 * m2 + 731_718 * m1)
let top2: Value = multiple * lightness * sub2 - 769_860 * target * lightness
Expand All @@ -98,8 +102,8 @@ func getBounds<Value: ComponentValue>(lightness: Value) -> [Vector<Value>] {

func intersectLine<Value: ComponentValue>(
_ line1: Vector<Value>,
_ line2: Vector<Value>) -> Value
{
_ line2: Vector<Value>
) -> Value {
(line1.1 - line2.1) / (line2.0 - line1.0)
}

Expand Down
69 changes: 34 additions & 35 deletions Sources/WrkstrmColor/Palette+HSLuv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> HSLuv<V>
{
reversed: Bool = false
) -> HSLuv<V> {
var dIndex: V = .init(index)
let dCount: V = .init(count)

Expand All @@ -13,44 +13,43 @@ extension Palette {
}
let ratio = dIndex / dCount
let hsLuv: HSLuv<V> =
switch gradient
{
case .red:
HSLuv(
h: 12.2,
s: 100.0 - 33.0 * ratio,
l: 30.0 + 30.0 * ratio)
switch gradient {
case .red:
HSLuv(
h: 12.2,
s: 100.0 - 33.0 * ratio,
l: 30.0 + 30.0 * ratio)

case .blue:
HSLuv(
h: 258.6,
s: 100.0 - 33.0 * ratio,
l: 30.0 + 30.0 * ratio)
case .blue:
HSLuv(
h: 258.6,
s: 100.0 - 33.0 * ratio,
l: 30.0 + 30.0 * ratio)

case .green:
HSLuv(
h: 127.7,
s: 100.0 - 33.0 * ratio,
l: 50.0 + 25.0 * ratio)
case .green:
HSLuv(
h: 127.7,
s: 100.0 - 33.0 * ratio,
l: 50.0 + 25.0 * ratio)

case .yellow:
HSLuv(
h: 86,
s: 100.0 - 33.0 * ratio,
l: 70 + 30.0 * ratio)
case .yellow:
HSLuv(
h: 86,
s: 100.0 - 33.0 * ratio,
l: 70 + 30.0 * ratio)

case .black:
HSLuv(
h: 0,
s: 0,
l: 0 + 40.0 * ratio)
case .black:
HSLuv(
h: 0,
s: 0,
l: 0 + 40.0 * ratio)

case .white:
HSLuv(
h: 0,
s: 0,
l: 60.0 + 40.0 * ratio)
}
case .white:
HSLuv(
h: 0,
s: 0,
l: 60.0 + 40.0 * ratio)
}
return hsLuv
}
}
55 changes: 30 additions & 25 deletions Sources/WrkstrmColor/Palette+Legacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ extension Palette {
public static func rgb(
for gradient: Gradient,
index: Int,
count: Int, reversed: Bool = false) -> RGB<Double>
{
count: Int, reversed: Bool = false
) -> RGB<Double> {
var newIndex: Double = .init(index)
var newCount: Double = .init(count)

Expand All @@ -31,7 +31,8 @@ extension Palette {
ending = (
red: sR + cutoff * 10,
green: sG + cutoff * 10,
blue: sB + cutoff * 10)
blue: sB + cutoff * 10
)

case .blue:
starting = (red: 45, green: 100, blue: 215)
Expand All @@ -40,7 +41,8 @@ extension Palette {
ending = (
red: sR + cutoff * 12,
green: sG + cutoff * 12,
blue: sB + cutoff * 10)
blue: sB + cutoff * 10
)

case .red:
starting = (red: 215, green: 25, blue: 25)
Expand All @@ -49,7 +51,8 @@ extension Palette {
ending = (
red: sR + cutoff * 10,
green: sG + cutoff * 20,
blue: sB + cutoff * 10)
blue: sB + cutoff * 10
)

case .black:
starting = (red: 65, green: 65, blue: 65)
Expand All @@ -58,7 +61,8 @@ extension Palette {
ending = (
red: sR + cutoff * 8,
green: sG + cutoff * 8,
blue: sB + cutoff * 8)
blue: sB + cutoff * 8
)

default:
starting = (red: 200, green: 200, blue: 200)
Expand All @@ -67,7 +71,8 @@ extension Palette {
ending = (
red: sR + cutoff * 6,
green: sG + cutoff * 6,
blue: sB + cutoff * 6)
blue: sB + cutoff * 6
)
}

var delta = 1.0 / cutoff
Expand Down Expand Up @@ -102,8 +107,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> Color
{
reversed: Bool = false
) -> Color {
let color: HSLuv<CGFloat> =
hsluvGradient(for: gradient, index: index, count: count, reversed: reversed)
return Color(hsluv: color, opacity: 1)
Expand All @@ -114,8 +119,8 @@ extension Palette {
for wrkstrm: Wrkstrm,
index: Int,
count: Int,
reversed: Bool = false) -> Color
{
reversed: Bool = false
) -> Color {
color(
for: Gradient(rawValue: wrkstrm.rawValue)!, // swiftlint:disable:this force_unwrapping
index: index,
Expand All @@ -128,8 +133,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> Color
{
reversed: Bool = false
) -> Color {
let color = Self.rgb(for: gradient, index: index, count: count, reversed: reversed)
return Color(
red: color.r / 255.0,
Expand All @@ -148,8 +153,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> UIColor
{
reversed: Bool = false
) -> UIColor {
let color: HSLuv<CGFloat> =
hsluvGradient(for: gradient, index: index, count: count, reversed: reversed)
return UIColor(hsluv: color, alpha: 1)
Expand All @@ -159,8 +164,8 @@ extension Palette {
for wrkstrm: Wrkstrm,
index: Int,
count: Int,
reversed: Bool = false) -> UIColor
{
reversed: Bool = false
) -> UIColor {
color(
for: Gradient(rawValue: wrkstrm.rawValue)!, // swiftlint:disable:this force_unwrapping
index: index,
Expand All @@ -172,8 +177,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> UIColor
{
reversed: Bool = false
) -> UIColor {
let color = rgb(for: gradient, index: index, count: count, reversed: reversed)
return UIColor(
red: CGFloat(color.r / 255.0),
Expand All @@ -192,8 +197,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> NSColor
{
reversed: Bool = false
) -> NSColor {
let color: HSLuv<CGFloat> = hsluvGradient(
for: gradient, index: index, count: count, reversed: reversed)
return NSColor(hsluv: color, alpha: 1)
Expand All @@ -203,8 +208,8 @@ extension Palette {
for wrkstrm: Wrkstrm,
index: Int,
count: Int,
reversed: Bool = false) -> NSColor
{
reversed: Bool = false
) -> NSColor {
color(
for: Gradient(rawValue: wrkstrm.rawValue)!, // swiftlint:disable:this force_unwrapping
index: index,
Expand All @@ -216,8 +221,8 @@ extension Palette {
for gradient: Gradient,
index: Int,
count: Int,
reversed: Bool = false) -> NSColor
{
reversed: Bool = false
) -> NSColor {
let color = Self.rgb(for: gradient, index: index, count: count, reversed: reversed)
return NSColor(
red: CGFloat(color.r / 255.0),
Expand Down
6 changes: 3 additions & 3 deletions Sources/WrkstrmColor/RGBEncodable+ContrastRatio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension RGBEncodable {
let (red, green, blue) = components
let luminance =
0.212_6 * invGamSRGB(inverseColor: red) + 0.715_2 * invGamSRGB(inverseColor: green) + 0.072_2
* invGamSRGB(inverseColor: blue)
* invGamSRGB(inverseColor: blue)
return min(1, max(0, luminance))
}

Expand Down Expand Up @@ -83,8 +83,8 @@ extension RGBEncodable {
/// 3.0:1 for larger text.
func sufficientContrast(
to other: some RGBEncodable,
with font: UIFont = UIFont.preferredFont(forTextStyle: .body)) -> Bool
{
with font: UIFont = UIFont.preferredFont(forTextStyle: .body)
) -> Bool {
let pointSizeThreshold: Value = font.isBold ? 14.0 : 18.0
let contrastRatioThreshold: Value =
Value(font.fontDescriptor.pointSize) < pointSizeThreshold ? 4.5 : 3.0
Expand Down
Loading

0 comments on commit 8b2595d

Please sign in to comment.