https://github.com/importryan/oklab
Björn Ottosson's Oklab perceptual color space in Swift
https://github.com/importryan/oklab
color colorspace colorspaces oklab swift
Last synced: 9 months ago
JSON representation
Björn Ottosson's Oklab perceptual color space in Swift
- Host: GitHub
- URL: https://github.com/importryan/oklab
- Owner: importRyan
- Created: 2021-03-16T21:49:14.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-30T22:18:38.000Z (almost 5 years ago)
- Last Synced: 2025-04-06T04:33:39.470Z (about 1 year ago)
- Topics: color, colorspace, colorspaces, oklab, swift
- Language: Swift
- Homepage:
- Size: 17.6 KB
- Stars: 39
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Oklab
Swift implementation of Björn Ottosson's Oklab perceptual color space for image processing.
Learn more:
- [From Björn](https://bottosson.github.io/posts/oklab/)
- [Review by Raph Levien](https://raphlinus.github.io/color/2021/01/18/oklab-critique.html)

## How to use
Inits move any `NSColor` `UIColor` `Color` `CIColor` or `CGColor` to the Oklab color space. Conversion uses `simd`.
```swift
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.
```swift
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.](https://bottosson.github.io/img/oklab/oklab_munsell.png)
```swift
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 {
SIMD3(lightness, a, b)
}
}
```
```swift
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.