Skip to content

Instantly share code, notes, and snippets.

@jkrumbiegel
Created June 9, 2022 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkrumbiegel/33a4e14753eb6db7e4ff1c204594e0b7 to your computer and use it in GitHub Desktop.
Save jkrumbiegel/33a4e14753eb6db7e4ff1c204594e0b7 to your computer and use it in GitHub Desktop.
Transparent GLMakie background
C_1 = C_a * α_a + C_b1 * (1 - α_a)
C_2 = C_a * α_a + C_b2 * (1 - α_a)
C_1 - C_2 = (C_b1 - C_b2) * (1 - α_a)
1 - (C_1 - C_2) / (C_b1 - C_b2) = α_a
(C_1 - (C_b1 * (1 - α_a))) / α_a = C_a
##
function alpha_colorbuffer(scene)
bg = scene.backgroundcolor[]
scene.backgroundcolor[] = RGBAf(0, 0, 0, 1)
b1 = copy(Makie.colorbuffer(scene))
scene.backgroundcolor[] = RGBAf(1, 1, 1, 1)
b2 = Makie.colorbuffer(scene)
scene.backgroundcolor[] = bg
return map(infer_alphacolor, b1, b2)
end
function infer_alphacolor(rgb1, rgb2)
rgb1 == rgb2 && return RGBAf(rgb1.r, rgb1.g, rgb1.b, 1)
c1 = Float64.((rgb1.r, rgb1.g, rgb1.b))
c2 = Float64.((rgb2.r, rgb2.g, rgb2.b))
alpha = @. 1 - (c1 - c2) * -1 # ( / (0 - 1))
meanalpha = clamp(sum(alpha) / 3, 0, 1)
meanalpha == 0 && return RGBAf(0, 0, 0, 0)
c = @. clamp((c1 / meanalpha), 0, 1)
return RGBAf(c..., meanalpha)
end
f, _ = scatter(randn(100, 3),
color = 1:100,
colormap = (:viridis, 0.5),
axis = (type = Axis3,),
markersize = 20
)
save("test_opaque.png", f.scene)
save("test_alpha.png", alpha_colorbuffer(f.scene))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment