【发布时间】:2022-02-05 05:10:59
【问题描述】:
The hue-rotate() filter function "旋转元素的色调...指定为角度"。如果这是真的,filter: hue-rotate(180deg) hue-rotate(180deg) 将无效。但它肯定确实有效果:
.square {
height: 3rem;
padding: 1rem;
background: linear-gradient( 90deg, rgba(255, 0, 0, 1) 0%, rgba(255, 154, 0, 1) 10%, rgba(208, 222, 33, 1) 20%, rgba(79, 220, 74, 1) 30%, rgba(63, 218, 216, 1) 40%, rgba(47, 201, 226, 1) 50%, rgba(28, 127, 238, 1) 60%, rgba(95, 21, 242, 1) 70%, rgba(186, 12, 248, 1) 80%, rgba(251, 7, 217, 1) 90%, rgba(255, 0, 0, 1) 100%);
font-family: monospace;
font-weight: bold;
color: white;
}
.double-invert {
filter: hue-rotate(180deg) hue-rotate(180deg);
}
<div class="square">filter: none</div>
<div class="square double-invert">filter: hue-rotate(180deg) hue-rotate(180deg)</div>
这里发生了什么? hue-rotate 实际上是做什么的?以及如何实现 是 其自身逆向的色调旋转? (或者,我怎样才能想出一个反转色调旋转的滤镜?)
更新:按照 Temani Aiff 的回答,似乎 hue-rotate(180deg) 实际上是一个矩阵乘法。但是,尚不清楚它实际使用的是什么矩阵。下面显示我们可以将 SVG 过滤器 type="hueRotate" 重新实现为原始矩阵,但 CSS 过滤器 hue-rotate 实际上并不匹配其中任何一个:
.square {
height: 3rem;
padding: 1rem;
background: linear-gradient( 90deg, rgba(255, 0, 0, 1) 0%, rgba(255, 154, 0, 1) 10%, rgba(208, 222, 33, 1) 20%, rgba(79, 220, 74, 1) 30%, rgba(63, 218, 216, 1) 40%, rgba(47, 201, 226, 1) 50%, rgba(28, 127, 238, 1) 60%, rgba(95, 21, 242, 1) 70%, rgba(186, 12, 248, 1) 80%, rgba(251, 7, 217, 1) 90%, rgba(255, 0, 0, 1) 100%);
font-family: monospace;
font-weight: bold;
color: white;
}
.hue-rotate {
filter: hue-rotate(180deg);
}
.hue-rotate-svg {
filter: url(#svgHueRotate180);
}
.hue-rotate-svg-matrix {
filter: url(#svgHueRotate180Matrix);
}
<svg style="position: absolute; top: -99999px" xmlns="http://www.w3.org/2000/svg">
<filter id="svgHueRotate180">
<feColorMatrix in="SourceGraphic" type="hueRotate"
values="180" />
</filter>
<!-- Following matrix calculated following spec -->
<filter id="svgHueRotate180Matrix">
<feColorMatrix in="SourceGraphic" type="matrix"
values="
-0.574 1.43 0.144 0 0
0.426 0.43 0.144 0 0
0.426 1.43 -0.856 0 0
0 0 0 1 0" />
</filter>
</svg>
<div class="square">filter: none</div>
<div class="square hue-rotate">filter: hue-rotate(180deg)</div>
<div class="square hue-rotate-svg">using SVG hueRotate</div>
<div class="square hue-rotate-svg-matrix">using SVG raw matrix</div>
至少在 Chrome 和 Firefox 中,hue-rotate 正在做一些不同于 SVG 过滤器的事情。但它在做什么?!
【问题讨论】:
-
为什么不应该有效果?
-
@cloned 因为在普通几何中,这些旋转添加到单个旋转 360 度,相当于 0 度
-
另外:因为如果
hue-rotate(Xdeg)表示将hsl(h,s,l)转换为hsl(h+X, s, l),那么hue-rotate(180deg) hue-rotate(180deg)将hsl(h,s,l)转换为hsl(h+360deg, s, l),相当于hsl(h,s,l) -
这可能是由于
rgba和hsla之间的转换而发生的,因为某些颜色无法正确转换。由于filter值被逐一应用(下一个应用于前一个的结果),转换偏差变得更加严重。
标签: html css svg colors css-filters