控制 n 个重叠区域的 alpha 混合/不透明度 [英] Control alpha blending / opacity of n overlapping areas

查看:18
本文介绍了控制 n 个重叠区域的 alpha 混合/不透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解(和控制)alpha 的混合 - 不幸的是,alpha 值不会简单地加起来"(0.5 + 0.5 不是 1).但我怎么能做到这一点?

I struggle to understand (and control) the blending of alphas - unfortunately, alpha values don't simply "add up" (0.5 + 0.5 is not 1). But how could I achieve that?

目的是定义重叠区域相对于观察总数的(绝对)灰度值.参见下面的示例.

The aim is to define the (absolute) grey value of overlapping areas relative to the total number of observations. See example below.

我试图设置 scale_alpha(range = c(0,1)) 无济于事,也许我没有正确使用它.

I tried to set scale_alpha(range = c(0,1)) to no avail, maybe I did not use it correctly.

library(ggplot2)
library(ggforce)

grid_df = data.frame(x = c(1:2, 2.5), y = rep(1,3), r = 1)

ggplot()+
geom_circle(data = grid_df, mapping = aes(x0 = x,  y0 = y, r = r), alpha = 0.33, fill = 'black') + 
  coord_fixed() 

推荐答案

添加到@MKBakker 的答案中,可以使用一个函数来预测来自任意数量的层和 alpha 值的结果 alpha:

Adding to @MKBakker's answer, one could use a function to predict the resulting alpha from any number of layers and alpha values:

alpha_out <- function(alpha, num = 1) {
  result = alpha
  if(num == 1)  return(result)
  for(i in 2:num) { result = result + alpha * (1-result) }
  return (result)
}

alpha_out(0.33, 1)
#[1] 0.33
alpha_out(0.33, 2)
#[1] 0.5511
alpha_out(0.33, 3)
#[1] 0.699237

这使得更容易看出 alpha 渐近地接近 1,层数越多.

This makes it easier to see that alpha asymptotically approaches 1 with more layers.

alpha_out(0.33, 40)
#[1] 0.9999999

如果假设 0.99足够接近",则需要使用 0.8 才能达到三层

If one presumes that 0.99 is "close enough," you need to use 0.8 to get there with three layers

alpha_out(0.8, 3)
#[1] 0.992

添加结果图表

我们可以看到从一系列 alpha 和层中得到的结果:

We can see what results we'd get from a range of alphas and layers:

library(tidyverse)
alpha_table <- 
  tibble(
    alpha = rep(0.01*1:99, 10),
    layers = rep(1:10, each = 99)
  )

alpha_table <- alpha_table %>%
  rowwise() %>%
  mutate(result = alpha_out(alpha, layers))

ggplot(alpha_table, aes(alpha, result, color = as_factor(layers),
                    group = layers)) +
geom_line()

而且我们还可以看到在给定每个层数的情况下,我们需要多少 alpha 才能通过组合不透明度的阈值.例如,对于给定的层数,这里是达到 0.99 总不透明度需要多少 alpha.例如,对于 5 层,您需要 alpha = 0.61.

And we can also see how much alpha we need to pass a threshold of combined opacity, given each number of layers. For instance, here's how much alpha you need to reach 0.99 total opacity for a given number of layers. For 5 layers, you need alpha = 0.61, for instance.

alpha_table %>%
  group_by(layers) %>%
  filter(result >= 0.99) %>%
  slice(1)
## A tibble: 10 x 3
## Groups:   layers [10]
#   alpha layers result
#   <dbl>  <int>  <dbl>
# 1  0.99      1  0.99 
# 2  0.9       2  0.99 
# 3  0.79      3  0.991
# 4  0.69      4  0.991
# 5  0.61      5  0.991
# 6  0.54      6  0.991
# 7  0.49      7  0.991
# 8  0.44      8  0.990
# 9  0.41      9  0.991
#10  0.37     10  0.990

所有这些都是说我认为没有一个简单的实现来获得您正在寻找的东西.如果您希望重叠区域 100% 暗,您可以尝试以下方法:

All this to say that I don't think there is a simple implementation to get what you're looking for. If you want 100% dark in the overlapped area, you might try these approaches:

  • 事后图像处理(也许可以使用 imagemagick)应用亮度曲线使暗区 100% 黑色,并使其他区域缩放到您期望的暗度级别.

  • image manipulation after the fact (perhaps doable using imagemagick) to apply a brightness curve to make the dark areas 100% black and make the others scale to the darkness levels you expect.

将图形转换为 sf 对象并分析形状以计算在任何给定点重叠的形状数量.然后,您可以手动将这些映射到您想要的暗度.

convert the graph to an sf object and analyze the shapes to somehow count how many shapes are overlapping at any given point. You could then manually map those to the darkness levels you want.

这篇关于控制 n 个重叠区域的 alpha 混合/不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆