2012-09-30 199 views
1

我在這裏創辦了這一段代碼堆棧:JAVASCRIPT亮度功能 - 降低

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); 
} 

沒有人知道如何使完全相同逆?

功能decreas_brightness(){}我的意思是

+0

你試過嗎? – azendh

+0

試過它不起作用 – sbaaaang

+1

該函數不適用於所有情況。 'increase_brightness('#777777',100)':輸出是#000000 – azendh

回答

1

你的代碼是從JavaScript Calculate brighter colour。根據評論,下面的改變應該會使亮度下降:

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); 
} 
+0

你的靜態(1 << 8)有什麼用途?爲什麼不輸入相應的值? – CBusBus

+0

實際上,這個代碼的作者在原始問題的評論中討論了這個問題 - 請參閱他在鏈接中的解釋以獲得完整的解釋,但本質上是這樣的,結果將被正確格式化(例如'05'而不是' 5' ) – Kelvin