From ffea59dce7f9005adc9680edfa2958332ddb6f5d Mon Sep 17 00:00:00 2001 From: Giorgio Calderone Date: Wed, 18 Mar 2020 14:41:41 +0100 Subject: [PATCH] Fixed palettes() and linestyles() --- Project.toml | 3 +-- README.md | 22 ++++++++++++------ src/Artifacts.toml | 2 -- src/Gnuplot.jl | 58 ++++++++++++++++++---------------------------- 4 files changed, 39 insertions(+), 46 deletions(-) delete mode 100644 src/Artifacts.toml diff --git a/Project.toml b/Project.toml index 01b78d9..2f11056 100644 --- a/Project.toml +++ b/Project.toml @@ -2,15 +2,14 @@ name = "Gnuplot" uuid = "dc211083-a33a-5b79-959f-2ff34033469d" [deps] +ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" -Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" ReusePatterns = "a39b5e78-89b5-562b-97d8-70689129df0c" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StructC14N = "d2514e9c-36c4-5b8e-97e2-51e7675c221c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" -ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea" [compat] julia = "≥ 1.0.0" diff --git a/README.md b/README.md index 63dbe09..7b7527e 100644 --- a/README.md +++ b/README.md @@ -233,14 +233,13 @@ push!(Gnuplot.options.init, "set term sixelgd") Note that the latter requires Sixel graphics to be enabled (e.g. `xterm -ti vt340`). -## Palettes -The **Gnuplot.jl** package comes with all the [Gnuplot palettes](https://github.com/Gnuplotting/gnuplot-palettes)) readily available. -The whole list can be retrieved with `print(Gnuplot.palette_list())`. +## Line styles and palettes +The **Gnuplot.jl** package comes with all the [ColorSchemes](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1) palettes readily available. -Individual palette can be accessed with `Gnuplot.palette()` and used as any other command, as in the following example: +A gnuplot-compliant palette can be retrieved with `palette()` and used as any other command, as in the following example: ```julia -pal1 = Gnuplot.palette("rdylgn") -pal2 = Gnuplot.palette("viridis") +pal1 = palette(:deepsea) +pal2 = palette(:viridis) x = -8:0.25:8 y = -8:0.25:8 @@ -250,5 +249,14 @@ z = sin.(sqrt.(r)) ./ sqrt.(r) @gsp "set multiplot layout 1,2" "set pm3d depthorder" "set border 0" @gsp :- "unset key" "unset xtics" "unset ytics" "unset ztics" "unset colorbox" @gsp :- "set view 60, 30, 1.5, 0.9" -@gsp :- 1 title="rdylgn" pal1 x y z "w pm3d" 2 tit="viridis" pal2 x y z "w pm3d" +@gsp :- 1 title="deepsea (discrete)" pal1 x y z "w pm3d" 2 tit="viridis (continuous)" pal2 x y z "w pm3d" +``` + +The [ColorSchemes](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1) palettes can also be used to generate line styles, e.g. +```julia +@gp linestyles(:deepsea) +x = 1:0.1:4pi +for i in 1:5 + @gp :- x i.* sin.(x) "w l notit ls $i lw 10" +end ``` diff --git a/src/Artifacts.toml b/src/Artifacts.toml deleted file mode 100644 index c584df5..0000000 --- a/src/Artifacts.toml +++ /dev/null @@ -1,2 +0,0 @@ -[gnuplot-palettes] -git-tree-sha1 = "3ec13a0d8f63c6d012ec0e53d42abd85c91f75c5" diff --git a/src/Gnuplot.jl b/src/Gnuplot.jl index ebb092e..5c9c7cf 100644 --- a/src/Gnuplot.jl +++ b/src/Gnuplot.jl @@ -1,7 +1,7 @@ module Gnuplot using StructC14N, ColorTypes, Printf, StatsBase, ReusePatterns, DataFrames -using Pkg.Artifacts, ZipFile +using ColorSchemes import Base.reset import Base.println @@ -9,7 +9,7 @@ import Base.iterate import Base.convert import Base.string -export @gp, @gsp, save, contourlines, hist +export @gp, @gsp, save, linestyles, palette, contourlines, hist # ╭───────────────────────────────────────────────────────────────────╮ # │ TYPE DEFINITIONS │ @@ -60,7 +60,6 @@ Base.@kwdef mutable struct Options end const sessions = Dict{Symbol, DrySession}() const options = Options() -const dpalettes = Dict{String, String}() # ╭───────────────────────────────────────────────────────────────────╮ # │ LOW LEVEL FUNCTIONS │ @@ -70,38 +69,6 @@ function string(c::ColorTypes.RGB) return string(float(c.r)*255) * " " * string(float(c.g)*255) * " " * string(float(c.b)*255) end -# --------------------------------------------------------------------- -palette_list() = keys(dpalettes) -function palette(choice="") - if length(dpalettes) == 0 - function path() - name = "gnuplot-palettes" - toml = joinpath(@__DIR__, "Artifacts.toml") - hash = artifact_hash(name, toml) - if hash == nothing || !artifact_exists(hash) - hash = create_artifact() do artifact_dir - download("https://github.com/Gnuplotting/gnuplot-palettes/archive/master.zip", joinpath(artifact_dir, name * ".zip")) - end - bind_artifact!(toml, name, hash) - end - return joinpath(artifact_path(hash), name * ".zip") - end - - dir = ZipFile.Reader(path()) - out = Vector{String}() - for entry in dir.files - (_, file) = splitdir(entry.name) - (length(file) > 4) || continue - (file[end-3:end] == ".pal") || continue - file = file[1:end-4] - dpalettes[file] = join(Char.(read(entry))) - end - close(dir) - end - (choice in keys(dpalettes)) && (return dpalettes[choice]) - return " " -end -palette() # Populate dictionary # --------------------------------------------------------------------- """ @@ -1036,6 +1003,27 @@ save(sid::Symbol, file::AbstractString; kw...) = open(file, "w") do stream; dump # ╭───────────────────────────────────────────────────────────────────╮ # │ HIGH LEVEL FACILITIES │ # ╰───────────────────────────────────────────────────────────────────╯ +# --------------------------------------------------------------------- +linestyles(s::Symbol) = linestyles(colorschemes[s]) +function linestyles(cmap::ColorScheme) + styles = Vector{String}() + for i in 1:length(cmap.colors) + push!(styles, "set style line $i lt 1 lc rgb '#" * Base.hex(cmap.colors[i])) + end + return join(styles, "\n") +end + +# -------------------------------------------------------------------- +palette(s::Symbol) = palette(colorschemes[s]) +function palette(cmap::ColorScheme) + levels = Vector{String}() + for x in LinRange(0, 1, length(cmap.colors)) + color = get(cmap, x) + push!(levels, "$x '#" * Base.hex(color) * "'") + end + return "set palette defined (" * join(levels, ", ") * ")\nset palette maxcol $(length(cmap.colors))\n" +end + # -------------------------------------------------------------------- #= Example: