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?
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):
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...
None of the proposed worked for me, but then used http://en.wikipedia.org/wiki/Alpha_compositing to come up with this: