Swiftpack.co -  importRyan/Oklab as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
importRyan/Oklab
Björn Ottosson's Oklab perceptual color space in Swift
.package(url: "https://github.com/importRyan/Oklab.git", from: "1.1.2")

Oklab

Swift implementation of Björn Ottosson's Oklab perceptual color space for image processing.

Learn more:

Color space Munsell comparisons

How to use

Inits move any NSColor UIColor Color CIColor or CGColor to the Oklab color space. Conversion uses simd.

import Oklab

let black      = NSColor.black

let oklab      = OklabColor(ns: black)
let stillBlack = NSColor(oklab)

let polar      = OklabColorPolar(ns: black)
let stillBlack = NSColor(polar)

Manipulating hue and chroma is easy with a polar Oklab color. The idea behind Oklab is that shifts in hue do not change perceived brightness or chroma. While hue is stored in radians, a few functions help manipulate hue in degrees.

let pink = OklabColorPolar(lightness: 0.5, chroma: 0.24, hueDegrees: 0)
let blue = pink.withHueRotatedBy(degrees: 240)

let cartesian = OklabColor(lightness: 0.5, a: 0.2, b: 0.1, alpha: 1)
var polar     = OklabColorPolar(cartesian)
polar.setHueTo(degrees: 120)
polar.setHueRotatedBy(degrees: -90)

// Cartesian colors also report chroma and hue
let hueAngle = cartesian.getHueAngleDegrees()
let hueAngle = cartesian.getHueAngleRadians()
let chroma   = cartesian.getChroma()

The OklabColor and OklabPolarColor structs are defined as below. Channel is a typealias for Float. For an idea about the ranges for the a and b channels, see Björn's Munsell plot preserved above or on his GitHub.

struct OklabColor {

    /// Perceptual lightness. To achieve pure black or white, 
    /// also adjust chroma (polar) or a/b channels (cartesian).
    var lightness: Channel
    
    /// Green to red. 
    /// Range can span about -0.6...0.6 in medium brightness.
    var a: Channel
    
    /// Yellow to blue. 
    /// Range can span about -0.55...0.8 in medium brightness.
    var b: Channel
    
    /// Opacity 0...1
    var alpha: Channel
    
    /// Vector construct of Lab channels for SIMD-based calculations
    var vector: SIMD3<Channel> {
        SIMD3<Channel>(lightness, a, b)
    }
}

struct OklabColorPolar {
    
    /// Perceived lightness
    var lightness: Channel
    
    ///  Chroma
    var chroma: Channel
    
    /// Set in the 0...2π range when converted from a cartesian color
    var hueRadians: Channel
    
    var hueDegrees: Channel {
        var degrees = hueRadians * 180 / .pi
        
        while degrees < 0   { degrees += 360 }
        while degrees > 360 { degrees -= 360 }
        return degrees
    }
    
    /// Alpha 0...1
    var alpha: Channel
}

That's all folks.

License ("MIT")

Swift implementation is Copyright (c) 2021 Ryan Ferrell under the license below.

Oklab is Copyright (c) 2020 Björn Ottosson.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

GitHub

link
Stars: 1
Last commit: 2 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Swift Oklab implementation
13 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API