Convert hex to rgb or rgba color using PHP

Convert hex to rgb or rgba color using PHP

As far as we know, many of WordPress theme developers use a lot of design options inside their themes, just as we do. Which means that you usually provide color pickers so the users may easily change color settings for their websites.

This usually means that you will need to create dynamic CSS files instead of a hard-coded CSS, so the user’s color changes may apply on the website instantly. As for HTML5/CSS3 standards, hex colors can’t always do all the work so you need to work with RGB or RGBA color codes.

While have been struggling with this sometimes, we have made a little helper function which converts hex color code string into RGB or even RGBA color, for example if you want to add opacity to some element color and basically achieve “transparent hex code”.

Hex to rgba converter php function

Here’s the code:

So far so good! Now we are going to show you how this function can be helpful whilst creating dynamic CSS with a simple example below.

Usage example:

Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

33 comments

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *