Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Oklab and Oklch #291

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,25 @@ struct LCHuv{T} <: Color{T,3}
end
```

### Oklab and Oklch

A perceptually uniform colorspace developed by
[Björn Ottosson](https://bottosson.github.io/posts/oklab/) and its
reparameterization using cylindrical coordinates.

```julia
struct Oklab{T} <: Color{T,3}
l::T # Lightness in [0,1]
a::T # Red/Green
b::T # Blue/Yellow
end

struct Oklch{T} <: Color{T,3}
l::T # Lightness in [0,1]
c::T # Chroma
h::T # Hue in [0,360]
end
```

### DIN99

Expand Down
5 changes: 3 additions & 2 deletions src/ColorTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export HSV, HSB, HSL, HSI
export XYZ, xyY, LMS, Lab, LCHab, Luv, LCHuv
export DIN99, DIN99d, DIN99o
export YIQ, YCbCr
export Oklab, Oklch

export Gray

Expand Down Expand Up @@ -65,8 +66,8 @@ ColorTypes summary:
- `RGB`, `BGR`, `XRGB`, `RGBX`, `RGB24` are all subtypes of `AbstractRGB`

- `HSV`, `HSL`, `HSI`, `XYZ`, `xyY`, `Lab`, `LCHab`, `Luv`, `LCHuv`,
`DIN99`, `DIN99d`, `DIN99o`, `LMS`, `YIQ`, `YCbCR` are subtypes of
`Color{T,3}`
`DIN99`, `DIN99d`, `DIN99o`, `LMS`, `YIQ`, `YCbCR`, `Oklab`, and
`Oklch` are subtypes of `Color{T,3}`

- Alpha-channel analogs in such as `ARGB` and `RGBA` for most of those
types (with a few exceptions like `RGB24`, which has `ARGB32`)
Expand Down
5 changes: 5 additions & 0 deletions src/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ gamutmin(::Type{<:YIQ}) = (0, -0.5957, -0.5226)
gamutmax(::Type{<:YCbCr}) = (235, 240, 240)
gamutmin(::Type{<:YCbCr}) = (16, 16, 16)

gamutmax(::Type{<:Oklab}) = (1, 0.4, 0.4)
gamutmin(::Type{<:Oklab}) = (0, -0.4, -0.4)

gamutmax(::Type{<:Oklch}) = (1, 0.4, 360)

gamutmax(::Type{C}) where {C<:TransparentColor} = (gamutmax(color_type(C))..., 1)
gamutmin(::Type{C}) where {C<:TransparentColor} = (gamutmin(color_type(C))..., 0)

Expand Down
10 changes: 5 additions & 5 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ gray(c::AGray32) = reinterpret(N0f8, c.color % UInt8)
gray(x::Number) = convert(eltype(ccolor(Gray, typeof(x))), x) # ensures it's a type supported by Gray

"""
`chroma(c)` returns the chroma of a `Lab`, `Luv` or their variants color.
`chroma(c)` returns the chroma of a `Lab`, `Luv`, `Oklab` or their variants color.
!!! note
The other color types (e.g. `RGB`, `HSV` or `YCbCr`) are not supported
because their definitions of *chroma* are not clear. Colorfulness, chroma
and saturation are defined as distinct aspects by the CIE.
"""
chroma(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{Lab,DIN99,DIN99o,DIN99d}} = sqrt(c.a^2 + c.b^2)
chroma(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{Lab,DIN99,DIN99o,DIN99d,Oklab}} = sqrt(c.a^2 + c.b^2)
chroma(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Luv} = sqrt(c.u^2 + c.v^2)
chroma(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{LCHab,LCHuv}} = c.c
chroma(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{LCHab,LCHuv,Oklch}} = c.c

"""
`hue(c)` returns the hue in degrees. This function does not guarantee that the
return value is in [0, 360].
"""
hue(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{HSV,HSL,HSI,LCHab,LCHuv}} = c.h
hue(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{Lab,DIN99,DIN99o,DIN99d}} =
hue(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{HSV,HSL,HSI,LCHab,LCHuv,Oklch}} = c.h
hue(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Union{Lab,DIN99,DIN99o,DIN99d,Oklab}} =
(h = atand(c.b, c.a); h < 0 ? h + 360 : h)
hue(c::Union{C,AlphaColor{C},ColorAlpha{C}}) where {C<:Luv} =
(h = atand(c.v, c.u); h < 0 ? h + 360 : h)
Expand Down
18 changes: 17 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ struct YCbCr{T<:AbstractFloat} <: Color{T,3}
cr::T
end

"`Oklab` is the Oklab colorspace."
struct Oklab{T<:AbstractFloat} <: Color{T,3}
l::T # Lightness in [0,1]
a::T # Red/Green
b::T # Blue/Yellow
end

"`Oklch` is the Luminance-Chroma-Hue, Polar-Oklab colorspace."
struct Oklch{T<:AbstractFloat} <: Color{T,3}
l::T # Lightness in [0,1]
c::T # Chroma
h::T # Hue in [0,360]
end

"""
`RGB24` uses a `UInt32` representation of color, 0xAARRGGBB, where
R=red, G=green, B=blue and A is irrelevant. This format is often used
Expand Down Expand Up @@ -491,7 +505,9 @@ for (C, acol, cola) in [(DIN99d, :ADIN99d, :DIN99dA),
(xyY, :AxyY, :xyYA),
(BGR, :ABGR, :BGRA),
(RGB, :ARGB, :RGBA),
(Gray, :AGray, :GrayA)]
(Gray, :AGray, :GrayA),
(Oklab, :AOklab, :OklabA),
(Oklch, :AOklch, :OklchA)]
fn = Expr(:tuple, fieldnames(C)...)
cfn = Expr(:tuple, colorfields(C)...)
elty = eltype_default(C)
Expand Down
5 changes: 3 additions & 2 deletions test/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ end
for (a, b) in ((RGB(1, 0.5, 0), RGBA(1, 0.5, 0, 0.9)),
(RGB(0.5, 0.5, 0.5), Gray(0.5)),
(HSV(0, 0, 0.5), HSV(100, 0, 0.5)), # grays
(Lab(70, 0, 60), LCHab(70, 60, 90)))
(Lab(70, 0, 60), LCHab(70, 60, 90)),
(Oklab(0.7, 0, 0.2), Oklch(0.7, 0.2, 90)))
local a, b
@test a != b
@test !isequal(a, b)
Expand Down Expand Up @@ -195,7 +196,7 @@ end
ARGB{Float32}, RGBA{N0f16}, XRGB{N0f8}, RGBX{Float64},
BGR{Float16}, ABGR{N0f32}, BGRA{N2f14},
HSV{Float32}, HSL{Float64}, ALab{Float32}, LCHabA{Float16},
Gray, AGray, GrayA,
AOklab{Float32}, Oklch{Float16}, Gray, AGray, GrayA,
unique(ColorTypes.parametric3)...,
AHSV, HSLA)
CC = isconcretetype(C) ? C : C{Float64}
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ doctest(ColorTypes, manual = false)
ctypes = union(setdiff(ColorTypes.parametric3, (XRGB, RGBX)), (Gray,))
@test Set(ctypes) ==
Set([DIN99d, DIN99o, DIN99, HSI, HSL, HSV, LCHab, LCHuv,
LMS, Lab, Luv, XYZ, YCbCr, YIQ, xyY, BGR, RGB, Gray])
LMS, Lab, Luv, XYZ, YCbCr, YIQ, xyY, BGR, RGB, Gray,
Oklab, Oklch])

if isdefined(Base, :Experimental) && isdefined(Base.Experimental, :register_error_hint)
@testset "error_hints" begin
Expand Down
4 changes: 4 additions & 0 deletions test/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ end
@test chroma(LCHuv(60, 40, 30)) ≈ 40.0
@test chroma(LabA(60, -40, 30, 0.5)) ≈ 50.0
@test chroma(ALab(60, -40, 30, 0.5)) ≈ 50.0
@test chroma(Oklab(0.6, -0.4, 0.3)) ≈ 0.5
@inferred chroma(LCHab(60, 40, 30))
@inferred chroma(LCHuv(6f1, 4f1, 3f1))
@inferred chroma(Oklch(0.6f1, 0.3f1, 3f1))
@inferred chroma(LabA(60, -40, 30, 0.5))
@test_throws MethodError chroma(HSV(30, 0.4, 0.6))
end
Expand All @@ -112,7 +114,9 @@ end
@test hue(LCHuv(60, 40, 30)) ≈ 30.0
@test hue(LabA(60, -30, 30, 0.5)) ≈ 135.0
@test hue(ALab(60, -30, 30, 0.5)) ≈ 135.0
@test hue(Oklab(0.6, -0.3, 0.3)) ≈ 135.0
@inferred hue(LCHab(60, -30, 30))
@inferred hue(Oklch(0.6, 0.1, 30))
@inferred hue(LCHuv(6f1, -3f1, 3f1))
@inferred hue(LabA(60, -30, 30, 0.5))
@test hue(HSV(999, 0.4, 0.6)) == 999 # without normalization
Expand Down
1 change: 1 addition & 0 deletions test/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ end
@test RGB(0.2, Gray24(0.3), 0.4) === RGB(0.2, 0.3N0f8, 0.4)
@test HSV(0.2, 0.3, Gray(0.4)) === HSV(0.2, 0.3, 0.4)
@test ALab(0.2, 0.3, 0.4, Gray24(0.5)) === ALab(0.2, 0.3, 0.4, 0.5N0f8)
@test AOklab(0.2, 0.3, 0.4, Gray24(0.5)) === AOklab(0.2, 0.3, 0.4, 0.5N0f8)
end

@testset "gray constructors" begin
Expand Down