Created
February 18, 2016 22:00
-
-
Save ronanrodrigo/a0244cc61c1c883d3479 to your computer and use it in GitHub Desktop.
Darken color
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
| func darkenColor(color: NSString, amount: Float) -> (hexColor: String, color: NSColor) { | |
| let originalRed = Float(Int(color.substringWithRange(NSRange(location: 0, length: 2)), radix: 16)!) | |
| let originalGreen = Float(Int(color.substringWithRange(NSRange(location: 2, length: 2)), radix: 16)!) | |
| let originalBlue = Float(Int(color.substringWithRange(NSRange(location: 4, length: 2)), radix: 16)!) | |
| let valueRed = originalRed * amount | |
| let darkenRed = String(format: "%02X", Int(valueRed)) | |
| let valueGreen = originalGreen * amount | |
| let darkenGreen = String(format: "%02X", Int(valueGreen)) | |
| let valueBlue = originalBlue * amount | |
| let darkenBlue = String(format: "%02X", Int(valueBlue)) | |
| return ( | |
| "#\(darkenRed)\(darkenGreen)\(darkenBlue)", | |
| NSColor( | |
| red: CGFloat(Int(darkenRed, radix: 16)!)/255, | |
| green: CGFloat(Int(darkenGreen, radix: 16)!)/255, | |
| blue: CGFloat(Int(darkenBlue, radix: 16)!)/255, | |
| alpha: 1.0 | |
| ) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment