Skip to content

Commit

Permalink
Feature to include RGBA colormaps, updated to 3.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mossr committed Aug 28, 2024
1 parent fb66c13 commit 4b78e66
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PGFPlots"
uuid = "3b7a836e-365b-5785-a47d-02c71176b4aa"
repo = "https://github.com/JuliaTeX/PGFPlots.jl.git"
version = "3.4.4"
version = "3.4.5"

[deps]
ColorBrewer = "a2cac450-b92f-5266-8821-25eda20663c8"
Expand Down
25 changes: 20 additions & 5 deletions src/PGFPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export plot, tikzCode, ErrorBars, Axis, Axes, PolarAxis, SmithAxis, TernaryAxis,
export pushPGFPlotsOptions, popPGFPlotsOptions, resetPGFPlotsOptions, pgfplotsoptions
export pushPGFPlotsPreamble, popPGFPlotsPreamble, resetPGFPlotsPreamble, pgfplotspreamble
export pushPGFPlots, popPGFPlots
import Colors: RGB
import Colors: RGB, RGBA
import Contour: contours, levels

using Requires
Expand Down Expand Up @@ -93,6 +93,10 @@ function define_color(name::AbstractString, color::RGB)
_pgfplotspreamble[end] = _pgfplotspreamble[end] * "\n\\definecolor{$name}{rgb}{$(color.r), $(color.g), $(color.b)}"
end

function define_color(name::AbstractString, color::RGBA)
_pgfplotspreamble[end] = _pgfplotspreamble[end] * "\n\\definecolor{$name}{rgba}{$(color.r), $(color.g), $(color.b), $(color.alpha)}"
end

define_color(name::AbstractString, color) = define_color(name, convert(RGB, color))

function define_color(name::AbstractString, color::Vector{T}) where {T<:AbstractFloat}
Expand All @@ -108,9 +112,15 @@ function define_color(name::AbstractString, color::Vector{T}) where {T<:Abstract
end

function define_color(name::AbstractString, color::Vector{T}) where {T<:Integer}
@assert(length(color) == 3, "Color must have three components")
r, g, b = color[1], color[2], color[3]
_pgfplotspreamble[end] = _pgfplotspreamble[end] * "\n\\definecolor{$name}{RGB}{$r, $g, $b}"
if length(color) == 3
r, g, b = color
_pgfplotspreamble[end] = _pgfplotspreamble[end] * "\n\\definecolor{$name}{RGB}{$r, $g, $b}"
elseif length(color) == 4
r, g, b, alpha = color
_pgfplotspreamble[end] = _pgfplotspreamble[end] * "\n\\definecolor{$name}{RGBA}{$r, $g, $b, $alpha}"
else
error("The color vector should be either [r,g,b] or [r,g,b,alpha].")
end
end

function define_color(name::AbstractString, color::UInt32)
Expand Down Expand Up @@ -845,9 +855,14 @@ function colormapOptions(cm::ColorMaps.RGBArrayMap)
o = IOBuffer()
print(o, "colormap={mycolormap}{ ")
n = length(cm.colors)
T = typeof(cm.colors)
for i = 1:n
c = cm.colors[i]
print(o, "rgb($(i-1)cm)=($(c.r),$(c.g),$(c.b)) ")
if T <: Vector{RGB}
print(o, "rgb($(i-1)cm)=($(c.r),$(c.g),$(c.b)) ")
elseif T <: Vector{RGBA}
print(o, "rgba($(i-1)cm)=($(c.r),$(c.g),$(c.b),$(c.alpha)) ")
end
end
print(o, "}")
String(take!(o))
Expand Down
14 changes: 9 additions & 5 deletions src/colormaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module ColorMaps
using ColorSchemes
export ColorMap, GrayMap, Brew, RGBArrayMap, Distinguishable, SparseDistinguishable, write
import Images: colorview, save, Gray, ImageMeta, clamp01nan
import Colors: RGB, distinguishable_colors, colormap
import Colors: RGB, RGBA, distinguishable_colors, colormap
import ColorBrewer: palette

abstract type ColorMap end
Expand All @@ -13,10 +13,12 @@ mutable struct GrayMap <: ColorMap
GrayMap(;invert = false) = new(invert)
end

const ColorVector = AbstractVector{<:Union{RGB{Float64},RGBA{Float64}}}

mutable struct RGBArrayMap <: ColorMap
colors::Vector{RGB{Float64}}
colors::ColorVector
interpolation_levels::UInt
function RGBArrayMap(colors::AbstractVector{RGB{Float64}}; invert=false, interpolation_levels=0)
function RGBArrayMap(colors::ColorVector; invert=false, interpolation_levels=0)
if invert
colors = reverse(colors)
end
Expand All @@ -37,7 +39,8 @@ function Distinguishable(n::Integer; invert=false)
end

function SparseDistinguishable(n, nonzeroidx; zerocolor = RGB{Float64}(1.,1.,1.))
m = RGB{Float64}[zerocolor for i = 1:n]
T = typeof(zerocolor)
m = T[zerocolor for i = 1:n]
sm = ColorMaps.Distinguishable(length(nonzeroidx))
for i = 1:length(sm.colors)
m[nonzeroidx[i]] = sm.colors[i]
Expand Down Expand Up @@ -71,7 +74,8 @@ function interpolate_RGBArrayMap(colormap::RGBArrayMap)
end

n = length(colormap.colors)
colors = Vector{RGB{Float64}}(undef,levels)
T = typeof(colormap.colors)
colors = T(undef,levels)
for (i,x) in enumerate(range(0.0,stop=1.0,length=levels))
t = 1 + x*(n-1)
a = colormap.colors[floor(Int, t)]
Expand Down

2 comments on commit 4b78e66

@mossr
Copy link
Member Author

@mossr mossr commented on 4b78e66 Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/113987

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.4.5 -m "<description of version>" 4b78e6687216e9e55f94581a6a69be2dde9e5767
git push origin v3.4.5

Please sign in to comment.