Skip to content

Instantly share code, notes, and snippets.

@ashtonmeuser
Created November 6, 2017 21:55
Show Gist options
  • Select an option

  • Save ashtonmeuser/8ca7eac73ca96fd706c96fd50dc70028 to your computer and use it in GitHub Desktop.

Select an option

Save ashtonmeuser/8ca7eac73ca96fd706c96fd50dc70028 to your computer and use it in GitHub Desktop.
Determines whether a color is light or dark
//
// 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