工具函数:rgba转rgb

div{
  width : 300px;
  height : 100px;
  text-align: center;
  line-height: 100px;
}

html{
  height: 100%;
  background-color: rgb(144, 200, 10);
}

#a{
  background-color: rgba(20,20,130,0.5);
}

#b{
  background-color: rgba(10,130,50,0.5);
}

#c{
  background-color: rgba(110,20,40,0.5);
}

#d{
  background-color: rgba(10,50,30,0.5);
}
function RGBA2RGB(rgba_color, background_color){
  //注:rgba_color的格式为rgba(0,0,0,0.1), back_color的格式为rgb(0,0,0)
  var bgArray = background_color.split("(")[1].split(")")[0].split(",");
  var rgbaArr = rgba_color.split("(")[1].split(")")[0].split(",");
  var a = rgbaArr[3];
  var r = bgArray[0] * (1 - a) + rgbaArr[0] * a;
  var g = bgArray[1] * (1 - a) + rgbaArr[1] * a;
  var b = bgArray[2] * (1 - a) + rgbaArr[2] * a;
  return "rgb(" + Math.round(r) + "," + Math.round(g) + "," + Math.round(b) +")";
}

$('div').click(function(){
  $(this).css(
    'background-color', 
    RGBA2RGB($(this).css('background-color'), $('html').css('background-color'))
  )
})

你可能感兴趣的:(工具函数:rgba转rgb)