Skip to content

Commit

Permalink
[WIP] Improve inference
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Aug 30, 2021
1 parent 0387249 commit 32a2ed1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/algorithms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function MSC(h::Float64)

col = ntuple(i -> i == p ? cpc : Float64(i == t), Val(3))

return convert(LCHuv, RGB{Float64}(col...))
return convert(LCHuv{Float64}, RGB{Float64}(col...))
end


Expand Down
15 changes: 9 additions & 6 deletions src/colormaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ function _diverging_palette(N, mid, logscale, h1, h2, w, d1, d2, c, s, b, wcolor
pal2 = _sequential_palette(N2 + 1, logscale, h2, w, d2, c, s, b, wcolor, dcolor2)

if isodd(N)
midcol = weighted_color_mean(0.5, pal1[1], pal2[1])
return @views vcat(pal1[end:-1:2], midcol, pal2[2:end])
pal2[1] = weighted_color_mean(0.5, pal1[1], pal2[1])
return @views vcat(pal1[end:-1:2], pal2[1:end])
else
return @views vcat(pal1[end:-1:2], pal2[2:end])
end
Expand Down Expand Up @@ -302,12 +302,15 @@ You can also use keyword argument names that match the argument names in
function colormap(cname::AbstractString, N::Integer=100; kvs...)
_colormap(String(cname), Int(N), kvs)
end

function _colormap(cname::String, N::Int, kvs)
logscale = get(kvs, :logscale, false)::Bool
lcname = lowercase(cname)
F = Float64
if haskey(colormaps_sequential, lcname)
pbs = colormaps_sequential[lcname]
keys_s = (:h, :w, :d, :c, :s, :b, :wcolor, :dcolor)
Ts = Tuple{F, F, F, F, F, F, RGB{F}, RGB{F}}
pbs = colormaps_sequential[lcname]::Ts
for k in keys(kvs)
k === :logscale && continue
k in keys_s || throw(ArgumentError("Unknown keyword argument: $k"))
Expand All @@ -316,17 +319,17 @@ function _colormap(cname::String, N::Int, kvs)
return _sequential_palette(N, logscale, (ps(i) for i in eachindex(pbs))...)
end
if haskey(colormaps_diverging, lcname)
pbd = colormaps_diverging[lcname]
keys_d = (:h1, :h2, :w, :d1, :d2, :c, :s, :b, :wcolor, :dcolor1, :dcolor2)
Td = Tuple{F, F, F, F, F, F, F, F, RGB{F}, RGB{F}, RGB{F}}
pbd = colormaps_diverging[lcname]::Td
for k in keys(kvs)
k === :logscale && continue
k === :mid && continue
k in keys_d || throw(ArgumentError("Unknown keyword argument: $k"))
end
mid = Float64(get(kvs, :mid, 0.5))
pd(i) = oftype(pbd[i], get(kvs, keys_d[i], pbd[i]))
params = Tuple(pd(i) for i in eachindex(pbd))::typeof(pbd)
return _diverging_palette(N, mid, logscale, params...)
return _diverging_palette(N, mid, logscale, (pd(i) for i in eachindex(pbd))...)
end
throw(ArgumentError(string("Unknown colormap: ", cname)))
end
4 changes: 3 additions & 1 deletion src/conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ convert(::Type{XYZ{T}}, c, wp::XYZ) where {T} = cnvt(XYZ{T}, c, wp)
convert(::Type{Lab{T}}, c, wp::XYZ) where {T} = cnvt(Lab{T}, c, wp)
convert(::Type{Luv{T}}, c, wp::XYZ) where {T} = cnvt(Luv{T}, c, wp)

# FIXME: inference helpers for LCH --> RGB conversions
# FIXME: inference helpers for LCH <--> RGB conversions
convert(::Type{RGB}, c::Union{LCHab{T}, LCHuv{T}}) where {T} = cnvt(RGB{T}, cnvt(XYZ{T}, c))
convert(::Type{RGB{T}}, c::Union{LCHab{T}, LCHuv{T}}) where {T} = cnvt(RGB{T}, cnvt(XYZ{T}, c))
convert(::Type{Lab{T}}, c::RGB{T}) where {T} = cnvt(Lab{T}, cnvt(XYZ{T}, c))
convert(::Type{Luv{T}}, c::RGB{T}) where {T} = cnvt(Luv{T}, cnvt(XYZ{T}, c))

# Fallback to catch undefined operations
cnvt(::Type{C}, c::TransparentColor) where {C<:Color} = cnvt(C, color(c))
Expand Down
6 changes: 4 additions & 2 deletions src/maps_data.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Colormap parameters

const colormaps_sequential = Dict(
# To avoid over-specialization, we use `Any` as the value type,
# but the actual type should be exactly as used here.
const colormaps_sequential = Dict{String, Any}(
#single hue
#name hue w d c s b wcolor dcolor
"blues" => (255.0, 0.3, 0.25, 0.88, 0.6, 0.75, RGB(1.0, 1.0, 0.0), RGB(0.0, 0.0, 1.0)),
Expand All @@ -11,7 +13,7 @@ const colormaps_sequential = Dict(
"reds" => ( 12.0, 0.15, 0.25, 0.8, 0.85, 0.6, RGB(1.0, 1.0, 0.0), RGB(0.3, 0.1, 0.1)),
)

const colormaps_diverging = Dict(
const colormaps_diverging = Dict{String, Any}(
#name h1 h2 w d1 d2 c s b wcolor dcolor dcolor2
"rdbu" => ( 12.0, 255.0, 0.2, 0.6, 0.0, 0.85, 0.6, 0.65, RGB(1.0, 1.0, 0.0), RGB(1.0, 0.0, 0.0), RGB(0.0, 0.0, 1.0)),
)

0 comments on commit 32a2ed1

Please sign in to comment.