【RGB和RGBA之间的转换】也许对你有用

项目需要用到透明度,但UI给我的都是颜色,没有给关于透明度的色系,无奈之下,自己写了一个转换,希望对你有用

代码如下:

function RGB2RGBA(rgb_color,alp){
	//注:rgb_color的格式为#FFFFFFF,alp为透明度
	var r = parseInt("0x" + rgb_color.substr(1,2));
	var g = parseInt("0x" + rgb_color.substr(3,2));
	var b = parseInt("0x" + rgb_color.substr(5,2));
	var a = alp;
	return "rgba(" + r + "," + g + "," + b + "," + a + ")";
}

function RGBA2RGB(rgba_color){
	//注:rgba_color的格式为rgba(0,0,0,0.1)
	var BGcolur = 1;
	var arr = rgba_color.split("(")[1].split(")")[0].split(",");
	var a = arr[3];
	var r = BGcolur * (1 - a) + arr[0] * a;
	var g = BGcolur * (1 - a) + arr[1] * a;
	var b = BGcolur * (1 - a) + arr[2] * a;
	return "rgb(" + r + "," + g + "," + b +")";
}

你可能感兴趣的:(工作)