Color opacity

I have a variable that I would like to assign the color red to it simply by using
RGB{Float64}(1, 0, 0) but I’d like the absolute value of the variable to determine the opacity of the color. How can one change the color opacity in Julia?

Opacity is represented by the A component of an RGBA value. For an opaque red, you’d use RGBA{Float64}(1, 0, 0, 1).

Thanks for your reply. I don’t seem to get a transparent color when the A component is zero though. like I tried this simple test scatter([1,2],[1,2],color=[RGBA{Float64}(1,0,0,1),RGBA{Float64}(1,0,0,0)],legends=false) where the point (2,2) should be transparent but it comes out same as (1,1).

Looks like transparency might be buggy for the GR.jl backend. Try switching to the PyPlot backend:

julia> Pkg.add("PyPlot"); pyplot()

With GR, use markeralpha to set alphas separately from the color

scatter([1,2],[1,2],markercolor=[RGB{Float64}(1,0,0),RGB{Float64}(1,1,0)],markeralpha=[1,0],legends=false)

You can do this more conveniently as:

using Plots; gr()
fc = [RGBA(1,0,0,1.0), RGBA(1,0,0,0.1)]
scatter([1,2], [1,2], c=fc, alpha=fc, legend=false)

3 Likes