Use HEX or RGBA

人走茶凉 提交于 2020-01-02 10:32:13

问题


I've a simple question: If I'm not going to use Alpha on RGBA, for some colors, should I use HEX on these colors? Is there something HEX has that RGBA doesn't? Like browser compatibility, for example? Or is it just fine to use RGBA all the time? I find it better, cause if I would want to use Alpha, I could. Thanks in advance.


回答1:


Both should be compatible with any standards-compliant browser.

Edit: There might be some buggy behavior in IE (suprise!): http://css-tricks.com/ie-background-rgb-bug/




回答2:


When using RGBA, I believe the correct syntax is to use R, G, B values. Hence the name. I've not tried using Hex instead, that may result in unexpected behavior.

In terms of browser compatibility, Hex and RGB are pretty much same/same.




回答3:


Form a browser compatibility stand point there is nearly no real difference in any modern-ish Browser.

Where you can, I'd go with HEX. Shorter, one string and therefore easier to copy to third party programs (e.g. Photoshop, etc.) and also you don't have the IE bug mentioned by Erty.

I tend to use RGBA only where I need alpha, on all other elements I use HEX.




回答4:


You could actually (almost) always use Hex.

Even when you need alpha there's a way to convert the RGBA value to an equivalent Hex value as long as the background color (under the element with rgba) is a single solid color.

The algorithm for this conversion is here.

Source => Target = (BGColor + Source) =
Target.R = ((1 - Source.A) * BGColor.R) + (Source.A * Source.R)
Target.G = ((1 - Source.A) * BGColor.G) + (Source.A * Source.G)
Target.B = ((1 - Source.A) * BGColor.B) + (Source.A * Source.B)

( I've actually used it a few times, and it works well for me.

As mentioned in previous answers, the gain over here is that the intended RBGA color will look like it should in ie8 and down.



来源:https://stackoverflow.com/questions/12695336/use-hex-or-rgba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!