简体   繁体   English

SCSS对比度(来自Compass)的输出与LESS的对比度不同

[英]SCSS Contrast (from Compass) output different to LESS's contrast

Trying to use the .contrast-color function in SCSS from the compass library is generating a different color to that of the .contrast which belongs in LESS even though the specification in LESS states otherwise. 尝试从罗盘库中使用SCSS中的.contrast-color函数会生成与LESS中属于.contrast的颜色不同的颜色,即使LESS中的规范另有规定。

This function works the same way as the contrast function in Compass for SASS. 此功能的作用方式与Compass for SASS中的对比功能相同。 http://lesscss.org/functions/ http://lesscss.org/functions/

Here is an example of me using the SCSS version of contrast-color and the outputted css. 这是我使用对比颜色的SCSS版本和输出的CSS的示例。

SCSS gist SCSS要点

and here is the LESS version. 这是LESS版本。

LESS version 较少版本

Is there a like for like LESS contrast function in SCSS? SCSS中是否有类似LESS的对比功能? If not, how would one go about converting it. 如果没有,将如何进行转换。 Looking at the source there seems to be a fair few dependenices within LESS for the contrast to work. 从源头上看,LESS内部似乎很少有依赖于工作的对比。

Reason: 原因:

No, the two functions are not the same. 不,两个功能不相同。 In Less, the contrast function compares the colors based on the gamma corrected luma value. 在“较少”中, contrast功能根据经过伽玛校正的亮度值比较颜色。 Below is the extract from the Less documentation : 以下是Less文档的摘录:

In accordance with WCAG 2.0, colors are compared using their gamma-corrected luma value, not their lightness. 根据WCAG 2.0,使用经过伽玛校正的亮度值(而不是亮度)比较颜色。

Whereas in Sass, they are comparing the colors based on brightness of the colors . 而在Sass中,他们正在根据颜色的brightness比较颜色 This function is giving the luminance value as the output and not the luma value. 此功能将亮度值作为输出,而不是亮度值。 So they are giving different outputs. 因此,他们给出了不同的输出。


Solution: 解:

While Less has a luma function to directly get the gamma corrected luma value also, Sass doesn't seem to have any built-in functions to provide this value. 尽管Less具有一个luma函数,也可以直接获取经伽马校正的亮度值,但Sass似乎没有任何内置函数来提供此值。 So, we have to write custom functions based on the WCAG 2.0 specifications . 因此,我们必须根据WCAG 2.0规范编写自定义函数。 The calculation logic is provided there and below is the extract: 此处提供了计算逻辑,下面是摘录:

For the sRGB colorspace, the relative luminance of a color is defined as L = 0.2126 * R + 0.7152 * G + 0.0722 * B where R, G and B are defined as: 对于sRGB色彩空间,颜色的相对亮度定义为L = 0.2126 * R + 0.7152 * G + 0.0722 * B,其中R,G和B定义为:

if RsRGB <= 0.03928 then R = RsRGB/12.92 else R = ((RsRGB+0.055)/1.055) ^ 2.4 如果RsRGB <= 0.03928,则R = RsRGB / 12.92否则R =((RsRGB + 0.055)/1.055)^ 2.4

if GsRGB <= 0.03928 then G = GsRGB/12.92 else G = ((GsRGB+0.055)/1.055) ^ 2.4 如果GsRGB <= 0.03928,则G = GsRGB / 12.92否则G =((GsRGB + 0.055)/1.055)^ 2.4

if BsRGB <= 0.03928 then B = BsRGB/12.92 else B = ((BsRGB+0.055)/1.055) ^ 2.4 and RsRGB, GsRGB, and BsRGB are defined as: 如果BsRGB <= 0.03928,则B = BsRGB / 12.92否则B =((BsRGB + 0.055)/1.055)^ 2.4且RsRGB,GsRGB和BsRGB定义为:

RsRGB = R8bit/255 RsRGB = R8bit / 255

GsRGB = G8bit/255 GsRGB = G8bit / 255

BsRGB = B8bit/255 BsRGB = B8bit / 255

I'm not a Sass expert to write this custom function by myself, so I took some help from this article by Toni Pinel (well, almost everything bar his re-gamma function). 我不是一个独自编写此自定义函数的Sass专家,因此我从Toni Pinel的这篇文章中获得了一些帮助(嗯,几乎所有内容都妨碍了他的re-gamma函数)。 If you use the below contrast-color function, it would provide the same output as Less. 如果使用下面的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);
    }
}

Below is the code that Less is using for the luma function and it is very similar to the custom function given above. 以下是Less用于luma函数的代码,它与上面给出的自定义函数非常相似。 This function returns the same output as the Sass custom function given above. 该函数返回与上面给出的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;
};

Notes: 笔记:

  1. If we use luma(darken(@bg,10%)) , Less compiler is giving 23.64695145 as the output. 如果我们使用luma(darken(@bg,10%)) ,则较少的编译器给出23.64695145作为输出。 This is slightly different from the Sass custom function's output, which is 23.83975738. 这与Sass定制函数的输出23.83975738略有不同。 But luma(#868686) gives the same output as the Sass custom function and so I don't think the custom function is wrong. 但是luma(#868686)提供的输出与Sass自定义函数相同,因此我认为自定义函数没有错。

  2. Some SASS compilers do not have the native pow() function which is required by the de-gamma function used above. 某些SASS编译器没有上面的de-gamma函数所需的本地pow()函数。 For this reason, you may need to include a library which has this or at least the pow() function from such a library. 出于这个原因,您可能需要包括一个具有此库的库,或者至少包含此类库中的pow()函数的库。 An example would be Sassy-Math. 一个例子是Sassy-Math。

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

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