Created
October 31, 2012 13:25
-
-
Save justan/3987026 to your computer and use it in GitHub Desktop.
给定一个背景颜色, 将 rgba 转换成 hex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//give a background color, convert rgba to hex | |
var rgbaToHex = function(rgba, bgRgb){ | |
var a = rgba[3] / 255 | |
, r = Math.round((1 - a) * bgRgb[0] + a * rgba[0]).toString(16) | |
, g = Math.round((1 - a) * bgRgb[1] + a * rgba[1]).toString(16) | |
, b = Math.round((1 - a) * bgRgb[2] + a * rgba[2]).toString(16) | |
; | |
return '#' + | |
(r.length === 1 ? ('0' + r) : r) + | |
(g.length === 1 ? ('0' + g) : g) + | |
(b.length === 1 ? ('0' + b) : b); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment