Skip to content

Commit

Permalink
add type assertions (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg authored Dec 5, 2022
1 parent 089d025 commit b89aec2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
40 changes: 19 additions & 21 deletions src/color_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
# background color and a short list of seed colors

# here are some magic constants that could be changed if you really want
const _lightness_darkbg = [80.0]
const _lightness_lightbg = [60.0]
const _lch_c_const = [60]
const _lightness_darkbg = Ref(80.0)
const _lightness_lightbg = Ref(60.0)
const _lch_c_const = Ref(60)

adjust_lch(color, l, c) = convert(RGBA{Float64}, LCHab(l, c, convert(LCHab, color).h))

function lightness_from_background(bgcolor)
bglight = convert(LCHab, bgcolor).l
bglight < 50.0 ? _lightness_darkbg[1] : _lightness_lightbg[1]
bglight < 50.0 ? _lightness_darkbg[] : _lightness_lightbg[]
end

function generate_colorscheme(
bgcolor = plot_color(:white);
color_bases = plot_color([colorant"steelblue", colorant"orangered"]),
lightness = lightness_from_background(bgcolor),
chroma = _lch_c_const[1],
chroma = _lch_c_const[],
n = 17,
)
seed_colors = vcat(bgcolor, map(c -> adjust_lch(c, lightness, chroma), color_bases))
Expand All @@ -37,7 +37,7 @@ end

# ----------------------------------------------------------------------------------

function getpctrange(n::Int)
function getpctrange(n::Integer)
n > 0 || error()
n == 1 && return zeros(1)
zs = [0.0, 1.0]
Expand All @@ -52,12 +52,12 @@ function getpctrange(n::Int)
widestj = j
end
end
push!(zs, sorted[widestj] + 0.5 * diffs[widestj])
push!(zs, sorted[widestj] + 0.5diffs[widestj])
end
zs
end

function get_zvalues(n::Int)
function get_zvalues(n::Integer)
offsets = getpctrange(ceil(Int, n / 4) + 1) / 4
offsets = vcat(offsets[1], offsets[3:end])
zvalues = Float64[]
Expand All @@ -79,25 +79,23 @@ end
lighten(c, v = 0.3) = darken(c, -v)

# borrowed from http://stackoverflow.com/a/1855903:
lightness_level(c::Colorant) = 0.299 * red(c) + 0.587 * green(c) + 0.114 * blue(c)
lightness_level(c::Colorant) = 0.299red(c) + 0.587green(c) + 0.114blue(c)

isdark(c::Colorant) = lightness_level(c) < 0.5
islight(c::Colorant) = !isdark(c)
isdark(c::Colorant)::Bool = lightness_level(c) < 0.5
islight(c::Colorant)::Bool = !isdark(c)

Base.convert(::Type{RGB}, h::Unsigned) =
RGB([(x & 0x0000FF) / 0xFF for x in (h >> 16, h >> 8, h)]...)

make255(x) = round(Int, 255 * x)
make255(x)::Int = round(Int, 255 * x)

rgb_string(c::Colorant) =
@sprintf("rgb(%d, %d, %d)", make255(red(c)), make255(green(c)), make255(blue(c)))

function rgba_string(c::Colorant)
@sprintf(
"rgba(%d, %d, %d, %1.3f)",
make255(red(c)),
make255(green(c)),
make255(blue(c)),
alpha(c)
)
end
rgba_string(c::Colorant) = @sprintf(
"rgba(%d, %d, %d, %1.3f)",
make255(red(c)),
make255(green(c)),
make255(blue(c)),
alpha(c)
)
4 changes: 3 additions & 1 deletion src/colors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ plot_color(s::Symbol) =
catch e
is_colorscheme(s) ? cgrad(s) : rethrow(e)
end
plot_color(b::Bool) = b ? error("plot_color(true) not allowed.") : invisible()
plot_color(b::Val{true}) = error("plot_color(true) not allowed.")
plot_color(b::Val{false}) = invisible()
plot_color(b::Bool) = plot_color(Val(b))
plot_color(::Nothing) = invisible()
plot_color(c::Colorant) = convert(RGBA{Float64}, c)
# plot_color(cs::AbstractVector) = RGBA{Float64}[plot_color(c) for c in cs]
Expand Down
2 changes: 1 addition & 1 deletion src/ticks.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const _logScales = [:ln, :log2, :log10]
const _logScales = (:ln, :log2, :log10)
const _logScaleBases = Dict(:ln => ℯ, :log2 => 2.0, :log10 => 10.0)

# NOTE: This file was moved from Gadfly.jl, and the original author is Daniel Jones (@dcjones)
Expand Down

0 comments on commit b89aec2

Please sign in to comment.