Convert HEX color to RGBA color code in PHP
While working with theme or plugin options, in some cases we need to convert a hex (hexadecimal) color code, which we get from a color picker field to the corresponding RGBA color code, e.g. to get a RGBA color value with the transparency for using in CSS.
How to convert hex to RGB or RGBA
How do I get the RGB or RGBA value if the stored option contains the hexadecimal color code? By passing a HEX color value to the following HEX2RGBA function, you will get the correct output for using in CSS like rgb(255,255,255)
or rgba(0,0,0,0.5)
:
How do we use the function?
Within a template, we can now use the function by calling hex2rgba( $color );
to convert the HEX code to RGB or RGBA. The following example shows how we can use this function: