Fixed palettes() and linestyles()

This commit is contained in:
Giorgio Calderone 2020-03-18 14:41:41 +01:00
parent acb21294e0
commit ffea59dce7
4 changed files with 39 additions and 46 deletions

View File

@ -2,15 +2,14 @@ name = "Gnuplot"
uuid = "dc211083-a33a-5b79-959f-2ff34033469d" uuid = "dc211083-a33a-5b79-959f-2ff34033469d"
[deps] [deps]
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f" ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
ReusePatterns = "a39b5e78-89b5-562b-97d8-70689129df0c" ReusePatterns = "a39b5e78-89b5-562b-97d8-70689129df0c"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StructC14N = "d2514e9c-36c4-5b8e-97e2-51e7675c221c" StructC14N = "d2514e9c-36c4-5b8e-97e2-51e7675c221c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
ZipFile = "a5390f91-8eb1-5f08-bee0-b1d1ffed6cea"
[compat] [compat]
julia = "≥ 1.0.0" julia = "≥ 1.0.0"

View File

@ -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`). Note that the latter requires Sixel graphics to be enabled (e.g. `xterm -ti vt340`).
## Palettes ## Line styles and palettes
The **Gnuplot.jl** package comes with all the [Gnuplot palettes](https://github.com/Gnuplotting/gnuplot-palettes)) readily available. The **Gnuplot.jl** package comes with all the [ColorSchemes](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1) palettes readily available.
The whole list can be retrieved with `print(Gnuplot.palette_list())`.
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 ```julia
pal1 = Gnuplot.palette("rdylgn") pal1 = palette(:deepsea)
pal2 = Gnuplot.palette("viridis") pal2 = palette(:viridis)
x = -8:0.25:8 x = -8:0.25:8
y = -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 "set multiplot layout 1,2" "set pm3d depthorder" "set border 0"
@gsp :- "unset key" "unset xtics" "unset ytics" "unset ztics" "unset colorbox" @gsp :- "unset key" "unset xtics" "unset ytics" "unset ztics" "unset colorbox"
@gsp :- "set view 60, 30, 1.5, 0.9" @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
``` ```

View File

@ -1,2 +0,0 @@
[gnuplot-palettes]
git-tree-sha1 = "3ec13a0d8f63c6d012ec0e53d42abd85c91f75c5"

View File

@ -1,7 +1,7 @@
module Gnuplot module Gnuplot
using StructC14N, ColorTypes, Printf, StatsBase, ReusePatterns, DataFrames using StructC14N, ColorTypes, Printf, StatsBase, ReusePatterns, DataFrames
using Pkg.Artifacts, ZipFile using ColorSchemes
import Base.reset import Base.reset
import Base.println import Base.println
@ -9,7 +9,7 @@ import Base.iterate
import Base.convert import Base.convert
import Base.string import Base.string
export @gp, @gsp, save, contourlines, hist export @gp, @gsp, save, linestyles, palette, contourlines, hist
# ╭───────────────────────────────────────────────────────────────────╮ # ╭───────────────────────────────────────────────────────────────────╮
# │ TYPE DEFINITIONS │ # │ TYPE DEFINITIONS │
@ -60,7 +60,6 @@ Base.@kwdef mutable struct Options
end end
const sessions = Dict{Symbol, DrySession}() const sessions = Dict{Symbol, DrySession}()
const options = Options() const options = Options()
const dpalettes = Dict{String, String}()
# ╭───────────────────────────────────────────────────────────────────╮ # ╭───────────────────────────────────────────────────────────────────╮
# │ LOW LEVEL FUNCTIONS │ # │ 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) return string(float(c.r)*255) * " " * string(float(c.g)*255) * " " * string(float(c.b)*255)
end 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 │ # │ 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: Example: