I’ve seen a construct like (:red, 0.3)
to control the color and opacity separately. What I’d like to achieve here is to change the opacity of a given RGBA color. What’s the idiomatic way?
At the end of this message, I show a self-contained, simplified example (which currently uses a fake function that pretends to do what I want to do).
Probably, to people who are familiar with dealing with user-defined types (struct
s), this problem should be very simple. Currently, I don’t know how to (safely) copy a user-defined-type object. (Perhaps I should use copy()
?) I also don’t know how to find out the names of the fields. (What’s the name of the alpha channel? Julia includes powerful reflection, so this should be easy.) I also don’t know whether direct assignment to a field is recommended/allowed or not. (In some languages, you use setter functions, instead of allowing for direct assignments.) In some languages, there are idioms like newobj = setfield(oldobj, fieldname = val)
. . .
[In that sense, it may have been better to ask these questions in a different forum, but I just wondered whether there is an idiomatic color-specific way.]
using CairoMakie
"""
Fake: ignores the input color and returns a fixed color.
"""
function set_opacity(c, alpha)
return Makie.RGBA{Float32}(0.9019608f0,0.62352943f0,0.0f0,alpha)
end
xax = -π:(π/16):π
var1 = cos.(xax)
var2 = sin.(xax)
cs = Makie.wong_colors() # Vector of RGBAs
fig = Figure()
ax = Axis(fig[1,1])
lines!(ax, xax, var1; color=cs[2])
lines!(ax, xax, var2; color=set_opacity(cs[2], 0.3f0), linewidth=10)
fig