Skip to content

Commit

Permalink
change color symbols (Crayon)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Aug 19, 2021
1 parent bf8da8f commit 8608e52
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
30 changes: 18 additions & 12 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,18 @@ bordermap[:dashed] = border_dashed
bordermap[:dotted] = border_dotted
bordermap[:ascii] = border_ascii

const UserColorType = Union{Integer,Symbol,NTuple{3,Integer}}
const ColorType = Union{Nothing,UInt8}
const UserColorType = Union{Integer,Symbol,NTuple{3,Integer},Nothing} # allowed color type
const JuliaColorType = Union{Symbol,Int} # printstyle color type (defined in base/util.jl)
const ColorType = Union{Nothing,UInt8} # internal UnicodePlots color type

const color_cycle = [:green, :blue, :red, :magenta, :yellow, :cyan]

function print_color(color::Union{Nothing,Integer,Symbol}, io::IO, args...)
col = if color === nothing
:normal
elseif color isa Symbol
color
else
Int(color)
end
printstyled(io, string(args...); color = col)
function print_color(color::UserColorType, io::IO, args...)
printstyled(io, string(args...); color = julia_color(color))
end

function crayon_256_color(color::UserColorType)::ColorType
color in (:normal, :default) && return nothing
color in (:normal, :default, :nothing, nothing) && return nothing
ansicolor = Crayons._parse_color(color)
if ansicolor.style == Crayons.COLORS_16
return Crayons.val(ansicolor) % 60
Expand All @@ -194,6 +188,18 @@ function crayon_256_color(color::UserColorType)::ColorType
Crayons.val(ansicolor)
end

function julia_color(color::UserColorType)::JuliaColorType
if color isa Nothing
:normal
elseif color isa Symbol
color
elseif color isa Integer
Int(color)
else
julia_color(crayon_256_color(color))
end
end

@inline function set_color!(colors::Array{ColorType,2}, x::Int, y::Int, color::ColorType; force::Bool=false)
if color === nothing || colors[x, y] === nothing || force
colors[x, y] = color
Expand Down
22 changes: 11 additions & 11 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ mutable struct Plot{T<:GraphicsArea}
padding::Int
border::Symbol
labels_left::Dict{Int,String}
colors_left::Dict{Int,ColorType}
colors_left::Dict{Int,JuliaColorType}
labels_right::Dict{Int,String}
colors_right::Dict{Int,ColorType}
colors_right::Dict{Int,JuliaColorType}
decorations::Dict{Symbol,String}
colors_deco::Dict{Symbol,ColorType}
colors_deco::Dict{Symbol,JuliaColorType}
show_labels::Bool
colormap::Any
show_colorbar::Bool
Expand All @@ -109,11 +109,11 @@ function Plot(
rows = nrows(graphics)
cols = ncols(graphics)
labels_left = Dict{Int,String}()
colors_left = Dict{Int,ColorType}()
colors_left = Dict{Int,JuliaColorType}()
labels_right = Dict{Int,String}()
colors_right = Dict{Int,ColorType}()
colors_right = Dict{Int,JuliaColorType}()
decorations = Dict{Symbol,String}()
colors_deco = Dict{Symbol,ColorType}()
colors_deco = Dict{Symbol,JuliaColorType}()
Plot{T}(graphics, title, xlabel, ylabel, zlabel,
margin, padding, border,
labels_left, colors_left, labels_right, colors_right,
Expand Down Expand Up @@ -304,20 +304,20 @@ function annotate!(plot::Plot, loc::Symbol, value::AbstractString, color::UserCo
if loc == :l
if(!haskey(plot.labels_left, row) || plot.labels_left[row] == "")
plot.labels_left[row] = value
plot.colors_left[row] = crayon_256_color(color)
plot.colors_left[row] = julia_color(color)
return plot
end
elseif loc == :r
if(!haskey(plot.labels_right, row) || plot.labels_right[row] == "")
plot.labels_right[row] = value
plot.colors_right[row] = crayon_256_color(color)
plot.colors_right[row] = julia_color(color)
return plot
end
end
end
else
plot.decorations[loc] = value
plot.colors_deco[loc] = crayon_256_color(color)
plot.colors_deco[loc] = julia_color(color)
return plot
end
plot
Expand All @@ -330,10 +330,10 @@ end
function annotate!(plot::Plot, loc::Symbol, row::Int, value::AbstractString, color::UserColorType)
if loc == :l
plot.labels_left[row] = value
plot.colors_left[row] = crayon_256_color(color)
plot.colors_left[row] = julia_color(color)
elseif loc == :r
plot.labels_right[row] = value
plot.colors_right[row] = crayon_256_color(color)
plot.colors_right[row] = julia_color(color)
else
throw(ArgumentError("Unknown location \"$(string(loc))\", try `:l` or `:r` instead"))
end
Expand Down
4 changes: 2 additions & 2 deletions test/tst_canvas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ end
lines!(c, 0.0, 1.2, 1.2, 1.2, :white)

lines!(c, 0.0, 0.0, 0.0, 1.2, :blue)
lines!(c, 0.2, 0.0, 0.2, 1.2, (255,0,0))
lines!(c, 0.4, 0.0, 0.4, 1.2, 22) # 256 color green
lines!(c, 0.2, 0.0, 0.2, 1.2, :red)
lines!(c, 0.4, 0.0, 0.4, 1.2, :green)
lines!(c, 0.6, 0.0, 0.6, 1.2, :yellow)
lines!(c, 0.8, 0.0, 0.8, 1.2, :magenta)
lines!(c, 1.0, 0.0, 1.0, 1.2, :cyan)
Expand Down
2 changes: 1 addition & 1 deletion test/tst_lineplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ end
"references/lineplot/sincos_parameters.txt",
@io2str(show(IOContext(::IO, :color=>true), p))
)
p = @inferred lineplot([sin, cos], -.5, 3, name = ["s", "c"], color = [196, :yellow], title = "Funs", ylabel = "f", xlabel = "num", xlim = [-.5, 2.5], ylim = [-.9, 1.2])
p = @inferred lineplot([sin, cos], -.5, 3, name = ["s", "c"], color = [:red, :yellow], title = "Funs", ylabel = "f", xlabel = "num", xlim = [-.5, 2.5], ylim = [-.9, 1.2])
test_ref(
"references/lineplot/sincos_parameters.txt",
@io2str(show(IOContext(::IO, :color=>true), p))
Expand Down

0 comments on commit 8608e52

Please sign in to comment.