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

support Makie.AbstractPattern as color in CairoMakie for non-mesh plots #2106

Merged
merged 14 commits into from
Jul 4, 2022

Conversation

Moelf
Copy link
Contributor

@Moelf Moelf commented Jul 2, 2022

Demo

julia> p = Pattern('/');

julia> save("/tmp/f.png", poly(Point2f[(0, 0), (2, 0), (3, 1), (1, 1)], color = p, strokecolor=:red, strokewidth=2))
using CairoMakie, Random
Random.seed!(3)
const Vec2f0 = CairoMakie.Vec2f0
# patterns
# `'/'`, `'\\'`, `'-'`, `'|'`, `'x'`, and `'+'`
directions = [Vec2f0(1), Vec2f0(1, -1), Vec2f0(1, 0), Vec2f0(0, 1),
    [Vec2f0(1), Vec2f0(1, -1)], [Vec2f0(1, 0), Vec2f0(0, 1)]]
colors = [:white, :red, (:green, 0.5), :white, (:navy, 0.85),:black]
patterns = [Makie.LinePattern(direction= hatch; width = 5, tilesize=(20,20),
    linecolor = colors[indx], background_color = colors[end-indx+1])
    for (indx, hatch) in enumerate(directions)]
fig = Figure(resolution = (1200,800), fontsize = 32)

ax = Axis(fig[1,1])
for (idx, pattern) in enumerate(patterns)
    barplot!(ax, [idx], [idx*(2rand()+1)], color = pattern, strokewidth = 2)
end
ax.xticks = (1:6, ["/", "\\", "-", "|", "x", "+"])
save("/tmp/strippedPatterns.png", fig)
first second
image strippedPatterns

Description

fix #1385

Type of change

Delete options that do not apply:

  • New feature (non-breaking change which adds functionality)

Checklist

  • Added an entry in NEWS.md (for new features and breaking changes)
  • Added or changed relevant sections in the documentation [NO NEED]
  • Added unit tests for new algorithms, conversion methods, etc.
  • Added reference image tests for new plotting functions, recipes, visual options, etc. [ALREADY PRESENT]

@Moelf Moelf changed the title support poly fill color being Makie.AbstractPattern support Makie.AbstractPattern as color in CairoMakie for non-mesh plots Jul 3, 2022
@Moelf Moelf requested a review from SimonDanisch July 3, 2022 14:18
Copy link
Member

@asinghvi17 asinghvi17 left a comment

Choose a reason for hiding this comment

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

I will try this out soon but it looks good so far! Thanks for the PR :)

CairoMakie/src/utils.jl Outdated Show resolved Hide resolved
Cairo.set_source_rgba(screen.context, rgbatuple(to_color(color))...)
if color isa Makie.AbstractPattern
cairopattern = Cairo.CairoPattern(color)
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
Copy link
Member

Choose a reason for hiding this comment

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

You should also set the filter so that Cairo doesn't interpolate...

Suggested change
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
Cairo.pattern_set_extend(cairopattern, Cairo.EXTEND_REPEAT);
Cairo.pattern_set_filter(cairopattern, Cairo.FILTER_NEAREST); # or something like this anyway

Copy link
Member

Choose a reason for hiding this comment

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

not sure, why no interpolation? should make diagonal line patterns look better no

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense - was looking at it from the perspective of ImagePattern, where you may not want the pattern to be interpolated. Maybe we could make this user-configurable somehow, perhaps by checking the interpolate keyword...

NEWS.md Outdated Show resolved Hide resolved
CairoMakie/src/utils.jl Show resolved Hide resolved
Copy link
Member

@asinghvi17 asinghvi17 left a comment

Choose a reason for hiding this comment

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

Makes sense to me, and looks good so far!

This looks mergeable to me.

At a later point I would like to have a vectorized version of LinePattern in CairoMakie, but that sounds like something for a different PR!

As for the Cairo recording surface: basically, one can draw on it using cairo_move_to or cairo_line_to, and then "replay" it onto a surface. It would be something like drawing lines across the surface in a pattern and the rest of the code would likely remain the same.

@Moelf Moelf merged commit ba73c43 into MakieOrg:master Jul 4, 2022
@Moelf Moelf deleted the pattern_for_cairo_backend branch July 4, 2022 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Patterned bar plots in CairoMakie
3 participants