1

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

itsme
  • 48,972
  • 96
  • 224
  • 345
  • 1
    The function is not works at all cases. `increase_brightness('#777777', 100)`: output is #000000 – azendh Sep 30 '12 at 11:46

1 Answers1

1

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);
}
Community
  • 1
  • 1
Kelvin
  • 5,227
  • 1
  • 23
  • 36
  • What purpose does your static (1 << 8) serve? Why not just enter the corresponding value? – CBusBus Sep 30 '12 at 12:52
  • This was actually discussed by the author of this code in the comments of the original question - see his explanation in the link for the full explanation, but essentially it's so that the result will be formatted correctly (eg, '05' rather than '5') – Kelvin Sep 30 '12 at 12:55