将RGBA转换为HEX [英] Convert RGBA to HEX

查看:1951
本文介绍了将RGBA转换为HEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定css颜色值,如:

  rgba(0,0,0,0.86)



如何将它转换为考虑到alpha分量的RGB十六进制值,假设白色背景?

解决方案

由于alpha值会衰减背景颜色和颜色值,这样的东西可以做到:

  function rgba2rgb(RGB_background,RGBA_color)
{
var alpha = RGBA_color.a;

return new Color(
(1 - alpha)* RGB_background.r + alpha * RGBA_color.r,
(1 - alpha)* RGB_background.g + alpha * RGBA_color。 g,
(1 - alpha)* RGB_background.b + alpha * RGBA_color.b
);
}

(交互地尝试: http://marcodiiga.github.io/rgba-to-rgb-conversion


Given a css color value like:

 rgba(0, 0, 0, 0.86)

How do I convert that to a RGB hex value that takes the alpha component into account, assuming a white background?

解决方案

Since alpha value both attenuates the background color and the color value, something like this could do the trick:

function rgba2rgb(RGB_background, RGBA_color)
{
    var alpha = RGBA_color.a;

    return new Color(
        (1 - alpha) * RGB_background.r + alpha * RGBA_color.r,
        (1 - alpha) * RGB_background.g + alpha * RGBA_color.g,
        (1 - alpha) * RGB_background.b + alpha * RGBA_color.b
    );
}

(Try it interactively: http://marcodiiga.github.io/rgba-to-rgb-conversion)

这篇关于将RGBA转换为HEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆