|
@@ -41,4 +41,17 @@ extension UIColor {
|
|
|
let brightness = ((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000
|
|
|
return (brightness < 0.05)
|
|
|
}
|
|
|
+
|
|
|
+ func isLight(threshold: Float = 0.7) -> Bool {
|
|
|
+ let originalCGColor = self.cgColor
|
|
|
+
|
|
|
+ // Now we need to convert it to the RGB colorspace. UIColor.white / UIColor.black are greyscale and not RGB.
|
|
|
+ // If you don't do this then you will crash when accessing components index 2 below when evaluating greyscale colors.
|
|
|
+ let RGBCGColor = originalCGColor.converted(to: CGColorSpaceCreateDeviceRGB(), intent: .defaultIntent, options: nil)
|
|
|
+ guard let components = RGBCGColor?.components else { return false }
|
|
|
+ guard components.count >= 3 else { return false }
|
|
|
+
|
|
|
+ let brightness = Float(((components[0] * 299) + (components[1] * 587) + (components[2] * 114)) / 1000)
|
|
|
+ return (brightness > threshold)
|
|
|
+ }
|
|
|
}
|