From 79992723298184d160bb890ad91f9f4e2fbb2503 Mon Sep 17 00:00:00 2001 From: kimikage Date: Fri, 6 Aug 2021 12:58:55 +0900 Subject: [PATCH] [WIP] Modify `MSC` and `find_maximum_chroma` interfaces --- src/algorithms.jl | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/algorithms.jl b/src/algorithms.jl index ea581d9c..15c0cc7a 100644 --- a/src/algorithms.jl +++ b/src/algorithms.jl @@ -180,10 +180,11 @@ may also be specified. value. This behavior might change in a future release. """ -function MSC(h) +MSC(h) = MSC(Float64(h)) +function MSC(h::Float64) #Wrap h to [0, 360] range - h = normalize_hue(Float64(h)) + h = normalize_hue(h) #Selecting edge of RGB cube; R=1 G=2 B=3 # p #variable @@ -200,8 +201,7 @@ function MSC(h) end p, t = pt(h) - alpha=-sind(h) - beta=cosd(h) + alpha, beta = sincos(-deg2rad(h)) # un & vn are calculated based on reference white (D65) un, vn = xyz_to_uv(WP_DEFAULT) @@ -219,7 +219,7 @@ function MSC(h) col = ntuple(i -> i == p ? cpc : Float64(i == t), Val(3)) - return convert(LCHuv, RGB(col...)) + return convert(LCHuv, RGB{Float64}(col...)) end @@ -240,16 +240,15 @@ end # This function finds the maximum chroma for the lightness `c.l` and hue `c.h` # by means of the binary search. Even though this requires more than 20 # iterations, somehow, this is fast. -function find_maximum_chroma(c::C, - low::Real=0.0, - high::Real=180.0) where {T, C<:Union{LCHab{T}, LCHuv{T}}} +function find_maximum_chroma(c::C) where {T, C<:LCHuv{T}} + _find_maximum_chroma(c, convert(T, 0), convert(T, 180)) +end +function _find_maximum_chroma(c::C, low::T, high::T) where {T, C<:Union{LCHab{T}, LCHuv{T}}} err = convert(T, 1e-6) - l, h = convert(T, low), convert(T, high) + l, h = low, high while true - h - l < err && return l - mid = convert(T, (l + h) * oftype(l, 0.5)) - min(mid - l, h - mid) == zero(T) && return l + @fastmath min(mid - l, h - mid) < err && break lchm = C(c.l, mid, c.h) rgbm = xyz_to_linear_rgb(convert(XYZ{T}, lchm)) clamped = max(red(rgbm), green(rgbm), blue(rgbm)) > 1-err || @@ -257,13 +256,16 @@ function find_maximum_chroma(c::C, l = clamped ? l : mid h = clamped ? mid : h end + return l end const LAB_HUE_Y = hue(convert(Lab, RGB(1.0, 1.0, 0.0))) -function find_maximum_chroma(c::LCHab{T}) where T - maxc = find_maximum_chroma(c, 0.0, 135.0) - +function find_maximum_chroma(c::C) where {T, C<:LCHab{T}} + find_maximum_chroma(c, convert(T, 0), convert(T, 135)) +end +function find_maximum_chroma(c::C, low::T, high::T) where {T, C<:LCHab{T}} + maxc = _find_maximum_chroma(c, low, high) # The sRGB gamut in LCHab space has a *hollow* around the yellow corner. # Since the following boundary is based on the D65 white point, the values # should be modified on other conditions.