Skip to content

Instantly share code, notes, and snippets.

View adamgraham's full-sized avatar
🎨

Adam Graham adamgraham

🎨
View GitHub Profile
@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+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
@adamgraham
adamgraham / UIColor+CIEXYZ.swift
Last active December 3, 2023 10:20
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+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 / String+CamelCase.swift
Last active July 30, 2023 12:56
An extension of the Swift String type to provide camel case formatting.
/// An extension to format strings in *camel case*.
extension String {
/// A collection of all the words in the string by separating out any punctuation and spaces.
var words: [String] {
return components(separatedBy: CharacterSet.alphanumerics.inverted).filter { !$0.isEmpty }
}
/// Returns a copy of the string with the first word beginning lowercased, and the first
/// letter of each word thereafter is capitalized, with no intervening spaces or punctuation,
@adamgraham
adamgraham / UIColor+HSL.swift
Last active July 6, 2022 02:34
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+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+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+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).