原因:
不,这两个功能不一样。在 Less 中,contrast 函数根据经过伽马校正的亮度值比较颜色。以下是Less documentation的摘录:
根据 WCAG 2.0,颜色的比较使用的是经过伽马校正的亮度值,而不是亮度。
而在 Sass 中,它们是 comparing the colors based on brightness of the colors。此函数给出luminance value as the output 而不是亮度值。所以他们给出了不同的输出。
解决方案:
虽然 Less 有一个 luma function 也可以直接获取经过 gamma 校正的亮度值,但 Sass 似乎没有任何内置函数来提供此值。因此,我们必须基于WCAG 2.0 specifications 编写自定义函数。那里提供了计算逻辑,下面是摘录:
对于 sRGB 颜色空间,颜色的相对亮度定义为 L = 0.2126 * R + 0.7152 * G + 0.0722 * B 其中 R、G 和 B 定义为:
如果 RsRGB
如果 GsRGB
如果 BsRGB
RsRGB = R8bit/255
GsRGB = G8bit/255
BsRGB = B8bit/255
我不是 Sass 专家,无法自己编写这个自定义函数,所以我从 this article by Toni Pinel 那里得到了一些帮助(好吧,几乎所有东西都没有他的 re-gamma 函数)。如果您使用下面的contrast-color 函数,它将提供与 Less 相同的输出。
@function de-gamma($n) { @if $n <= 0.03928 { @return $n / 12.92; } @else { @return pow((($n + 0.055)/1.055),2.4); } }
// sRGB BT-709 BRIGHTNESS
@function brightness($c) {
$rlin: de-gamma(red($c)/255);
$glin: de-gamma(green($c)/255);
$blin: de-gamma(blue($c)/255);
@return (0.2126 * $rlin + 0.7152 * $glin + 0.0722 * $blin) * 100;
}
// Compares contrast of a given color to the light/dark arguments and returns whichever is most "contrasty"
@function contrast-color($color, $dark: #000000, $light: #FFFFFF) {
@if $color == null {
@return null;
}
@else {
$color-brightness: brightness($color);
$light-text-brightness: brightness($light);
$dark-text-brightness: brightness($dark);
@return if(abs($color-brightness - $light-text-brightness) > abs($color-brightness - $dark-text-brightness), $light, $dark);
}
}
下面是Less is using for the luma function 的代码,它与上面给出的自定义函数非常相似。此函数返回与上面给出的 Sass 自定义函数相同的输出。
Color.prototype.luma = function () {
var r = this.rgb[0] / 255,
g = this.rgb[1] / 255,
b = this.rgb[2] / 255;
r = (r <= 0.03928) ? r / 12.92 : Math.pow(((r + 0.055) / 1.055), 2.4);
g = (g <= 0.03928) ? g / 12.92 : Math.pow(((g + 0.055) / 1.055), 2.4);
b = (b <= 0.03928) ? b / 12.92 : Math.pow(((b + 0.055) / 1.055), 2.4);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
注意事项:
如果我们使用luma(darken(@bg,10%)),Less 编译器会给出 23.64695145 作为输出。这与 Sass 自定义函数的输出(即 23.83975738)略有不同。但是luma(#868686)给出的输出和Sass自定义函数一样,所以我认为自定义函数没有错。
一些 SASS 编译器没有上面使用的 de-gamma 函数所需的本机 pow() 函数。出于这个原因,您可能需要包含一个库,该库具有此库或至少具有此类库中的 pow() 函数。 Sassy-Math 就是一个例子。