2016-08-02 42 views
0

我正在使用Stylus,CSS預處理器,並且我想要一個函數將顏色的「感知亮度」調整爲20%或-10%。在觸控筆中調整顏色的亮度

我發現了「亮度」這個詞,它看起來像Stylus有一個功能,可以得到一個顏色的亮度,但是沒有調整亮度。

如何創建這樣的功能?

+0

'lighten'功能沒有足夠多的達到你的目的 – blonfu

+0

或者更好的'lightness' e.g:'亮度(#000,50%)==>#808080' – blonfu

回答

0

經過一番修補之後,這一個似乎工作。我不確定它是否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