Skip to content

Instantly share code, notes, and snippets.

@freddyh
Created April 5, 2019 20:01
Show Gist options
  • Save freddyh/38491e69e80f4f808b46b20852bc011e to your computer and use it in GitHub Desktop.
Save freddyh/38491e69e80f4f808b46b20852bc011e to your computer and use it in GitHub Desktop.
Color Convenience
import UIKit
extension UIColor {
/**
Initializes and returns a color object using the specified RGB component values.
- Parameter red: Red value of the color object
- Parameter green: Green value of the color object
- Parameter blue: Green value of the color object
- Returns: A new color object.
*/
convenience init(red: Int, green: Int, blue: Int) {
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: 1.0)
}
/**
Extracts the red, green, and blue component values using the specified Int value
- Parameter rgb: Integer that represents the rgb component values of the desired color
- Returns: A new color object.
*/
convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment