Skip to content

Commit

Permalink
[WIP] Optimize colormap
Browse files Browse the repository at this point in the history
This uses `Tuple` instead of `Vector` for the colormap parameters.
This also unifies the type of preset values to `Float64`.
  • Loading branch information
kimikage committed Aug 3, 2021
1 parent f71d7a1 commit 204a31b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 162 deletions.
14 changes: 11 additions & 3 deletions docs/src/colormapsandcolorscales.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ This package provides some pre-defined colormaps (described below). There are al

The `colormap()` function returns a predefined sequential or diverging colormap computed using the algorithm by Wijffelaars, M., et al. (2008).

`colormap(cname::String [, N::Int=100; mid=0.5, logscale=false, kvs...])`
```julia
colormap(cname::String [, N::Int=100; mid=0.5, logscale=false, <keyword arguments>])
```

The optional arguments are:

Expand Down Expand Up @@ -167,7 +169,10 @@ colormap

You can create your own color palettes by using `sequential_palette()`:

`sequential_palette(h, [N::Int=100; c=0.88, s=0.6, b=0.75, w=0.15, d=0.0, wcolor=RGB(1,1,0), dcolor=RGB(0,0,1), logscale=false])`
```julia
sequential_palette(h, [N::Int=100; c=0.88, s=0.6, b=0.75, w=0.15, d=0.0,
wcolor=RGB(1,1,0), dcolor=RGB(0,0,1), logscale=false])
```

which creates a sequential map for a hue `h` (defined in LCHuv space).

Expand All @@ -185,7 +190,10 @@ Other possible parameters that you can fine tune are:

Two sequential maps can also be combined into a diverging colormap by using:

`diverging_palette(h1, h2 [, N::Int=100; mid=0.5,c=0.88, s=0.6, b=0.75, w=0.15, d1=0.0, d2=0.0, wcolor=RGB(1,1,0), dcolor1=RGB(1,0,0), dcolor2=RGB(0,0,1), logscale=false])`
```julia
diverging_palette(h1, h2 [, N::Int=100; mid=0.5,c=0.88, s=0.6, b=0.75, w=0.15, d1=0.0, d2=0.0,
wcolor=RGB(1,1,0), dcolor1=RGB(1,0,0), dcolor2=RGB(0,0,1), logscale=false])
```

where the arguments are:

Expand Down
31 changes: 15 additions & 16 deletions src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,36 +234,35 @@ function MSC(h, l; linear::Bool=false)
pend_l = l > pmid.l ? 100.0 : 0.0
return (pend_l - l) / (pend_l - pmid.l) * pmid.c
end
return find_maximum_chroma(LCHuv{Float64}(l, 0, h))
return find_maximum_chroma(LCHuv{Float64}(l, 0.0, h))
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,
high::Real=180) where {T, C<:Union{LCHab{T}, LCHuv{T}}}
low::Real=0.0,
high::Real=180.0) where {T, C<:Union{LCHab{T}, LCHuv{T}}}
err = convert(T, 1e-6)
l, h = convert(T, low), convert(T, high)
h - l < err && return l

mid = convert(T, (l + h) / 2)
min(mid - l, h - mid) == zero(T) && return l
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
if clamped
return find_maximum_chroma(c, l, mid)::T
else
return find_maximum_chroma(c, mid, h)::T
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
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
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, 135)
maxc = find_maximum_chroma(c, 0.0, 135.0)

# 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
Expand Down
Loading

0 comments on commit 204a31b

Please sign in to comment.