问题描述:

i founded this piece of code here on stack:

function increase_brightness(hex, percent){

var r = parseInt(hex.substr(1, 2), 16),

g = parseInt(hex.substr(3, 2), 16),

b = parseInt(hex.substr(5, 2), 16);

return '#' +

((0|(1<<8) + r + (256 - r) * percent / 100).toString(16)).substr(1) +

((0|(1<<8) + g + (256 - g) * percent / 100).toString(16)).substr(1) +

((0|(1<<8) + b + (256 - b) * percent / 100).toString(16)).substr(1);

}

does anyone knows how to make the exactly inverse?

function decreas_brightness(){} i mean

网友答案:

Your code is from Javascript Calculate brighter colour. According to the comments, the following change should make it decrease brightness:

function decrease_brightness(hex, percent){
    var r = parseInt(hex.substr(1, 2), 16),
        g = parseInt(hex.substr(3, 2), 16),
        b = parseInt(hex.substr(5, 2), 16);

   return '#' +
       ((0|(1<<8) + r * (100 - percent) / 100).toString(16)).substr(1) +
       ((0|(1<<8) + g * (100 - percent) / 100).toString(16)).substr(1) +
       ((0|(1<<8) + b * (100 - percent) / 100).toString(16)).substr(1);
}
相关阅读:
Top