Skip to content

Instantly share code, notes, and snippets.

@justan
Created October 31, 2012 13:25
Show Gist options
  • Save justan/3987026 to your computer and use it in GitHub Desktop.
Save justan/3987026 to your computer and use it in GitHub Desktop.
给定一个背景颜色, 将 rgba 转换成 hex
//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