使用带有白色背景的 alpha 混合将 ARBG 转换为 RGB [英] Converting ARBG to RGB with alpha blending with a White Background

查看:116
本文介绍了使用带有白色背景的 alpha 混合将 ARBG 转换为 RGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中存储了一个颜色十六进制字符串,例如 0x78dce6b0;我可以使用以下方法将其转换为 ARGB 颜色:

I have a color hex string stored in the database like 0x78dce6b0; I can convert this to an ARGB color using:

string colorString=0x78dce6b0;
int hexColor = Convert.ToInt32(colorString ?? "0", 16);
Color colorTL = Color.FromArgb(hexColor);

现在我想将其转换为在 HTML 页面中使用,因此我需要转换为像 #cc3388 这样的 HTML 值.如果我直接使用 ColorTranslator.ToHtml(colorTL) 进行转换,则会丢失 alpha 混合值.假设背景始终为白色,如何通过考虑 alpha 值来转换它?

Now I want to convert this to use in an HTML page, so I need to convert into an HTML value like #cc3388. If I directly convert using ColorTranslator.ToHtml(colorTL), I lose the alpha blending value. How do I convert it by taking into consideration the alpha value, assuming the background is always white?

推荐答案

HTML 颜色没有 Alpha 组件.

HTML Colors do not have an Alpha component.

不幸的是,不可能在不丢失 Alpha 组件的情况下将 ARGB 转换为 HTML RGB.

Unfortunately it is impossible to convert ARGB to HTML RGB without losing the Alpha component.

如果您想根据您的 alpha 值将颜色与白色混合...

  • 将每个十六进制分量(A、R、G、B)转换为 0 到 255 之间的值,其中 FF 的值等于 255,00 等于 0.
  • 我们将这些 0-255 个值命名为 A、R、G、B
  • 我们假设 Alpha 值为 255 表示完全透明",Alpha 值为 0 表示完全不透明"
  • New_R = CInt((255 - R) * (A/255.0) + R)
  • New_G = CInt((255 - G) * (A/255.0) + G)
  • New_B = CInt((255 - G) * (A/255.0) + B)
  • 您最终的 HTML 颜色字符串将是:#" &cstr(New_R) &cstr(New_G) &cstr(New_B)

这篇关于使用带有白色背景的 alpha 混合将 ARBG 转换为 RGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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