Skip to content

Commit

Permalink
Fix the mathematical operator precedence overload #51
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickl committed Sep 14, 2019
1 parent cc68113 commit 79048e2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 35 deletions.
10 changes: 5 additions & 5 deletions Sources/DynamicColor+Lab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public extension DynamicColor {
let clippedB = clip(b, -128, 127)

let normalized = { (c: CGFloat) -> CGFloat in
pow(c, 3) > 0.008856 ? pow(c, 3) : (c - 16 / 116) / 7.787
pow(c, 3) > 0.008856 ? pow(c, 3) : (c - (16 / 116)) / 7.787
}

let preY = (clippedL + 16) / 116
let preX = clippedA / 500 + preY
let preZ = preY - clippedB / 200
let preX = (clippedA / 500) + preY
let preZ = preY - (clippedB / 200)

let X = 95.05 * normalized(preX)
let Y = 100 * normalized(preY)
Expand All @@ -75,15 +75,15 @@ public extension DynamicColor {
*/
final func toLabComponents() -> (L: CGFloat, a: CGFloat, b: CGFloat) {
let normalized = { (c: CGFloat) -> CGFloat in
c > 0.008856 ? pow(c, 1.0 / 3) : 7.787 * c + 16.0 / 116
c > 0.008856 ? pow(c, 1.0 / 3) : (7.787 * c) + (16.0 / 116)
}

let xyz = toXYZComponents()
let normalizedX = normalized(xyz.X / 95.05)
let normalizedY = normalized(xyz.Y / 100)
let normalizedZ = normalized(xyz.Z / 108.9)

let L = roundDecimal(116 * normalizedY - 16, precision: 1000)
let L = roundDecimal((116 * normalizedY) - 16, precision: 1000)
let a = roundDecimal(500 * (normalizedX - normalizedY), precision: 1000)
let b = roundDecimal(200 * (normalizedY - normalizedZ), precision: 1000)

Expand Down
32 changes: 16 additions & 16 deletions Sources/DynamicColor+Mixing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public extension DynamicColor {
let c1 = toLabComponents()
let c2 = color.toLabComponents()

let L = c1.L + weight * (c2.L - c1.L)
let a = c1.a + weight * (c2.a - c1.a)
let b = c1.b + weight * (c2.b - c1.b)
let alpha = alphaComponent + weight * (color.alphaComponent - alphaComponent)
let L = c1.L + (weight * (c2.L - c1.L))
let a = c1.a + (weight * (c2.a - c1.a))
let b = c1.b + (weight * (c2.b - c1.b))
let alpha = alphaComponent + (weight * (color.alphaComponent - alphaComponent))

return DynamicColor(L: L, a: a, b: b, alpha: alpha)
}
Expand All @@ -98,10 +98,10 @@ public extension DynamicColor {
let c1 = toHSLComponents()
let c2 = color.toHSLComponents()

let h = c1.h + weight * mixedHue(source: c1.h, target: c2.h)
let s = c1.s + weight * (c2.s - c1.s)
let l = c1.l + weight * (c2.l - c1.l)
let alpha = alphaComponent + weight * (color.alphaComponent - alphaComponent)
let h = c1.h + (weight * mixedHue(source: c1.h, target: c2.h))
let s = c1.s + (weight * (c2.s - c1.s))
let l = c1.l + (weight * (c2.l - c1.l))
let alpha = alphaComponent + (weight * (color.alphaComponent - alphaComponent))

return DynamicColor(hue: h, saturation: s, lightness: l, alpha: alpha)
}
Expand All @@ -110,10 +110,10 @@ public extension DynamicColor {
let c1 = toHSBComponents()
let c2 = color.toHSBComponents()

let h = c1.h + weight * mixedHue(source: c1.h, target: c2.h)
let s = c1.s + weight * (c2.s - c1.s)
let b = c1.b + weight * (c2.b - c1.b)
let alpha = alphaComponent + weight * (color.alphaComponent - alphaComponent)
let h = c1.h + (weight * mixedHue(source: c1.h, target: c2.h))
let s = c1.s + (weight * (c2.s - c1.s))
let b = c1.b + (weight * (c2.b - c1.b))
let alpha = alphaComponent + (weight * (color.alphaComponent - alphaComponent))

return DynamicColor(hue: h, saturation: s, brightness: b, alpha: alpha)
}
Expand All @@ -122,10 +122,10 @@ public extension DynamicColor {
let c1 = toRGBAComponents()
let c2 = color.toRGBAComponents()

let red = c1.r + weight * (c2.r - c1.r)
let green = c1.g + weight * (c2.g - c1.g)
let blue = c1.b + weight * (c2.b - c1.b)
let alpha = alphaComponent + weight * (color.alphaComponent - alphaComponent)
let red = c1.r + (weight * (c2.r - c1.r))
let green = c1.g + (weight * (c2.g - c1.g))
let blue = c1.b + (weight * (c2.b - c1.b))
let alpha = alphaComponent + (weight * (color.alphaComponent - alphaComponent))

return DynamicColor(red: red, green: green, blue: blue, alpha: alpha)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/DynamicColor+XYZ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public extension DynamicColor {
return abs(roundDecimal(rgb, precision: 1000))
}

let red = toRGB(clippedX * 3.2406 + clippedY * -1.5372 + clippedZ * -0.4986)
let green = toRGB(clippedX * -0.9689 + clippedY * 1.8758 + clippedZ * 0.0415)
let blue = toRGB(clippedX * 0.0557 + clippedY * -0.2040 + clippedZ * 1.0570)
let red = toRGB((clippedX * 3.2406) + (clippedY * -1.5372) + (clippedZ * -0.4986))
let green = toRGB((clippedX * -0.9689) + (clippedY * 1.8758) + (clippedZ * 0.0415))
let blue = toRGB((clippedX * 0.0557) + (clippedY * -0.2040) + (clippedZ * 1.0570))

self.init(red: red, green: green, blue: blue, alpha: alpha)
}
Expand All @@ -80,9 +80,9 @@ public extension DynamicColor {
let green = toSRGB(rgba.g)
let blue = toSRGB(rgba.b)

let X = roundDecimal((red * 0.4124 + green * 0.3576 + blue * 0.1805) * 100, precision: 1000)
let Y = roundDecimal((red * 0.2126 + green * 0.7152 + blue * 0.0722) * 100, precision: 1000)
let Z = roundDecimal((red * 0.0193 + green * 0.1192 + blue * 0.9505) * 100, precision: 1000)
let X = roundDecimal(((red * 0.4124) + (green * 0.3576) + (blue * 0.1805)) * 100, precision: 1000)
let Y = roundDecimal(((red * 0.2126) + (green * 0.7152) + (blue * 0.0722)) * 100, precision: 1000)
let Z = roundDecimal(((red * 0.0193) + (green * 0.1192) + (blue * 0.9505)) * 100, precision: 1000)

return (X: X, Y: Y, Z: Z)
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/HSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ internal struct HSL {
}

if rgba.r == maximum {
h = (rgba.g - rgba.b) / delta + (rgba.g < rgba.b ? 6 : 0)
h = ((rgba.g - rgba.b) / delta) + (rgba.g < rgba.b ? 6 : 0)
}
else if rgba.g == maximum {
h = (rgba.b - rgba.r) / delta + 2
h = ((rgba.b - rgba.r) / delta) + 2
}
else if rgba.b == maximum {
h = (rgba.r - rgba.g) / delta + 4
h = ((rgba.r - rgba.g) / delta) + 4
}
}

Expand All @@ -109,9 +109,9 @@ internal struct HSL {
let m2 = l <= 0.5 ? l * (s + 1) : (l + s) - (l * s)
let m1 = (l * 2) - m2

let r = hueToRGB(m1: m1, m2: m2, h: h + 1 / 3)
let r = hueToRGB(m1: m1, m2: m2, h: h + (1 / 3))
let g = hueToRGB(m1: m1, m2: m2, h: h)
let b = hueToRGB(m1: m1, m2: m2, h: h - 1 / 3)
let b = hueToRGB(m1: m1, m2: m2, h: h - (1 / 3))

return DynamicColor(red: r, green: g, blue: b, alpha: CGFloat(a))
}
Expand All @@ -121,13 +121,13 @@ internal struct HSL {
let hue = moda(h, m: 1)

if hue * 6 < 1 {
return m1 + (m2 - m1) * hue * 6
return m1 + ((m2 - m1) * hue * 6)
}
else if hue * 2 < 1 {
return CGFloat(m2)
}
else if hue * 3 < 1.9999 {
return m1 + (m2 - m1) * (2 / 3 - hue) * 6
return m1 + ((m2 - m1) * (2 / 3 - hue) * 6)
}

return CGFloat(m1)
Expand All @@ -142,7 +142,7 @@ internal struct HSL {
- returns: A HSL color with the hue changed.
*/
func adjustedHue(amount: CGFloat) -> HSL {
return HSL(hue: h * 360 + amount, saturation: s, lightness: l, alpha: a)
return HSL(hue: (h * 360) + amount, saturation: s, lightness: l, alpha: a)
}

/**
Expand Down

0 comments on commit 79048e2

Please sign in to comment.