-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathColorsExt.jl
47 lines (39 loc) · 1.5 KB
/
ColorsExt.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module ColorsExt
import PGFPlotsX
PGFPlotsX.EXTENSIONS_SUPPORTED ? (using Colors) : (using ..Colors)
function _rgb_for_printing(c::Colors.Colorant)
rgb = convert(Colors.RGB{Float64}, c)
# round colors since pgfplots cannot parse scientific notation, eg 1e-10
round.((Colors.red(rgb), Colors.green(rgb), Colors.blue(rgb)); digits = 4)
end
function PGFPlotsX.print_opt(io::IO, c::Colors.Colorant)
rgb_64 = _rgb_for_printing(c)
print(io, "rgb,1:",
"red," , rgb_64[1], ";",
"green,", rgb_64[2], ";",
"blue," , rgb_64[3])
end
# For printing surface plots with explicit color, pgfplots manual 4.6.7.
# If there are any other uses outside options that need a different format,
# we should introduce a wrapper type.
function PGFPlotsX.print_tex(io::IO, c::Colors.Colorant)
rgb_64 = _rgb_for_printing(c)
print(io, "rgb=", rgb_64[1], ",", rgb_64[2], ",", rgb_64[3])
end
function PGFPlotsX.print_tex(io::IO, c::Tuple{String, Colors.Colorant}, ::Any)
name, color = c
rgb_64 = _rgb_for_printing(color)
print(io, "\\definecolor{$name}{rgb}{$(rgb_64[1]), $(rgb_64[2]), $(rgb_64[3])}")
end
function PGFPlotsX.print_tex(io::IO,
c::Tuple{String, Vector{<:Colors.Colorant}},
::Any)
name, colors = c
println(io, "\\pgfplotsset{ colormap={$name}{")
for col in colors
rgb_64 = _rgb_for_printing(col)
println(io, "rgb=(", join(rgb_64, ","), ")")
end
println(io, "}}")
end
end