Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow band to pass the alpha attribute to its mesh #3613

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fixed bugs with format strings and add new features by switching to Format.jl [#3633](https://github.com/MakieOrg/Makie.jl/pull/3633).
- Fixed an issue where CairoMakie would unnecessarily rasterize polygons [#3605](https://github.com/MakieOrg/Makie.jl/pull/3605).
- Added `PointBased` conversion trait to `scatterlines` recipe [#3603](https://github.com/MakieOrg/Makie.jl/pull/3603).
- Fixed `band` not passing the `alpha` keyword to its mesh [#3612](https://github.com/MakieOrg/Makie.jl/pull/3612)

## [0.20.7] - 2024-02-04

Expand Down
51 changes: 37 additions & 14 deletions CairoMakie/src/overrides.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,29 @@ function draw_poly(scene::Scene, screen::Screen, poly, points, color, model, str
draw_poly_as_mesh(scene, screen, poly)
end

function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2})
color = to_cairo_color(poly.color[], poly)
"""
_retrieve_poly_calculated_colors(poly::Makie.Poly)

Polygon colors are passed down to the internal `mesh` and `lines`
plots which do the computation correctly, so we need to retrieve
them from there, since `poly` doesn't define a [`Makie.calculated_attributes!`](@ref) method!
"""
function _retrieve_poly_calculated_colors(poly::Makie.Poly)
color = if haskey(poly.plots[1].attributes, :calculated_colors)
to_cairo_color(poly.plots[1].calculated_colors[], poly)
else
to_cairo_color(poly.color[], poly)
end
strokecolor = to_cairo_color(poly.strokecolor[], poly)
# if haskey(poly.plots[2].attributes, :calculated_colors)
# to_cairo_color(poly.plots[2].calculated_colors[], poly)
# else
# to_cairo_color(poly.strokecolor[], poly)
# end
return (color, strokecolor)
end
function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2})
color, strokecolor = _retrieve_poly_calculated_colors(poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())
draw_poly(scene, screen, poly, points, color, poly.model[], strokecolor, strokestyle, poly.strokewidth[])
end
Expand All @@ -69,8 +89,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, points::Vector{<:Point2},
end

function draw_poly(scene::Scene, screen::Screen, poly, points_list::Vector{<:Vector{<:Point2}})
color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
color, strokecolor = _retrieve_poly_calculated_colors(poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())

broadcast_foreach(points_list, color,
Expand All @@ -87,7 +106,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, shapes::Vector{<:Union{Re
space = to_value(get(poly, :space, :data))
projected_shapes = project_shape.(Ref(poly), space, shapes, Ref(model))

color = to_cairo_color(poly.color[], poly)
color, strokecolor = _retrieve_poly_calculated_colors(poly)

linestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())
if isnothing(linestyle)
Expand All @@ -97,7 +116,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, shapes::Vector{<:Union{Re
else
error("Wrong type for linestyle: $(poly.linestyle[]).")
end
strokecolor = to_cairo_color(poly.strokecolor[], poly)

broadcast_foreach(projected_shapes, color, strokecolor, poly.strokewidth[]) do shape, c, sc, sw
create_shape_path!(screen.context, shape)
set_source(screen.context, c)
Expand Down Expand Up @@ -165,8 +184,7 @@ function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<
space = to_value(get(poly, :space, :data))
projected_polys = project_polygon.(Ref(poly), space, polygons, Ref(model))

color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
color, strokecolor = _retrieve_poly_calculated_colors(poly)
strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())

broadcast_foreach(projected_polys, color, strokecolor, strokestyle, poly.strokewidth[]) do po, c, sc, ss, sw
Expand All @@ -185,8 +203,8 @@ function draw_poly(scene::Scene, screen::Screen, poly, polygons::AbstractArray{<
space = to_value(get(poly, :space, :data))
projected_polys = project_multipolygon.(Ref(poly), space, polygons, Ref(model))

color = to_cairo_color(poly.color[], poly)
strokecolor = to_cairo_color(poly.strokecolor[], poly)
color, strokecolor = _retrieve_poly_calculated_colors(poly)

strokestyle = Makie.convert_attribute(poly.linestyle[], key"linestyle"())
broadcast_foreach(projected_polys, color, strokecolor, strokestyle, poly.strokewidth[]) do mpo, c, sc, ss, sw
for po in mpo.polygons
Expand All @@ -211,9 +229,14 @@ end

function draw_plot(scene::Scene, screen::Screen,
band::Band{<:Tuple{<:AbstractVector{<:Point2},<:AbstractVector{<:Point2}}})

if !(band.color[] isa AbstractArray)
color = to_cairo_color(band.color[], band)

# Similarly to what we do in `poly`, we retrieve the
# calculated colors from the internal `mesh` plot of `band`.
# See the `_retrieve_poly_calculated_colors` function for more details.
band_color = band.plots[1].calculated_colors[]

if !(band_color isa AbstractArray)
color = to_cairo_color(band_color, band)
upperpoints = band[1][]
lowerpoints = band[2][]
points = vcat(lowerpoints, reverse(upperpoints))
Expand Down Expand Up @@ -252,7 +275,7 @@ end
function draw_plot(scene::Scene, screen::Screen, tric::Tricontourf)

pol = only(tric.plots)::Poly
colornumbers = pol.color[]
colornumbers = pol.plots[1].calculated_colors[] # just as in `poly`, we retrieve the calculated colors from the internal `mesh` plot.
colors = to_cairo_color(colornumbers, pol)
polygons = pol[1][]
model = pol.model[]
Expand Down
8 changes: 8 additions & 0 deletions ReferenceTests/src/tests/examples2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1392,3 +1392,11 @@ end
ylims!(ax, 0, 1)
fig
end

@reference_test "transparency in band (#3356)" begin
f = Figure()
ax = Axis(f[1, 1])
band!(ax, 1:10, 1:10, 2:11)
band!(ax, reverse(1:10), 1:10, 2:11, alpha = 0.5)
f
end
Loading