Skip to content

Instantly share code, notes, and snippets.

View adamgraham's full-sized avatar
🎨

Adam Graham adamgraham

🎨
View GitHub Profile
@adamgraham
adamgraham / UIColor+WebSafe.swift
Created May 29, 2019 01:08
An extension of the iOS class UIColor to derive web safe colors.
/// An extension to derive web safe colors.
extension UIColor {
/// The nearest web safe color to the current color. All web safe colors have RGB
/// component values of 0, 51, 102, 153, 204, or 255 (20% intervals).
var webSafe: UIColor {
var (r, g, b, a) = (CGFloat(), CGFloat(), CGFloat(), CGFloat())
getRed(&r, green: &g, blue: &b, alpha: &a)
let clusters: [CGFloat] = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
@adamgraham
adamgraham / UIColor+YIQ.swift
Last active May 29, 2019 04:27
An extension of the iOS class UIColor to provide conversion to and from YIQ colors.
/// An extension to provide conversion to and from Y′IQ colors.
extension UIColor {
/// The Y′IQ components of a color - luma (Y′) and chroma (I,Q).
struct YIQ: Hashable {
/// The luma component of the color, in the range [0, 1] (black to white).
var Y: CGFloat
/// The orange-blue chroma component of the color, in the range [-0.596, 0.596].
var I: CGFloat
@adamgraham
adamgraham / UIColor+YUV.swift
Last active June 21, 2021 22:47
An extension of the iOS class UIColor to provide conversion to and from YUV colors.
/// An extension to provide conversion to and from Y′UV colors.
extension UIColor {
/// The Y′UV components of a color - luma (Y′) and chroma (U,V).
struct YUV: Hashable {
/// The luma component of the color, in the range [0, 1] (black to white).
var Y: CGFloat
/// The blue-difference chroma component of the color, in the range [-0.436, 0.436].
var U: CGFloat
@adamgraham
adamgraham / UIColor+HSI.swift
Created May 28, 2019 03:42
An extension of the iOS class UIColor to provide conversion to and from HSI (hue, saturation, intensity) colors.
/// An extension to provide conversion to and from HSI (hue, saturation, intensity) colors.
extension UIColor {
/// The HSI (hue, saturation, intensity) components of a color.
struct HSI: 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+YPbPr.swift
Last active May 29, 2019 04:33
An extension of the iOS class UIColor to provide conversion to and from YPbPr colors.
/// An extension to provide conversion to and from Y′PbPr 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′PbPr components of a color - luma (Y′) and chroma (Pb,Pr).
struct YPbPr: Hashable {
/// The luma component of the color, in the range [0, 1] (black to white).
@adamgraham
adamgraham / UIColor+LinearRGB.swift
Last active November 1, 2021 19:24
An extension of the iOS class UIColor to provide conversion to and from linear RGB colors.
/// An extension to provide conversion to and from linear RGB colors.
extension UIColor {
/// A set of non-gamma corrected linear RGB values, in the range [0, 1].
typealias LinearRGB = (r: CGFloat, g: CGFloat, b: CGFloat)
/// The inverse companded sRGB components to get non-gamma corrected linear values,
/// in the range [0, 1].
var linearRGB: LinearRGB {
var (r, g, b) = (CGFloat(), CGFloat(), CGFloat())
@adamgraham
adamgraham / UIColor+CIELCh.swift
Last active December 3, 2023 10:20
An extension of the iOS class UIColor to provide conversion to and from CIELCh° colors.
/// An extension to provide conversion to and from CIELCh° colors.
extension UIColor {
/// The CIELCh° components of a color - lightness (L), chroma (C), and hue (h).
struct CIELCh: Hashable {
/// The lightness component of the color, in the range [0, 100] (darkest to brightest).
var L: CGFloat
/// The chroma component of the color.
var C: CGFloat
@adamgraham
adamgraham / UIColor+CIELUV.swift
Last active December 3, 2023 10:20
An extension of the iOS class UIColor to provide conversion to and from CIELUV colors.
/// An extension to provide conversion to and from CIELUV colors.
extension UIColor {
/// The CIELUV components of a color - lightness (L) and chromaticity (u,v).
struct CIELUV: 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 [-100, 100].
var u: CGFloat
@adamgraham
adamgraham / UIView+ParentViewController.swift
Last active May 18, 2019 03:12
An extension of the iOS class UIView to traverse the responder chain for the nearest parent view controller.
/// An extension to traverse the responder chain for the nearest parent view controller.
extension UIView {
/// The nearest parent view controller in the responder chain.
var parentViewController: UIViewController? {
if let nextResponder = self.next as? UIViewController {
return nextResponder
} else if let nextResponder = self.next as? UIView {
return nextResponder.parentViewController
} else {
@adamgraham
adamgraham / UIColor+HunterLab.swift
Last active June 2, 2019 03:54
An extension of the iOS class UIColor to provide conversion to and from Hunter Lab colors.
/// An extension to provide conversion to and from Hunter Lab colors.
extension UIColor {
/// XYZ tristimulus values of d65 illuminant with standard 2° observer.
private static let d65: (X: CGFloat, Y: CGFloat, Z: CGFloat) = (95.047, 100.000, 108.883)
/// The Hunter Lab components of a color - lightness (L) and chromaticity (a,b).
struct HunterLab: Hashable {
/// The lightness component of the color, in the range [0, 100] (darkest to brightest).