Last active
August 12, 2017 08:30
-
-
Save HarshilShah/af5fb535607fe96949a8787539245ad5 to your computer and use it in GitHub Desktop.
To simplify accessing UIColor components
This file contains 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
// | |
// UIColor+Components.swift | |
// | |
// Created by Harshil Shah on 12/08/17. | |
// Copyright © 2017 Harshil Shah. All rights reserved. | |
// | |
import UIKit | |
extension UIColor { | |
enum Component { | |
case red, green, blue | |
case hue, saturation, brightness | |
case white, alpha | |
} | |
func get(component: UIColor.Component) -> CGFloat? { | |
var result: CGFloat = 0 | |
let resultWasFound: Bool | |
switch component { | |
case .red: resultWasFound = getRed(&result, green: nil, blue: nil, alpha: nil) | |
case .green: resultWasFound = getRed(nil, green: &result, blue: nil, alpha: nil) | |
case .blue: resultWasFound = getRed(nil, green: nil, blue: &result, alpha: nil) | |
case .hue: resultWasFound = getHue(&result, saturation: nil, brightness: nil, alpha: nil) | |
case .saturation: resultWasFound = getHue(nil, saturation: &result, brightness: nil, alpha: nil) | |
case .brightness: resultWasFound = getHue(nil, saturation: nil, brightness: &result, alpha: nil) | |
case .white: resultWasFound = getWhite(&result, alpha: nil) | |
case .alpha: resultWasFound = getWhite(nil, alpha: &result) | |
} | |
if resultWasFound { | |
return result | |
} else { | |
return nil | |
} | |
} | |
} |
This file contains 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
// | |
// UIColor+ComponentSet.swift | |
// | |
// Created by Harshil Shah on 12/08/17. | |
// Copyright © 2017 Harshil Shah. All rights reserved. | |
// | |
import UIKit | |
extension UIColor { | |
private enum ComponentSet { | |
case rgba | |
case hsba | |
} | |
private func get(componentSet: UIColor.ComponentSet) -> (CGFloat, CGFloat, CGFloat, CGFloat)? { | |
var result: (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0) | |
let resultWasFound: Bool | |
switch componentSet { | |
case .rgba: resultWasFound = getRed(&result.0, green: &result.1, blue: &result.2, alpha: &result.3) | |
case .hsba: resultWasFound = getHue(&result.0, saturation: &result.1, brightness: &result.2, alpha: &result.3) | |
} | |
if resultWasFound { | |
return result | |
} else { | |
return nil | |
} | |
} | |
func getRGBA() -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? { | |
return get(componentSet: .rgba) | |
} | |
func getHSBA() -> (hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)? { | |
return get(componentSet: .hsba) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment