通过与白色背景进行 alpha 混合将 ARBG 转换为 RGB

发布于 2024-11-11 07:06:41 字数 405 浏览 4 评论 0原文

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

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满天都是小星星 2024-11-18 07:06:41

HTML 颜色没有 Alpha 组件。

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

如果您想根据您的 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)

HTML Colors do not have an Alpha component.

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

If you want to blend your color with white, based upon your alpha value...

  • Convert each Hex component (A, R, G, B) into a value from 0 to 255, where a value of FF equates to 255, 00 equates to 0.
  • We'll name these 0-255 values, A, R, G, B
  • We'll assume that an Alpha value of 255 means "fully transparent" and an Alpha value of 0 means "fully opaque"
  • 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)
  • Your final HTML color string will be: "#" & cstr(New_R) & cstr(New_G) & cstr(New_B)
苦笑流年记忆 2024-11-18 07:06:41

所有提议都不适合我,但后来使用了
http://en.wikipedia.org/wiki/Alpha_compositing
想出这个:

var useColor = Color.FromArgb(
                (int)((theStructureColor.R) * (theStructureColor.A / 255.0) + 255 - theStructureColor.A),
                (int)((theStructureColor.G) * (theStructureColor.A / 255.0) + 255 - theStructureColor.A),
                (int)((theStructureColor.B) * (theStructureColor.A / 255.0) + 255 - theStructureColor.A));

None of the proposed worked for me, but then used
http://en.wikipedia.org/wiki/Alpha_compositing
to come up with this:

var useColor = Color.FromArgb(
                (int)((theStructureColor.R) * (theStructureColor.A / 255.0) + 255 - theStructureColor.A),
                (int)((theStructureColor.G) * (theStructureColor.A / 255.0) + 255 - theStructureColor.A),
                (int)((theStructureColor.B) * (theStructureColor.A / 255.0) + 255 - theStructureColor.A));
对岸观火 2024-11-18 07:06:41

因此,您真正要做的是将白色背景与半透明前景色组合起来,并计算结果显示颜色。这似乎是 计算从 2 种颜色得到的 RGB 的重复问题,一个是透明的,其中发布了合适的答案。要将其应用到您的情况,它将类似于以下内容(注意,我尚未测试此代码):

public static Color FromTransparentWithWhiteBG(Color A)
{
    // Assuming Alpha is a value between 0 and 254...
    var aFactor = (A.A/254);
    var bgAFactor = (1-(A.A/254));
    var background = Color.White;
    var R_c = (Int32)(Math.Ceiling((Double)A.R * aFactor) + Math.Ceiling ((Double)background.R * (1 - bgAFactor )));
    var G_c = (Int32)(Math.Ceiling((Double)A.G * aFactor) + Math.Ceiling ((Double)background.G * (1 - bgAFactor )));
    var B_c = (Int32)(Math.Ceiling((Double)A.B * aFactor) + Math.Ceiling ((Double)background.B * (1 - bgAFactor )));
    return Color.FromArgb(1, R_c, G_c, B_c);
}

So what you are really doing is combining a white background with a semi-transparent foreground color, and calculating the resulting display color. This seems to be a duplicate question of Calculate resulting RGB from 2 colors, one is transparent, where a suitable answer was posted. To apply it to your situation, it would be something like the following (note, I haven't tested this code):

public static Color FromTransparentWithWhiteBG(Color A)
{
    // Assuming Alpha is a value between 0 and 254...
    var aFactor = (A.A/254);
    var bgAFactor = (1-(A.A/254));
    var background = Color.White;
    var R_c = (Int32)(Math.Ceiling((Double)A.R * aFactor) + Math.Ceiling ((Double)background.R * (1 - bgAFactor )));
    var G_c = (Int32)(Math.Ceiling((Double)A.G * aFactor) + Math.Ceiling ((Double)background.G * (1 - bgAFactor )));
    var B_c = (Int32)(Math.Ceiling((Double)A.B * aFactor) + Math.Ceiling ((Double)background.B * (1 - bgAFactor )));
    return Color.FromArgb(1, R_c, G_c, B_c);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文