How do you calculate overlapping rgba values?

对着背影说爱祢 提交于 2020-01-14 04:24:07

问题


Let's say I have this:

.example { 

  background-color: rgba(0, 0, 0, 0.5); 
  border-color: rgba(0, 0, 0, 0.5);

}

The background-color and border-color have the same rgba value, but are different colors because the border-color is calculated on top of the background-color.

(simple example: http://jsfiddle.net/BBdB3/)

What transparency value for the border-color would make it match the background-color? What's the math here?

Edit: thanks guys, using rgba(0, 0, 0, 0) or transparency is sufficient for my first example, but I still have a problem. (http://jsfiddle.net/CPK7L/2/). In this case I need to match the border-color of the first element with the border-color of the second element.

So how would I calculate the alpha value for the border of the first element based on the alpha value of the second element?


回答1:


If you set the borders alpha channel to 0, it'll match the background-color. It's where no modifications are made to the underlying color; it's the transparent mode.

To calculate the sum of the alpha-channels it is basically:

alpha-channel of border * (1 - alpha-channel of background) + alpha-channel of background

So the border has effectively an alpha-channel value of 0.5 * (1 - 0.5) + 0.5 = 0.5 * 0.5 + 0.5 = 0.25 + 0.5 = 0.75.




回答2:


A transparency (alpha) value of 0 for the border-color will make it match the background-color. See: http://en.wikipedia.org/wiki/RGBA_color_space

.example {
    display: inline-block;
    padding: 50px;
    background: rgba(0, 0, 0, 0.5);
    border: 25px solid rgba(0, 0, 0, 0);
}

Fiddle: http://jsfiddle.net/BBdB3/2/



来源:https://stackoverflow.com/questions/16906525/how-do-you-calculate-overlapping-rgba-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!