-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathUIColor+Extension.swift
158 lines (123 loc) · 5.6 KB
/
UIColor+Extension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// UIColor+Extension.swift
// ownCloud
//
// Created by Pablo Carrascal on 15/03/2018.
// Copyright © 2018 ownCloud GmbH. All rights reserved.
//
/*
* Copyright (C) 2018, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/
import UIKit
extension UIColor {
public convenience init(red: Int, green: Int, blue: Int, alpha: Float = 1.0) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: CGFloat(alpha))
}
public convenience init(hexa unsignedRGBAHex: UInt) {
let rgbaHex = Int(unsignedRGBAHex)
self.init(red: (rgbaHex >> 24) & 0xFF, green: (rgbaHex >> 16) & 0xFF, blue: (rgbaHex >> 8) & 0xFF, alpha: Float(rgbaHex & 0xFF) / 255.0)
}
public convenience init(hex unsignedRGBHex: UInt, alpha: Float = 1.0) {
let rgbHex = Int(unsignedRGBHex)
self.init(red: (rgbHex >> 16) & 0xFF, green: (rgbHex >> 8) & 0xFF, blue: (rgbHex & 0xFF), alpha: alpha)
}
public func blended(withFraction fraction: Double, ofColor blendColor: UIColor) -> UIColor {
var selfRed : CGFloat = 0, selfGreen : CGFloat = 0, selfBlue : CGFloat = 0, selfAlpha : CGFloat = 0
var blendRed : CGFloat = 0, blendGreen : CGFloat = 0, blendBlue : CGFloat = 0, blendAlpha : CGFloat = 0
self.getRed(&selfRed, green:&selfGreen, blue:&selfBlue, alpha:&selfAlpha)
blendColor.getRed(&blendRed, green:&blendGreen, blue:&blendBlue, alpha:&blendAlpha)
return UIColor(red: CGFloat((Double(selfRed) * (1.0-fraction)) + (Double(blendRed) * fraction)),
green: CGFloat((Double(selfGreen) * (1.0-fraction)) + (Double(blendGreen) * fraction)),
blue: CGFloat((Double(selfBlue) * (1.0-fraction)) + (Double(blendBlue) * fraction)),
alpha: CGFloat((Double(selfAlpha) * (1.0-fraction)) + (Double(blendAlpha) * fraction)))
}
public func lighter(_ fraction: Double) -> UIColor {
var selfRed : CGFloat = 0, selfGreen : CGFloat = 0, selfBlue : CGFloat = 0, selfAlpha : CGFloat = 0
self.getRed(&selfRed, green:&selfGreen, blue:&selfBlue, alpha:&selfAlpha)
return UIColor(red: CGFloat((Double(selfRed) * (1.0-fraction)) + fraction),
green: CGFloat((Double(selfGreen) * (1.0-fraction)) + fraction),
blue: CGFloat((Double(selfBlue) * (1.0-fraction)) + fraction),
alpha: selfAlpha)
}
public func darker(_ fraction: Double) -> UIColor {
var selfRed : CGFloat = 0, selfGreen : CGFloat = 0, selfBlue : CGFloat = 0, selfAlpha : CGFloat = 0
self.getRed(&selfRed, green:&selfGreen, blue:&selfBlue, alpha:&selfAlpha)
return UIColor(red: CGFloat(Double(selfRed) * (1.0-fraction)),
green: CGFloat(Double(selfGreen) * (1.0-fraction)),
blue: CGFloat(Double(selfBlue) * (1.0-fraction)),
alpha: selfAlpha)
}
public var greyscale : UIColor {
var selfRed : CGFloat = 0, selfGreen : CGFloat = 0, selfBlue : CGFloat = 0, selfAlpha : CGFloat = 0, greyscale : CGFloat = 0
self.getRed(&selfRed, green:&selfGreen, blue:&selfBlue, alpha:&selfAlpha)
greyscale = (selfRed + selfGreen + selfBlue) / 3
return UIColor(red: CGFloat(greyscale),
green: CGFloat(greyscale),
blue: CGFloat(greyscale),
alpha: selfAlpha)
}
public var luminance : CGFloat {
var selfRed : CGFloat = 0, selfGreen : CGFloat = 0, selfBlue : CGFloat = 0, selfAlpha : CGFloat = 0
var luminance : CGFloat = 0
self.getRed(&selfRed, green:&selfGreen, blue:&selfBlue, alpha:&selfAlpha)
// Luminance according to https://de.wikipedia.org/wiki/Luminanz
luminance = selfRed * 0.2126 + selfGreen * 0.7152 + selfBlue * 0.0722
return luminance
}
public func hexString(leadIn: String = "#") -> String {
var selfRed : CGFloat = 0, selfGreen : CGFloat = 0, selfBlue : CGFloat = 0, selfAlpha : CGFloat = 0
self.getRed(&selfRed, green:&selfGreen, blue:&selfBlue, alpha:&selfAlpha)
return (String(format: "\(leadIn)%02x%02x%02x", Int(selfRed*255.0), Int(selfGreen*255.0), Int(selfBlue*255.0)))
}
public var brightness: CGFloat? {
guard let components = cgColor.components, components.count > 2 else { return nil }
return ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
}
public func isLight() -> Bool {
if let brightness, brightness > 0.5 {
return true
}
return false
}
public func contrast(to otherColor: UIColor) -> CGFloat? {
if let brightness, let otherBrightness = otherColor.brightness {
if brightness > otherBrightness {
return (brightness + 0.05) / (otherBrightness + 0.05)
} else {
return (otherBrightness + 0.05) / (brightness + 0.05)
}
}
return nil
}
public func preferredContrastColor(from colors: [UIColor]) -> UIColor? {
var highestContrastColor: UIColor?
var highestContrast: CGFloat = 0
for color in colors {
if let contrast = contrast(to: color) {
if (contrast > highestContrast) || (highestContrastColor == nil) {
highestContrastColor = color
highestContrast = contrast
}
}
}
return highestContrastColor
}
public func withHighContrastAlternative(_ highContrastColor: UIColor) -> UIColor {
return UIColor(dynamicProvider: { _ in
if UIAccessibility.isDarkerSystemColorsEnabled {
return highContrastColor
} else {
return self
}
})
}
}