Created
November 6, 2017 21:55
-
-
Save ashtonmeuser/8ca7eac73ca96fd706c96fd50dc70028 to your computer and use it in GitHub Desktop.
Determines whether a color is light or dark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // ColorIsLight.swift | |
| // | |
| // Created by Ashton Meuser on 2017-04-26. | |
| // Copyright © 2017 Ashton Meuser. All rights reserved. | |
| // | |
| import Foundation | |
| import UIKit | |
| extension UIColor { | |
| func isLight() -> Bool { | |
| // Refer to https://www.w3.org/WAI/ER/WD-AERT/#color-contrast | |
| guard let components = self.cgColor.components else { | |
| return false | |
| } | |
| var brightness = components[0]*299 | |
| brightness += components[1]*587 | |
| brightness += components[2]*114 | |
| return brightness > 490 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment