Skip to content

Instantly share code, notes, and snippets.

View adamgraham's full-sized avatar
🎨

Adam Graham adamgraham

🎨
View GitHub Profile
@adamgraham
adamgraham / NibLoadable.swift
Last active June 16, 2019 21:35
A utility iOS protocol that allows for easy instantiation of classes that are associated with a Nib.
/// A type that can be loaded from an associated nib.
protocol NibLoadable: class {
/// The name of the nib for the loadable class.
static var nibName: String { get }
/// The bundle the nib is contained within for the loadable class.
static var nibBundle: Bundle { get }
}
@adamgraham
adamgraham / DismissSegue.swift
Last active May 18, 2019 03:16
An @IBDesignable iOS class that can be used in Interface Builder as a custom segue to dismiss a modally presented view controller.
@IBDesignable class DismissSegue: UIStoryboardSegue {
var completion: (() -> Void)?
override func perform() {
self.source.presentingViewController?.dismiss(animated: UIView.areAnimationsEnabled, completion: self.completion)
}
}
@adamgraham
adamgraham / UIColor+Hex.swift
Last active May 27, 2019 22:14
An extension of the iOS class UIColor to instantiate and represent RGB colors with hexadecimal values.
/// An extension to instantiate and represent RGB colors as hexadecimal values.
extension UIColor {
/// Initializes a color from a hexadecimal integer in the RGB format (RRGGBB), e.g., 0xffff00.
/// - parameter hex: The hexadecimal value of the color.
/// - parameter alpha: The alpha value of the color.
convenience init(hex: Int, alpha: CGFloat = 1.0) {
let rgb = (
r: CGFloat((hex >> 16) & 0xff) / 0xff,
g: CGFloat((hex >> 08) & 0xff) / 0xff,
@adamgraham
adamgraham / UIColor+RGB.swift
Last active May 27, 2019 23:29
An extension of the iOS class UIColor to provide conversion to and from RGB (red, green, blue) colors.
/// An extension to provide conversion to and from RGB (red, green, blue) colors.
extension UIColor {
/// The RGB (red, green, blue) components of a color, in the range [0, 255].
struct RGB: Hashable {
/// The red component of the color, in the range [0, 255].
var red: Int
/// The green component of the color, in the range [0, 255].
var green: Int
@adamgraham
adamgraham / UIColor+CMYK.swift
Last active May 7, 2024 06:33
An extension of the iOS class UIColor to provide conversion to and from CMYK (cyan, magenta, yellow, black) colors.
/// An extension to provide conversion to and from CMYK (cyan, magenta, yellow, black) colors.
extension UIColor {
/// The CMYK (cyan, magenta, yellow, black) components of a color, in the range [0, 100%].
struct CMYK: Hashable {
/// The cyan component of the color, in the range [0, 100%].
var cyan: CGFloat
/// The magenta component of the color, in the range [0, 100%].
var magenta: CGFloat
@adamgraham
adamgraham / UIColor+HSB.swift
Last active May 27, 2019 23:43
An extension of the iOS class UIColor to provide conversion to and from HSB (hue, saturation, brightness) colors.
/// An extension to provide conversion to and from HSB (hue, saturation, brightness) colors.
extension UIColor {
/// The HSB (hue, saturation, brightness) components of a color.
struct HSB: Hashable {
/// The hue component of the color, in the range [0, 360°].
var hue: CGFloat
/// The saturation component of the color, in the range [0, 100%].
var saturation: CGFloat
@adamgraham
adamgraham / UIColor+HSL.swift
Last active December 5, 2024 14:09
An extension of the iOS class UIColor to provide conversion to and from HSL (hue, saturation, lightness) colors.
/// An extension to provide conversion to and from HSL (hue, saturation, lightness) colors.
extension UIColor {
/// The HSL (hue, saturation, lightness) components of a color.
struct HSL: Hashable {
/// The hue component of the color, in the range [0, 360°].
var hue: CGFloat
/// The saturation component of the color, in the range [0, 100%].
var saturation: CGFloat
@adamgraham
adamgraham / UIColor+YCbCr.swift
Last active December 10, 2020 00:39
An extension of the iOS class UIColor to provide conversion to and from YCbCr colors.
/// An extension to provide conversion to and from Y′CbCr colors.
extension UIColor {
/// The Rec.601 (standard-definition) coefficients used to calculate luma.
private static let encoding: (r: CGFloat, g: CGFloat, b: CGFloat) = (0.299, 0.587, 0.114)
/// The Y′CbCr components of a color - luma (Y′) and chroma (Cb,Cr).
struct YCbCr: Hashable {
/// The luma component of the color, in the full range [0, 255] (black to white).
@adamgraham
adamgraham / UIColor+CIEXYZ.swift
Last active May 16, 2024 14:30
An extension of the iOS class UIColor to provide conversion to and from CIE 1931 XYZ colors.
/// An extension to provide conversion to and from CIE 1931 XYZ colors.
extension UIColor {
/// The CIE 1931 XYZ components of a color - luminance (Y) and chromaticity (X,Z).
struct CIEXYZ: Hashable {
/// A mix of cone response curves chosen to be orthogonal to luminance and
/// non-negative, in the range [0, 95.047].
var X: CGFloat
/// The luminance component of the color, in the range [0, 100].
@adamgraham
adamgraham / UIColor+CIELAB.swift
Last active December 3, 2023 10:20
An extension of the iOS class UIColor to provide conversion to and from CIELAB colors.
/// An extension to provide conversion to and from CIELAB colors.
extension UIColor {
/// The CIELAB components of a color - lightness (L) and chromaticity (a,b).
struct CIELAB: Hashable {
/// The lightness component of the color, in the range [0, 100] (darkest to brightest).
var L: CGFloat
/// The green-red chromaticity component of the color, typically in the range [-128, 128].
var a: CGFloat