Last active
March 3, 2020 13:25
-
-
Save dnedrow/1204a57348f1228d58db7003dadb1fce to your computer and use it in GitHub Desktop.
Defines functions for transforming RGBA values.
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
| // Created by Nedrow, David E on 2019-04-11. | |
| // Attribution 4.0 International (CC 4.0), see bottom. | |
| import Foundation | |
| // MARK: - UIColor+RGBAProtocol.swift | |
| /// Defines functions for transforming RGBA values. | |
| public protocol RGBAProtocol { | |
| /// Create a color from the given RGBA value. | |
| /// | |
| /// - Parameter rgbValue: The rgb value | |
| /// - Returns: Color based on the input value. | |
| static func colorFromRGBA(_ rgbaValue: UInt32) -> UIColor | |
| /// Create a color using the given RGBA string. | |
| /// Implementations of this protocol should handle strings containing | |
| /// a leading pound sign "#". | |
| /// | |
| /// - Parameter rgbaString: String description. | |
| /// - Returns: Color based on the input string. | |
| static func colorFromRGBAString(_ rgbaString: String) -> UIColor? | |
| /// Creates an RGBA hex string representation of this color, optionally | |
| /// decorated with a leading poound (#) sign. | |
| /// | |
| /// - Parameter withDecoration: Prefix string with a pound (#) sign. | |
| /// - Returns: The hex string representation of this color. | |
| func rgbaString(withDecoration: Bool?) -> String | |
| } | |
| // MARK: - Default implementation of RGBAProtocol static functions. | |
| /// Default implementations of the RGBAProtocol static functions. | |
| public extension RGBAProtocol { | |
| /// Create a color from the given RGBA value. | |
| /// | |
| /// - Parameter rgbValue: The rgb value | |
| /// - Returns: Color based on the input value. | |
| static func colorFromRGBA(_ rgbaValue: UInt32) -> UIColor { | |
| return UIColor( | |
| red: CGFloat((rgbaValue & 0xFF000000) >> 24) / 255.0, | |
| green: CGFloat((rgbaValue & 0x00FF0000) >> 16) / 255.0, | |
| blue: CGFloat((rgbaValue & 0x0000FF00) >> 8) / 255.0, | |
| alpha: CGFloat(rgbaValue & 0x000000FF) / 255.0 | |
| ) | |
| } | |
| /// Create a color using the given RGBA string. | |
| /// Implementations of this protocol should handle strings containing | |
| /// a leading pound sign "#". | |
| /// | |
| /// - Parameter rgbaString: String description. | |
| /// - Returns: Color based on the input string. | |
| static func colorFromRGBAString(_ rgbaString: String) -> UIColor? { | |
| var rgbaStringLength: Int = rgbaString.count | |
| if rgbaString.firstIndex(of: "#") != nil { | |
| rgbaStringLength -= 1 | |
| } | |
| if rgbaStringLength != 8 { | |
| return nil | |
| } | |
| guard let rgbaValue = rgbaString.intFromHexString() /* Ignores "#" prefix else */ else { | |
| return nil | |
| } | |
| return UIColor( | |
| red: CGFloat((rgbaValue & 0xFF000000) >> 24) / 255.0, | |
| green: CGFloat((rgbaValue & 0x00FF0000) >> 16) / 255.0, | |
| blue: CGFloat((rgbaValue & 0x0000FF00) >> 8) / 255.0, | |
| alpha: CGFloat(rgbaValue & 0x000000FF) / 255.0 | |
| ) | |
| } | |
| } | |
| // MARK: - Default implementation of RGBAProtocol instance functions. | |
| /// Default implementations of the RGBAProtocol instance functions. | |
| extension UIColor: RGBAProtocol { | |
| /// Creates an RGBA hex string representation of this color, optionally | |
| /// decorated with a leading poound (#) sign. | |
| /// | |
| /// - Parameter withDecoration: Prefix string with a pound (#) sign. | |
| /// - Returns: The hex string representation of this color. | |
| public func rgbaString(withDecoration: Bool? = false) -> String { | |
| var red: CGFloat = 0 | |
| var green: CGFloat = 0 | |
| var blue: CGFloat = 0 | |
| var alpha: CGFloat = 0 | |
| self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) | |
| let rgba: Int = (Int)(red * 255) << 24 | (Int)(green * 255) << 16 | (Int)(blue * 255) << 8 | (Int)(alpha * 255) << 0 | |
| return String(format: (withDecoration ?? false) ? "#%08x" : "%08x", rgba) | |
| } | |
| } | |
| // This work is licensed under Attribution 4.0 International (CC 4.0) | |
| // You are free to: | |
| // Share — copy and redistribute the material in any medium or format | |
| // Adapt — remix, transform, and build upon the material | |
| // for any purpose, even commercially. | |
| // | |
| // This license is acceptable for Free Cultural Works. | |
| // The licensor cannot revoke these freedoms as long as you follow the license terms. | |
| // See https://creativecommons.org/licenses/by/4.0/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment