Skip to content

Commit

Permalink
[WIP] Modify MSC and find_maximum_chroma interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Aug 6, 2021
1 parent ae9e44f commit 7999272
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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


Expand All @@ -240,30 +240,32 @@ 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 ||
min(red(rgbm), green(rgbm), blue(rgbm)) <= 0
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.
Expand Down

0 comments on commit 7999272

Please sign in to comment.