通过与白色背景进行 alpha 混合将 ARBG 转换为 RGB
我在数据库中存储了一个颜色十六进制字符串,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTML 颜色没有
Alpha
组件。不幸的是,不可能在不丢失
Alpha
组件的情况下将ARGB
转换为 HTMLRGB
。如果您想根据您的 alpha 值将颜色与白色混合...
HTML Colors do not have an
Alpha
component.Unfortunately it is impossible to convert
ARGB
to HTMLRGB
without losing theAlpha
component.If you want to blend your color with white, based upon your alpha value...
所有提议都不适合我,但后来使用了
http://en.wikipedia.org/wiki/Alpha_compositing
想出这个:
None of the proposed worked for me, but then used
http://en.wikipedia.org/wiki/Alpha_compositing
to come up with this:
因此,您真正要做的是将白色背景与半透明前景色组合起来,并计算结果显示颜色。这似乎是 计算从 2 种颜色得到的 RGB 的重复问题,一个是透明的,其中发布了合适的答案。要将其应用到您的情况,它将类似于以下内容(注意,我尚未测试此代码):
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):