Add decimal information to constants #51

This commit is contained in:
Yannick Loriot 2019-09-15 18:16:30 +01:00
parent 0dfcf55012
commit a859fc170d
12 changed files with 80 additions and 80 deletions

View File

@ -49,9 +49,9 @@ extension DynamicColor {
case .standard: case .standard:
return 4.5 return 4.5
case .standardLargeText: case .standardLargeText:
return 3 return 3.0
case .enhanced: case .enhanced:
return 7 return 7.0
case .enhancedLargeText: case .enhancedLargeText:
return 4.5 return 4.5
} }

View File

@ -52,7 +52,7 @@ public extension DynamicColor {
- seealso: adjustedHueColor: - seealso: adjustedHueColor:
*/ */
final func complemented() -> DynamicColor { final func complemented() -> DynamicColor {
return adjustedHue(amount: 180) return adjustedHue(amount: 180.0)
} }
/** /**
@ -116,9 +116,9 @@ public extension DynamicColor {
final func inverted() -> DynamicColor { final func inverted() -> DynamicColor {
let rgba = toRGBAComponents() let rgba = toRGBAComponents()
let invertedRed = 1 - rgba.r let invertedRed = 1.0 - rgba.r
let invertedGreen = 1 - rgba.g let invertedGreen = 1.0 - rgba.g
let invertedBlue = 1 - rgba.b let invertedBlue = 1.0 - rgba.b
return DynamicColor(red: invertedRed, green: invertedGreen, blue: invertedBlue, alpha: rgba.a) return DynamicColor(red: invertedRed, green: invertedGreen, blue: invertedBlue, alpha: rgba.a)
} }

View File

@ -41,9 +41,9 @@ extension DynamicColor {
- returns: The HSB components as a tuple (h, s, b). - returns: The HSB components as a tuple (h, s, b).
*/ */
public final func toHSBComponents() -> (h: CGFloat, s: CGFloat, b: CGFloat) { public final func toHSBComponents() -> (h: CGFloat, s: CGFloat, b: CGFloat) {
var h: CGFloat = 0 var h: CGFloat = 0.0
var s: CGFloat = 0 var s: CGFloat = 0.0
var b: CGFloat = 0 var b: CGFloat = 0.0
#if os(iOS) || os(tvOS) || os(watchOS) #if os(iOS) || os(tvOS) || os(watchOS)
getHue(&h, saturation: &s, brightness: &b, alpha: nil) getHue(&h, saturation: &s, brightness: &b, alpha: nil)
@ -51,10 +51,10 @@ extension DynamicColor {
return (h: h, s: s, b: b) return (h: h, s: s, b: b)
#elseif os(OSX) #elseif os(OSX)
if isEqual(DynamicColor.black) { if isEqual(DynamicColor.black) {
return (0, 0, 0) return (0.0, 0.0, 0.0)
} }
else if isEqual(DynamicColor.white) { else if isEqual(DynamicColor.white) {
return (0, 0, 1) return (0.0, 0.0, 1.0)
} }
getHue(&h, saturation: &s, brightness: &b, alpha: nil) getHue(&h, saturation: &s, brightness: &b, alpha: nil)

View File

@ -60,6 +60,6 @@ extension DynamicColor {
public final func toHSLComponents() -> (h: CGFloat, s: CGFloat, l: CGFloat) { public final func toHSLComponents() -> (h: CGFloat, s: CGFloat, l: CGFloat) {
let hsl = HSL(color: self) let hsl = HSL(color: self)
return (hsl.h * 360, hsl.s, hsl.l) return (hsl.h * 360.0, hsl.s, hsl.l)
} }
} }

View File

@ -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. - 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) { convenience init(L: CGFloat, a: CGFloat, b: CGFloat, alpha: CGFloat = 1) {
let clippedL = clip(L, 0, 100) let clippedL = clip(L, 0.0, 100.0)
let clippedA = clip(a, -128, 127) let clippedA = clip(a, -128.0, 127.0)
let clippedB = clip(b, -128, 127) let clippedB = clip(b, -128.0, 127.0)
let normalized = { (c: CGFloat) -> CGFloat in 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 preY = (clippedL + 16.0) / 116.0
let preX = (clippedA / 500) + preY let preX = (clippedA / 500.0) + preY
let preZ = preY - (clippedB / 200) let preZ = preY - (clippedB / 200.0)
let X = 95.05 * normalized(preX) let X = 95.05 * normalized(preX)
let Y = 100 * normalized(preY) let Y = 100.0 * normalized(preY)
let Z = 108.9 * normalized(preZ) let Z = 108.9 * normalized(preZ)
self.init(X: X, Y: Y, Z: Z, alpha: alpha) self.init(X: X, Y: Y, Z: Z, alpha: alpha)
@ -75,17 +75,17 @@ public extension DynamicColor {
*/ */
final func toLabComponents() -> (L: CGFloat, a: CGFloat, b: CGFloat) { final func toLabComponents() -> (L: CGFloat, a: CGFloat, b: CGFloat) {
let normalized = { (c: CGFloat) -> CGFloat in 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 xyz = toXYZComponents()
let normalizedX = normalized(xyz.X / 95.05) let normalizedX = normalized(xyz.X / 95.05)
let normalizedY = normalized(xyz.Y / 100) let normalizedY = normalized(xyz.Y / 100.0)
let normalizedZ = normalized(xyz.Z / 108.9) let normalizedZ = normalized(xyz.Z / 108.9)
let L = roundDecimal((116 * normalizedY) - 16, precision: 1000) let L = roundDecimal((116.0 * normalizedY) - 16.0, precision: 1000)
let a = roundDecimal(500 * (normalizedX - normalizedY), precision: 1000) let a = roundDecimal(500.0 * (normalizedX - normalizedY), precision: 1000)
let b = roundDecimal(200 * (normalizedY - normalizedZ), precision: 1000) let b = roundDecimal(200.0 * (normalizedY - normalizedZ), precision: 1000)
return (L: L, a: a, b: b) return (L: L, a: a, b: b)
} }

View File

@ -46,7 +46,7 @@ public extension DynamicColor {
- Returns: A color object corresponding to the two colors object mixed together. - 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 { 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 { switch colorspace {
case .lab: case .lab:
@ -131,11 +131,11 @@ public extension DynamicColor {
} }
func mixedHue(source: CGFloat, target: CGFloat) -> CGFloat { func mixedHue(source: CGFloat, target: CGFloat) -> CGFloat {
if target > source && target - source > 180 { if target > source && target - source > 180.0 {
return target - source + 360 return target - source + 360.0
} }
else if target < source && source - target > 180 { else if target < source && source - target > 180.0 {
return target + 360 - source return target + 360.0 - source
} }
return target - source return target - source

View File

@ -115,7 +115,7 @@ public extension DynamicColor {
*/ */
final func adjustedAlpha(amount: CGFloat) -> DynamicColor { final func adjustedAlpha(amount: CGFloat) -> DynamicColor {
let components = toRGBAComponents() 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) return DynamicColor(red: components.r, green: components.g, blue: components.b, alpha: normalizedAlpha)
} }

View File

@ -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. - 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) { convenience init(X: CGFloat, Y: CGFloat, Z: CGFloat, alpha: CGFloat = 1) {
let clippedX = clip(X, 0, 95.05) / 100 let clippedX = clip(X, 0.0, 95.05) / 100.0
let clippedY = clip(Y, 0, 100) / 100 let clippedY = clip(Y, 0.0, 100) / 100.0
let clippedZ = clip(Z, 0, 108.9) / 100 let clippedZ = clip(Z, 0.0, 108.9) / 100.0
let toRGB = { (c: CGFloat) -> CGFloat in 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)) let red = toRGB((clippedX * 3.2406) + (clippedY * -1.5372) + (clippedZ * -0.4986))
@ -80,9 +80,9 @@ public extension DynamicColor {
let green = toSRGB(rgba.g) let green = toSRGB(rgba.g)
let blue = toSRGB(rgba.b) let blue = toSRGB(rgba.b)
let X = roundDecimal(((red * 0.4124) + (green * 0.3576) + (blue * 0.1805)) * 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, precision: 1000) 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, precision: 1000) let Z = roundDecimal(((red * 0.0193) + (green * 0.1192) + (blue * 0.9505)) * 100.0, precision: 1000.0)
return (X: X, Y: Y, Z: Z) return (X: X, Y: Y, Z: Z)
} }

View File

@ -87,10 +87,10 @@ public extension DynamicColor {
let b = cappedHex >> (alphaChannel ? 8 : 0) & mask let b = cappedHex >> (alphaChannel ? 8 : 0) & mask
let a = alphaChannel ? cappedHex & mask : 255 let a = alphaChannel ? cappedHex & mask : 255
let red = CGFloat(r) / 255 let red = CGFloat(r) / 255.0
let green = CGFloat(g) / 255 let green = CGFloat(g) / 255.0
let blue = CGFloat(b) / 255 let blue = CGFloat(b) / 255.0
let alpha = CGFloat(a) / 255 let alpha = CGFloat(a) / 255.0
self.init(red: red, green: green, blue: blue, alpha: alpha) self.init(red: red, green: green, blue: blue, alpha: alpha)
} }
@ -170,7 +170,7 @@ public extension DynamicColor {
*/ */
func isLight() -> Bool { func isLight() -> Bool {
let components = toRGBAComponents() 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 return brightness >= 0.5
} }

View File

@ -61,7 +61,7 @@ final public class DynamicGradient {
return (0 ..< amount).map { _ in colors[0] } 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) } return (0 ..< amount).map { pickColorAt(scale: CGFloat($0) * increment, inColorSpace: colorspace) }
} }
@ -80,7 +80,7 @@ final public class DynamicGradient {
return colors.first ?? .black 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) } let positions = (0 ..< colors.count).map { CGFloat($0) / CGFloat(colors.count - 1) }
var color: DynamicColor = .black var color: DynamicColor = .black
@ -88,7 +88,7 @@ final public class DynamicGradient {
for (index, position) in positions.enumerated() { for (index, position) in positions.enumerated() {
guard clippedScale <= position else { continue } guard clippedScale <= position else { continue }
guard clippedScale != 0 && clippedScale != 1 else { guard clippedScale != 0.0 && clippedScale != 1.0 else {
return colors[index] return colors[index]
} }

View File

@ -33,13 +33,13 @@
/// Hue-saturation-lightness structure to make the color manipulation easier. /// Hue-saturation-lightness structure to make the color manipulation easier.
internal struct HSL { internal struct HSL {
/// Hue value between 0.0 and 1.0 (0.0 = 0 degree, 1.0 = 360 degree). /// 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. /// 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. /// 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. /// Alpha value between 0.0 and 1.0.
var a: CGFloat = 1 var a: CGFloat = 1.0
// MARK: - Initializing HSL Colors // MARK: - Initializing HSL Colors
@ -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 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. - 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) { init(hue: CGFloat, saturation: CGFloat, lightness: CGFloat, alpha: CGFloat = 1.0) {
h = hue.truncatingRemainder(dividingBy: 360) / 360 h = hue.truncatingRemainder(dividingBy: 360.0) / 360.0
s = clip(saturation, 0, 1) s = clip(saturation, 0.0, 1.0)
l = clip(lightness, 0, 1) l = clip(lightness, 0.0, 1.0)
a = clip(alpha, 0, 1) a = clip(alpha, 0.0, 1.0)
} }
/** /**
@ -71,30 +71,30 @@ internal struct HSL {
let delta = maximum - mininimum let delta = maximum - mininimum
h = 0 h = 0.0
s = 0 s = 0.0
l = (maximum + mininimum) / 2 l = (maximum + mininimum) / 2.0
if delta != 0 { if delta != 0.0 {
if l < 0.5 { if l < 0.5 {
s = delta / (maximum + mininimum) s = delta / (maximum + mininimum)
} }
else { else {
s = delta / (2 - maximum - mininimum) s = delta / (2.0 - maximum - mininimum)
} }
if rgba.r == maximum { 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 { 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 { 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 a = rgba.a
} }
@ -106,12 +106,12 @@ internal struct HSL {
- returns: A DynamicColor object corresponding to the current HSV color. - returns: A DynamicColor object corresponding to the current HSV color.
*/ */
func toDynamicColor() -> DynamicColor { func toDynamicColor() -> DynamicColor {
let m2 = l <= 0.5 ? l * (s + 1) : (l + s) - (l * s) let m2 = l <= 0.5 ? l * (s + 1.0) : (l + s) - (l * s)
let m1 = (l * 2) - m2 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 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)) return DynamicColor(red: r, green: g, blue: b, alpha: CGFloat(a))
} }
@ -120,14 +120,14 @@ internal struct HSL {
private func hueToRGB(m1: CGFloat, m2: CGFloat, h: CGFloat) -> CGFloat { private func hueToRGB(m1: CGFloat, m2: CGFloat, h: CGFloat) -> CGFloat {
let hue = moda(h, m: 1) let hue = moda(h, m: 1)
if hue * 6 < 1 { if hue * 6 < 1.0 {
return m1 + ((m2 - m1) * hue * 6) return m1 + ((m2 - m1) * hue * 6.0)
} }
else if hue * 2 < 1 { else if hue * 2.0 < 1.0 {
return m2 return m2
} }
else if hue * 3 < 1.9999 { else if hue * 3.0 < 1.9999 {
return m1 + ((m2 - m1) * (2 / 3 - hue) * 6) return m1 + ((m2 - m1) * ((2.0 / 3.0) - hue) * 6.0)
} }
return m1 return m1
@ -142,7 +142,7 @@ internal struct HSL {
- returns: A HSL color with the hue changed. - returns: A HSL color with the hue changed.
*/ */
func adjustedHue(amount: CGFloat) -> HSL { 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)
} }
/** /**
@ -152,7 +152,7 @@ internal struct HSL {
- returns: A lighter HSL color. - returns: A lighter HSL color.
*/ */
func lighter(amount: CGFloat) -> HSL { 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)
} }
/** /**
@ -162,7 +162,7 @@ internal struct HSL {
- returns: A darker HSL color. - returns: A darker HSL color.
*/ */
func darkened(amount: CGFloat) -> HSL { func darkened(amount: CGFloat) -> HSL {
return lighter(amount: amount * -1) return lighter(amount: amount * -1.0)
} }
/** /**
@ -172,7 +172,7 @@ internal struct HSL {
- returns: A HSL color more saturated. - returns: A HSL color more saturated.
*/ */
func saturated(amount: CGFloat) -> HSL { 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)
} }
/** /**
@ -182,6 +182,6 @@ internal struct HSL {
- returns: A HSL color less saturated. - returns: A HSL color less saturated.
*/ */
func desaturated(amount: CGFloat) -> HSL { func desaturated(amount: CGFloat) -> HSL {
return saturated(amount: amount * -1) return saturated(amount: amount * -1.0)
} }
} }

View File

@ -61,13 +61,13 @@ internal func moda(_ x: CGFloat, m: CGFloat) -> CGFloat {
- Parameter x: The value to round. - Parameter x: The value to round.
- Parameter m: The precision. Default to 10000. - Parameter m: The precision. Default to 10000.
*/ */
internal func roundDecimal(_ x: CGFloat, precision: CGFloat = 10000) -> CGFloat { internal func roundDecimal(_ x: CGFloat, precision: CGFloat = 10000.0) -> CGFloat {
return CGFloat(Int(round(x * precision))) / precision return CGFloat(Int(round(x * precision))) / precision
} }
internal func roundToHex(_ x: CGFloat) -> UInt32 { internal func roundToHex(_ x: CGFloat) -> UInt32 {
guard x > 0 else { return 0 } guard x > 0 else { return 0 }
let rounded: CGFloat = round(x * 255) let rounded: CGFloat = round(x * 255.0)
return UInt32(rounded) return UInt32(rounded)
} }