Skip to content

Commit

Permalink
implement colorscale
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 22, 2022
1 parent 8c3815c commit 8cd0714
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 19 deletions.
7 changes: 3 additions & 4 deletions CairoMakie/src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,13 @@ function to_cairo_image(img::AbstractMatrix{<: AbstractFloat}, attributes)
end

function to_rgba_image(img::AbstractMatrix{<: AbstractFloat}, attributes)
Makie.@get_attribute attributes (colormap, colorrange, nan_color, lowclip, highclip)
transform = Makie.composed_transform_func(attributes)
Makie.@get_attribute attributes (colormap, colorrange, colorscale, nan_color, lowclip, highclip)
nan_color = Makie.to_color(nan_color)
lowclip = isnothing(lowclip) ? lowclip : Makie.to_color(lowclip)
highclip = isnothing(highclip) ? highclip : Makie.to_color(highclip)

colorrange = transform.(colorrange)
[get_rgba_pixel(pixel, colormap, colorrange, nan_color, lowclip, highclip) for pixel in transform.(img)]
colorrange = colorscale.(colorrange)
[get_rgba_pixel(pixel, colormap, colorrange, nan_color, lowclip, highclip) for pixel in colorscale.(img)]
end

to_rgba_image(img::AbstractMatrix{<: Colorant}, attributes) = RGBAf.(img)
Expand Down
21 changes: 21 additions & 0 deletions MakieCore/src/basic_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Plots an image on range `x, y` (defaults to dimensions).
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` is set by the plot.
- `colormap::Union{Symbol, Vector{<:Colorant}} = [:black, :white` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for the position of the image. See `Makie.spaces()` for possible inputs.
Expand All @@ -44,6 +45,7 @@ Plots an image on range `x, y` (defaults to dimensions).
Attributes(;
default_theme(scene)...,
colormap = [:black, :white],
colorscale = identity,
colorrange = automatic,
lowclip = automatic,
highclip = automatic,
Expand Down Expand Up @@ -80,6 +82,7 @@ Plots a heatmap as an image on `x, y` (defaults to interpretation as dimensions)
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` is set by the plot.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for the position of the heatmap. See `Makie.spaces()` for possible inputs.
Expand All @@ -88,6 +91,7 @@ Plots a heatmap as an image on `x, y` (defaults to interpretation as dimensions)
Attributes(;
default_theme(scene)...,
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
lowclip = automatic,
highclip = automatic,
Expand Down Expand Up @@ -132,6 +136,7 @@ Available algorithms are:
- `depth_shift::Float32 = 0f0` adjusts the depth value of a plot after all other transformations, i.e. in clip space, where `0 <= depth <= 1`. This only applies to GLMakie and WGLMakie and can be used to adjust render order (like a tunable overdraw).
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for box encompassing the volume plot. See `Makie.spaces()` for possible inputs.
Expand All @@ -151,6 +156,7 @@ Available algorithms are:
isorange = 0.05,
color = nothing,
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = (0, 1),
fxaa = true,
inspectable = theme(scene, :inspectable),
Expand Down Expand Up @@ -184,6 +190,8 @@ Plots a surface, where `(x, y)` define a grid whose heights are the entries in
- `depth_shift::Float32 = 0f0` adjusts the depth value of a plot after all other transformations, i.e. in clip space, where `0 <= depth <= 1`. This only applies to GLMakie and WGLMakie and can be used to adjust render order (like a tunable overdraw).
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for vertices generated by surface. See `Makie.spaces()` for possible inputs.
Expand All @@ -202,6 +210,7 @@ Plots a surface, where `(x, y)` define a grid whose heights are the entries in
backlight = 0f0,
color = nothing,
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
lowclip = automatic,
highclip = automatic,
Expand Down Expand Up @@ -242,6 +251,7 @@ Creates a connected line plot for each element in `(x, y, z)`, `(x, y)` or `posi
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` sets the color of the plot. It can be given as a named color `Symbol` or a `Colors.Colorant`. Transparency can be included either directly as an alpha value in the `Colorant` or as an additional float in a tuple `(color, alpha)`. The color can also be set for each point in the line by passing a `Vector` of colors or be used to index the `colormap` by passing a `Real` number or `Vector{<: Real}`.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for line position. See `Makie.spaces()` for possible inputs.
Expand All @@ -252,6 +262,7 @@ Creates a connected line plot for each element in `(x, y, z)`, `(x, y)` or `posi
linewidth = theme(scene, :linewidth),
color = theme(scene, :linecolor),
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
linestyle = nothing,
fxaa = false,
Expand Down Expand Up @@ -288,6 +299,7 @@ Plots a line for each pair of points in `(x, y, z)`, `(x, y)`, or `positions`.
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` sets the color of the plot. It can be given as a named color `Symbol` or a `Colors.Colorant`. Transparency can be included either directly as an alpha value in the `Colorant` or as an additional float in a tuple `(color, alpha)`. The color can also be set for each point in the line by passing a `Vector` or be used to index the `colormap` by passing a `Real` number or `Vector{<: Real}`.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for line position. See `Makie.spaces()` for possible inputs.
Expand Down Expand Up @@ -318,6 +330,7 @@ Plots a 3D or 2D mesh. Supported `mesh_object`s include `Mesh` types from [Geome
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` sets the color of the plot. It can be given as a named color `Symbol` or a `Colors.Colorant`. Transparency can be included either directly as an alpha value in the `Colorant` or as an additional float in a tuple `(color, alpha)`. A `Vector` of any of these can be passed to define the color per vertex. (It may be helpful to check `GeometryBasics.coordinates(my_mesh)` for this.) A `Vector{<: Real}` can also be passed to sample a colormap for each vertex. And finally, if the mesh includes uv coordinates you can pass a `Matrix` of colors to be used as a texture.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for vertex positions. See `Makie.spaces()` for possible inputs.
Expand All @@ -339,6 +352,7 @@ Plots a 3D or 2D mesh. Supported `mesh_object`s include `Mesh` types from [Geome
color = :black,
backlight = 0f0,
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
lowclip = automatic,
highclip = automatic,
Expand Down Expand Up @@ -385,6 +399,7 @@ Plots a marker for each element in `(x, y, z)`, `(x, y)`, or `positions`.
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` sets the color of the plot. It can be given as a named color `Symbol` or a `Colors.Colorant`. Transparency can be included either directly as an alpha value in the `Colorant` or as an additional float in a tuple `(color, alpha)`. The color can also be set for each scattered marker by passing a `Vector` of colors or be used to index the `colormap` by passing a `Real` number or `Vector{<: Real}`.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for positions of markers. See `Makie.spaces()` for possible inputs.
Expand All @@ -394,6 +409,7 @@ Plots a marker for each element in `(x, y, z)`, `(x, y)`, or `positions`.
default_theme(scene)...,
color = theme(scene, :markercolor),
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
marker = theme(scene, :marker),
markersize = theme(scene, :markersize),
Expand Down Expand Up @@ -444,6 +460,7 @@ Plots a mesh for each element in `(x, y, z)`, `(x, y)`, or `positions` (similar
- `model::Makie.Mat4f` sets a model matrix for the plot. This replaces adjustments made with `translate!`, `rotate!` and `scale!`.
- `color` sets the color of the plot. It can be given as a named color `Symbol` or a `Colors.Colorant`. Transparency can be included either directly as an alpha value in the `Colorant` or as an additional float in a tuple `(color, alpha)`. The color can also be set for each scattered mesh by passing a `Vector` of colors or be used to index the `colormap` by passing a `Real` number or `Vector{<: Real}`.
- `colormap::Union{Symbol, Vector{<:Colorant}} = :viridis` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for the positions of meshes. See `Makie.spaces()` for possible inputs.
Expand All @@ -461,6 +478,7 @@ Plots a mesh for each element in `(x, y, z)`, `(x, y)`, or `positions` (similar
default_theme(scene)...,
color = :black,
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
marker = :Sphere,
markersize = 0.1,
Expand Down Expand Up @@ -570,6 +588,7 @@ Plots polygons, which are defined by
- `inspectable::Bool = true` sets whether this plot should be seen by `DataInspector`.
- `color` is set by the plot.
- `colormap::Union{Symbol, Vector{<:Colorant}} = [:black, :white` sets the colormap that is sampled for numeric `color`s.
- `colorscale::Function = identity` color transform function.
- `colorrange::Tuple{<:Real, <:Real}` sets the values representing the start and end points of `colormap`.
- `nan_color::Union{Symbol, <:Colorant} = RGBAf(0,0,0,0)` sets a replacement color for `color = NaN`.
- `space::Symbol = :data` sets the transformation space for the position of the image. See `Makie.spaces()` for possible inputs.
Expand All @@ -582,6 +601,7 @@ Plots polygons, which are defined by
visible = theme(scene, :visible),
strokecolor = theme(scene, :patchstrokecolor),
colormap = theme(scene, :colormap),
colorscale = identity,
colorrange = automatic,
lowclip = automatic,
highclip = automatic,
Expand Down Expand Up @@ -620,6 +640,7 @@ end
normalize = false,
lengthscale = 1f0,
colormap = theme(scene, :colormap),
colorscale = identity,
quality = 32,
inspectable = theme(scene, :inspectable),
markerspace = :pixel,
Expand Down
2 changes: 1 addition & 1 deletion precompile/shared-precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ end
end

@compile begin
heatmap(rand(10, 5), axis = (yscale = log10, xscale=log10))
heatmap(1 .+ rand(10, 5), axis = (yscale = log10, xscale=log10))
end

@compile begin
Expand Down
8 changes: 4 additions & 4 deletions src/colorsampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,20 @@ end
function numbers_to_colors(numbers::AbstractArray{<:Number}, primitive)
colormap = get_attribute(primitive, :colormap)::Vector{RGBAf}
colorrange = get_attribute(primitive, :colorrange)::Union{Nothing, Vec2f}
transform = composed_transform_func(primitive)
colorscale = get_attribute(primitive, :colorscale)
cmin, cmax = if isnothing(colorrange)
# TODO, plot primitive should always expand automatic values
transform.(Vec2f(extrema_nan(numbers)))
colorscale.(Vec2f(extrema_nan(numbers)))
else
transform.(colorrange)
colorscale.(colorrange)
end

lowclip = get_attribute(primitive, :lowclip)
highclip = get_attribute(primitive, :highclip)
nan_color = get_attribute(primitive, :nan_color, RGBAf(0,0,0,0))

return map(numbers) do number
scaled_number = transform(Float64(number)) # ints don't work in interpolated_getindex
scaled_number = colorscale(Float64(number)) # ints don't work in interpolated_getindex
if isnan(scaled_number)
return nan_color
elseif !isnothing(lowclip) && scaled_number < cmin
Expand Down
10 changes: 0 additions & 10 deletions src/layouting/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,6 @@ transformationmatrix(x) = transformation(x).model
transform_func(x) = transform_func_obs(x)[]
transform_func_obs(x) = transformation(x).transform_func

reduce_transform(x::Tuple) = reduce(, x)
reduce_transform(x) = x

"""
composed_transform_func(x)
Returns transformation composition e.g. log10 ∘ identity for xscale=log10 in 2D.
"""
composed_transform_func(x) = reduce_transform(transform_func(x))

"""
apply_transform(f, data, space)
Apply the data transform func to the data if the space matches one
Expand Down
1 change: 1 addition & 0 deletions src/makielayout/blocks/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function Colorbar(fig_or_scene, heatmap::Union{Heatmap, Image}; kwargs...)
limits = heatmap.colorrange,
highclip = heatmap.highclip,
lowclip = heatmap.lowclip,
scale = heatmap.colorscale,
kwargs...
)
end
Expand Down

0 comments on commit 8cd0714

Please sign in to comment.