Created
February 12, 2016 17:55
-
-
Save mingsai/706105515944db718ea5 to your computer and use it in GitHub Desktop.
A Swift class with additional functionality for Color. Also shows name spacing techniques for adding non-colliding extensions.
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
| // | |
| // MNGColorExtensions.swift | |
| // | |
| // | |
| // Created by Tommie N. Carter, Jr., MBA on 8/19/15. | |
| // Copyright © 2015 MING Technology. All rights reserved. | |
| // | |
| import Foundation | |
| #if os(OSX) | |
| import Cocoa | |
| public typealias PXColor = NSColor | |
| #else | |
| import UIKit | |
| public typealias PXColor = UIColor | |
| #endif | |
| extension PXColor { | |
| func lighter(amount : CGFloat = 0.25) -> PXColor { | |
| return hueColorWithBrightnessAmount(1 + amount) | |
| } | |
| func darker(amount : CGFloat = 0.25) -> PXColor { | |
| return hueColorWithBrightnessAmount(1 - amount) | |
| } | |
| private func hueColorWithBrightnessAmount(amount: CGFloat) -> PXColor { | |
| var hue : CGFloat = 0 | |
| var saturation : CGFloat = 0 | |
| var brightness : CGFloat = 0 | |
| var alpha : CGFloat = 0 | |
| #if os(iOS) | |
| if getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) { | |
| return PXColor( hue: hue, | |
| saturation: saturation, | |
| brightness: brightness * amount, | |
| alpha: alpha ) | |
| } else { | |
| return self | |
| } | |
| #else | |
| getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) | |
| return PXColor( hue: hue, | |
| saturation: saturation, | |
| brightness: brightness * amount, | |
| alpha: alpha ) | |
| #endif | |
| } | |
| func colorWithHexString (hexStr:String, alpha:Float? = 1.0) -> PXColor { | |
| var hexInt:UInt32 = 0 | |
| let scanner = NSScanner(string: hexStr) | |
| scanner.charactersToBeSkipped = NSCharacterSet(charactersInString: "#") | |
| scanner.scanHexInt(&hexInt) | |
| return | |
| PXColor(colorLiteralRed: ((Float) ((hexInt & 0xFF0000) >> 16))/255, green: ((Float) ((hexInt & 0xFF00) >> 8))/255, blue: ((Float) (hexInt & 0xFF))/255, alpha: alpha!) | |
| } | |
| func hexStringWithColor (color:PXColor) -> String { | |
| let components = CGColorGetComponents(color.CGColor) | |
| let r = Float(components[0]) | |
| let g = Float(components[1]) | |
| let b = Float(components[2]) | |
| return String(format: "#%02lX%02lX%02lX", lroundf(r * 255),lroundf(g * 255),lroundf(b * 255)) | |
| } | |
| 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) | |
| } | |
| convenience init(netHex:Int) { | |
| self.init(red:(netHex >> 16) & 0xff, green:(netHex >> 8) & 0xff, blue:netHex & 0xff) | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment