将 RGBA 转换为十六进制 [英] Convert RGBA to HEX

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

问题描述

给定一个 css 颜色值,例如:

Given a css color value like:

 rgba(0, 0, 0, 0.86)

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

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

推荐答案

由于 alpha 值同时衰减背景颜色和颜色值,这样的事情可以解决问题:

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
    );
}

(交互式尝试:https://marcodiiga.github.io/rgba-to-rgb-conversion)

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

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