简体   繁体   English

如何仅使用实线边框来近似CSS box-shadow属性?

[英]How to approximate CSS box-shadow property using solid border only?

Please note that the question is not about CSS, rather about imaging, colors and a bit of mathematics. 请注意,问题不是关于CSS,而是关于成像,颜色和一些数学。 We are not talking here about how to do things in CSS. 我们不是在谈论如何在CSS中做事情。 Make sure your answer does not contain a single line of CSS. 确保您的答案不包含一行CSS。 Mathematics, tables, images, and formulas only, please. 请数学,表格,图像和公式。

I need a way (data table, formula, whatever) to calculate good enough values of border-width and border-color 's alpha channel (assuming that we use border-style: solid ) required to mimic blur-radius and spread-radius of box shadow of the same color (assuming there are no X and Y offsets), at least, from the point of human's eye perception. 我需要一种方法(数据表,公式,等等)来计算足够好的border-widthborder-color的alpha通道值(假设我们使用border-style: solid )来模拟blur-radiusspread-radius相同颜色的盒子阴影(假设没有X和Y偏移),至少从人眼感知的角度来看。

Below is an example of black rgb(0,0,0) color and blur-radius ranging from 1px to 8px. 下面是黑色rgb(0,0,0)颜色和blur-radius范围从1px到8px的示例。

颜色= 000,模糊= 1px颜色= 000,模糊= 2px颜色= 000,模糊= 3px颜色= 000,模糊= 4px颜色= 000,模糊= 5px颜色= 000,模糊= 6px颜色= 000,模糊= 7px颜色= 000,模糊= 8px

Currently, I'm building a data table of opacities for every next shadow pixel depending on the blur value, and if we presume that alpha blending takes place (white+black), then for the first three pixels opacities look this way (at least, in Google Chrome): 目前,我正在根据模糊值为每个下一个阴影像素构建一个不透明度的数据表,如果我们假设发生了alpha混合(白色+黑色),那么对于前三个像素,不透明度就是这样(至少,在谷歌浏览器中):

黑色的不透明度取决于阴影模糊和像素索引

But still, even if I collect some empirical evidence and try out a few ideas, I don't know how to compare quantitatively my approximations and the original box-shadow version. 但是,即使我收集了一些经验证据并尝试了一些想法,我也不知道如何定量地比较我的近似值和原始的盒子阴影版本。 If I can't measure, I can't tell whether an idea really works. 如果我无法衡量,我无法分辨一个想法是否真的有效。

Maybe you've got some ideas how to pick best matching border width and color? 也许你有一些想法如何选择最佳匹配的边框宽度和颜色?

Thanks in advance. 提前致谢。

Sometimes we have to answer our own questions. 有时我们必须回答我们自己的问题。 What did I do to answer mine - see below: 我做了什么来回答我的问题 - 见下文:

I've prepared 16x16 grid of Chrome screenshots (120px x 120px) using this utility HTML: 我使用此实用程序HTML准备了16x16网格的Chrome屏幕截图(120px x 120px):

https://noomorph.com/approximate-box-shadow-using-border/screenshotter/ https://noomorph.com/approximate-box-shadow-using-border/screenshotter/

It renders a grid of 40x40 squares with shadow blur = 0..15px and shadow size = 0..15px . 它渲染一个40x40正方形的网格,阴影模糊= 0..15px ,阴影大小= 0..15px

组合网格

Then I've cut the big page screenshot (1920x1920) to 256 smaller ones like this one in the link below. 然后我将大页面截图(1920x1920)剪切为256个较小的截图,如下面的链接中的这个。 My snake_case convention was <shadow-size>_<shadow-blur>.png : 我的snake_case约定是<shadow-size>_<shadow-blur>.png

https://github.com/noomorph/approximate-box-shadow-using-border/blob/master/scripts/00_15.png https://github.com/noomorph/approximate-box-shadow-using-border/blob/master/scripts/00_15.png

Using ImageMagick convert , grep , sed , sort , uniq I've produced stats for every image: 使用ImageMagick convertgrepsedsortuniq我为每个图像生成了统计信息:

https://github.com/noomorph/approximate-box-shadow-using-border/blob/master/scripts/00_15.png.stats https://github.com/noomorph/approximate-box-shadow-using-border/blob/master/scripts/00_15.png.stats

Using this tiny script I produced JSON array of cases like { size, blur, colors: [{ count, value }]} : 使用这个小脚本,我生成了JSON数组,例如{ size, blur, colors: [{ count, value }]}

https://github.com/noomorph/approximate-box-shadow-using-border/blob/master/scripts/aggregate_stats.js https://github.com/noomorph/approximate-box-shadow-using-border/blob/master/scripts/aggregate_stats.js

Then using simple arithmetics, basic distance formula D=(C1-C2)^2 (where C1 and C2 are gray channels of two colors respectively) and brute force (border-width= 0..20 and RGB grayscale channel= 1..255 ) I've got the mapping you can see on my demo site I've built for this: 然后使用简单的算术,基本距离公式D=(C1-C2)^2 (其中C1和C2分别是两种颜色的灰色通道)和强力(边界宽度= 0..20和RGB灰度通道= 1..255 )我已经在我为此构建的演示站点上看到了映射:

https://noomorph.com/approximate-box-shadow-using-border/ https://noomorph.com/approximate-box-shadow-using-border/

演示截图

I was trying to build some 3D and 4D plots, but still not sure about the nature of the correlation between box-shadow and border . 我试图构建一些3D和4D图,但仍然不确定box-shadowborder之间的相关性。 I sincerely hope that my small research helps somebody in the future. 我真诚地希望我的小型研究能够在将来帮助某些人。

3D情节

Happy hacking! 快乐的黑客!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM