Skip to content

Commit

Permalink
Add decimal information to constants #51
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickl committed Sep 15, 2019
1 parent 0dfcf55 commit d4fa351
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 79 deletions.
4 changes: 2 additions & 2 deletions Sources/ContrastDisplayContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ extension DynamicColor {
case .standard:
return 4.5
case .standardLargeText:
return 3
return 3.0
case .enhanced:
return 7
return 7.0
case .enhancedLargeText:
return 4.5
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/DynamicColor+Deriving.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public extension DynamicColor {
- seealso: adjustedHueColor:
*/
final func complemented() -> DynamicColor {
return adjustedHue(amount: 180)
return adjustedHue(amount: 180.0)
}

/**
Expand Down Expand Up @@ -116,9 +116,9 @@ public extension DynamicColor {
final func inverted() -> DynamicColor {
let rgba = toRGBAComponents()

let invertedRed = 1 - rgba.r
let invertedGreen = 1 - rgba.g
let invertedBlue = 1 - rgba.b
let invertedRed = 1.0 - rgba.r
let invertedGreen = 1.0 - rgba.g
let invertedBlue = 1.0 - rgba.b

return DynamicColor(red: invertedRed, green: invertedGreen, blue: invertedBlue, alpha: rgba.a)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/DynamicColor+HSB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ extension DynamicColor {
- returns: The HSB components as a tuple (h, s, b).
*/
public final func toHSBComponents() -> (h: CGFloat, s: CGFloat, b: CGFloat) {
var h: CGFloat = 0
var s: CGFloat = 0
var b: CGFloat = 0
var h: CGFloat = 0.0
var s: CGFloat = 0.0
var b: CGFloat = 0.0

#if os(iOS) || os(tvOS) || os(watchOS)
getHue(&h, saturation: &s, brightness: &b, alpha: nil)

return (h: h, s: s, b: b)
#elseif os(OSX)
if isEqual(DynamicColor.black) {
return (0, 0, 0)
return (0.0, 0.0, 0.0)
}
else if isEqual(DynamicColor.white) {
return (0, 0, 1)
return (0.0, 0.0, 1.0)
}

getHue(&h, saturation: &s, brightness: &b, alpha: nil)
Expand Down
2 changes: 1 addition & 1 deletion Sources/DynamicColor+HSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ extension DynamicColor {
public final func toHSLComponents() -> (h: CGFloat, s: CGFloat, l: CGFloat) {
let hsl = HSL(color: self)

return (hsl.h * 360, hsl.s, hsl.l)
return (hsl.h * 360.0, hsl.s, hsl.l)
}
}
22 changes: 11 additions & 11 deletions Sources/DynamicColor+Lab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ public extension DynamicColor {
- parameter alpha: The opacity value of the color object, specified as a value from 0.0 to 1.0. Default to 1.0.
*/
convenience init(L: CGFloat, a: CGFloat, b: CGFloat, alpha: CGFloat = 1) {
let clippedL = clip(L, 0, 100)
let clippedA = clip(a, -128, 127)
let clippedB = clip(b, -128, 127)
let clippedL = clip(L, 0.0, 100.0)
let clippedA = clip(a, -128.0, 127.0)
let clippedB = clip(b, -128.0, 127.0)

let normalized = { (c: CGFloat) -> CGFloat in
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 preY = (clippedL + 16.0) / 116.0
let preX = (clippedA / 500.0) + preY
let preZ = preY - (clippedB / 200.0)

let X = 95.05 * normalized(preX)
let Y = 100 * normalized(preY)
let Y = 100.0 * normalized(preY)
let Z = 108.9 * normalized(preZ)

self.init(X: X, Y: Y, Z: Z, alpha: alpha)
Expand All @@ -75,17 +75,17 @@ 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.0) : (7.787 * c) + (16.0 / 116.0)
}

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 a = roundDecimal(500 * (normalizedX - normalizedY), precision: 1000)
let b = roundDecimal(200 * (normalizedY - normalizedZ), precision: 1000)
let L = roundDecimal((116.0 * normalizedY) - 16.0, precision: 1000)
let a = roundDecimal(500.0 * (normalizedX - normalizedY), precision: 1000)
let b = roundDecimal(200.0 * (normalizedY - normalizedZ), precision: 1000)

return (L: L, a: a, b: b)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/DynamicColor+Mixing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public extension DynamicColor {
- Returns: A color object corresponding to the two colors object mixed together.
*/
final func mixed(withColor color: DynamicColor, weight: CGFloat = 0.5, inColorSpace colorspace: DynamicColorSpace = .rgb) -> DynamicColor {
let normalizedWeight = clip(weight, 0, 1)
let normalizedWeight = clip(weight, 0.0, 1.0)

switch colorspace {
case .lab:
Expand Down Expand Up @@ -131,11 +131,11 @@ public extension DynamicColor {
}

func mixedHue(source: CGFloat, target: CGFloat) -> CGFloat {
if target > source && target - source > 180 {
return target - source + 360
if target > source && target - source > 180.0 {
return target - source + 360.0
}
else if target < source && source - target > 180 {
return target + 360 - source
else if target < source && source - target > 180.0 {
return target + 360.0 - source
}

return target - source
Expand Down
2 changes: 1 addition & 1 deletion Sources/DynamicColor+RGBA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public extension DynamicColor {
*/
final func adjustedAlpha(amount: CGFloat) -> DynamicColor {
let components = toRGBAComponents()
let normalizedAlpha = clip(components.a + amount, 0, 1)
let normalizedAlpha = clip(components.a + amount, 0.0, 1.0)

return DynamicColor(red: components.r, green: components.g, blue: components.b, alpha: normalizedAlpha)
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/DynamicColor+XYZ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public extension DynamicColor {
- parameter alpha: The opacity value of the color object, specified as a value from 0.0 to 1.0. Default to 1.0.
*/
convenience init(X: CGFloat, Y: CGFloat, Z: CGFloat, alpha: CGFloat = 1) {
let clippedX = clip(X, 0, 95.05) / 100
let clippedY = clip(Y, 0, 100) / 100
let clippedZ = clip(Z, 0, 108.9) / 100
let clippedX = clip(X, 0.0, 95.05) / 100.0
let clippedY = clip(Y, 0.0, 100) / 100.0
let clippedZ = clip(Z, 0.0, 108.9) / 100.0

let toRGB = { (c: CGFloat) -> CGFloat in
let rgb = c > 0.0031308 ? 1.055 * pow(c, 1 / 2.4) - 0.055 : c * 12.92
let rgb = c > 0.0031308 ? 1.055 * pow(c, 1.0 / 2.4) - 0.055 : c * 12.92

return abs(roundDecimal(rgb, precision: 1000))
return abs(roundDecimal(rgb, precision: 1000.0))
}

let red = toRGB((clippedX * 3.2406) + (clippedY * -1.5372) + (clippedZ * -0.4986))
Expand Down Expand Up @@ -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.0, precision: 1000.0)
let Y = roundDecimal(((red * 0.2126) + (green * 0.7152) + (blue * 0.0722)) * 100.0, precision: 1000.0)
let Z = roundDecimal(((red * 0.0193) + (green * 0.1192) + (blue * 0.9505)) * 100.0, precision: 1000.0)

return (X: X, Y: Y, Z: Z)
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/DynamicColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public extension DynamicColor {
let b = cappedHex >> (alphaChannel ? 8 : 0) & mask
let a = alphaChannel ? cappedHex & mask : 255

let red = CGFloat(r) / 255
let green = CGFloat(g) / 255
let blue = CGFloat(b) / 255
let alpha = CGFloat(a) / 255
let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255.0
let alpha = CGFloat(a) / 255.0

self.init(red: red, green: green, blue: blue, alpha: alpha)
}
Expand Down Expand Up @@ -170,7 +170,7 @@ public extension DynamicColor {
*/
func isLight() -> Bool {
let components = toRGBAComponents()
let brightness = ((components.r * 299) + (components.g * 587) + (components.b * 114)) / 1000
let brightness = ((components.r * 299.0) + (components.g * 587.0) + (components.b * 114.0)) / 1000.0

return brightness >= 0.5
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/DynamicGradient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final public class DynamicGradient {
return (0 ..< amount).map { _ in colors[0] }
}

let increment = 1 / CGFloat(amount - 1)
let increment = 1.0 / CGFloat(amount - 1)

return (0 ..< amount).map { pickColorAt(scale: CGFloat($0) * increment, inColorSpace: colorspace) }
}
Expand All @@ -80,15 +80,15 @@ final public class DynamicGradient {
return colors.first ?? .black
}

let clippedScale = clip(scale, 0, 1)
let clippedScale = clip(scale, 0.0, 1.0)
let positions = (0 ..< colors.count).map { CGFloat($0) / CGFloat(colors.count - 1) }

var color: DynamicColor = .black

for (index, position) in positions.enumerated() {
guard clippedScale <= position else { continue }

guard clippedScale != 0 && clippedScale != 1 else {
guard clippedScale != 0.0 && clippedScale != 1.0 else {
return colors[index]
}

Expand Down
64 changes: 32 additions & 32 deletions Sources/HSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
/// Hue-saturation-lightness structure to make the color manipulation easier.
internal struct HSL {
/// Hue value between 0.0 and 1.0 (0.0 = 0 degree, 1.0 = 360 degree).
var h: CGFloat = 0
var h: CGFloat = 0.0
/// Saturation value between 0.0 and 1.0.
var s: CGFloat = 0
var s: CGFloat = 0.0
/// Lightness value between 0.0 and 1.0.
var l: CGFloat = 0
var l: CGFloat = 0.0
/// Alpha value between 0.0 and 1.0.
var a: CGFloat = 1
var a: CGFloat = 1.0

// MARK: - Initializing HSL Colors

Expand All @@ -51,11 +51,11 @@ internal struct HSL {
- parameter l: The lightness component of the color object, specified as a value between 0.0 and 1.0.
- parameter a: The opacity component of the color object, specified as a value between 0.0 and 1.0.
*/
init(hue: CGFloat, saturation: CGFloat, lightness: CGFloat, alpha: CGFloat = 1) {
h = hue.truncatingRemainder(dividingBy: 360) / 360
s = clip(saturation, 0, 1)
l = clip(lightness, 0, 1)
a = clip(alpha, 0, 1)
init(hue: CGFloat, saturation: CGFloat, lightness: CGFloat, alpha: CGFloat = 1.0) {
h = hue.truncatingRemainder(dividingBy: 360.0) / 360.0
s = clip(saturation, 0.0, 1.0)
l = clip(lightness, 0.0, 1.0)
a = clip(alpha, 0.0, 1.0)
}

/**
Expand All @@ -71,30 +71,30 @@ internal struct HSL {

let delta = maximum - mininimum

h = 0
s = 0
l = (maximum + mininimum) / 2
h = 0.0
s = 0.0
l = (maximum + mininimum) / 2.0

if delta != 0 {
if delta != 0.0 {
if l < 0.5 {
s = delta / (maximum + mininimum)
}
else {
s = delta / (2 - maximum - mininimum)
s = delta / (2.0 - maximum - mininimum)
}

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 : 0.0)
}
else if rgba.g == maximum {
h = ((rgba.b - rgba.r) / delta) + 2
h = ((rgba.b - rgba.r) / delta) + 2.0
}
else if rgba.b == maximum {
h = ((rgba.r - rgba.g) / delta) + 4
h = ((rgba.r - rgba.g) / delta) + 4.0
}
}

h /= 6
h /= 6.0
a = rgba.a
}

Expand All @@ -106,12 +106,12 @@ internal struct HSL {
- returns: A DynamicColor object corresponding to the current HSV color.
*/
func toDynamicColor() -> DynamicColor {
let m2 = l <= 0.5 ? l * (s + 1) : (l + s) - (l * s)
let m1 = (l * 2) - m2
let m2 = l <= 0.5 ? l * (s + 1.0) : (l + s) - (l * s)
let m1 = (l * 2.0) - m2

let r = hueToRGB(m1: m1, m2: m2, h: h + (1 / 3))
let r = hueToRGB(m1: m1, m2: m2, h: h + (1.0 / 3.0))
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.0 / 3.0))

return DynamicColor(red: r, green: g, blue: b, alpha: CGFloat(a))
}
Expand All @@ -120,14 +120,14 @@ internal struct HSL {
private func hueToRGB(m1: CGFloat, m2: CGFloat, h: CGFloat) -> CGFloat {
let hue = moda(h, m: 1)

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

return 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.0) + amount, saturation: s, lightness: l, alpha: a)
}

/**
Expand All @@ -152,7 +152,7 @@ internal struct HSL {
- returns: A lighter HSL color.
*/
func lighter(amount: CGFloat) -> HSL {
return HSL(hue: h * 360, saturation: s, lightness: l + amount, alpha: a)
return HSL(hue: h * 360.0, saturation: s, lightness: l + amount, alpha: a)
}

/**
Expand All @@ -162,7 +162,7 @@ internal struct HSL {
- returns: A darker HSL color.
*/
func darkened(amount: CGFloat) -> HSL {
return lighter(amount: amount * -1)
return lighter(amount: amount * -1.0)
}

/**
Expand All @@ -172,7 +172,7 @@ internal struct HSL {
- returns: A HSL color more saturated.
*/
func saturated(amount: CGFloat) -> HSL {
return HSL(hue: h * 360, saturation: s + amount, lightness: l, alpha: a)
return HSL(hue: h * 360.0, saturation: s + amount, lightness: l, alpha: a)
}

/**
Expand All @@ -182,6 +182,6 @@ internal struct HSL {
- returns: A HSL color less saturated.
*/
func desaturated(amount: CGFloat) -> HSL {
return saturated(amount: amount * -1)
return saturated(amount: amount * -1.0)
}
}
Loading

0 comments on commit d4fa351

Please sign in to comment.