我正在与CSS预处理器Stylus一起工作,我想要一个函数来将颜色的“感知亮度”调整为20%或-10%。

我发现了“亮度”一词,看起来手写笔具有获取颜色亮度的功能,但不能调节颜色亮度。

如何创建这样的功能?

最佳答案

经过一番修补后,似乎可以正常使用了。我不确定这是否100%正确。

adjust-luminance($color, $amount = 10%)
    $scale = unit($amount, '') / 100 * 255

    //green is the lightest component, followed by red, then blue.
    $redWeight = 0.2126
    $greenWeight = 0.7152
    $blueWeight = 0.0722

    // get the individual components of the color
    $red = red($color)
    $green = green($color)
    $blue = blue($color)

    //get percent
    $percentRed = $red / 255
    $percentGreen = $green / 255
    $percentBlue = $blue / 255

    $lumRed = $percentRed * $redWeight
    $lumGreen = $percentGreen * $greenWeight
    $lumBlue = $percentBlue * $blueWeight

    $lumTotal = 1 + ($lumRed + $lumGreen + $lumBlue)

    $red += $scale * (1 - $redWeight) * $lumTotal
    $green += $scale * (1 - $greenWeight) * $lumTotal
    $blue += $scale * (1 - $blueWeight) * $lumTotal

    $adjusted = rgb($red, $green, $blue)
    $adjusted = saturation($adjusted, saturation($color))
    return $adjusted

10-05 20:59
查看更多