PHP Convert HEX color code to RGB or RGB to HEX

Hi Guys,

This is a quick TIP so that I don’t have a burden of not sharing this age old function I wrote ages ago with you ūüôā

Anyway there are many occasion when you would like to problematically adjust say brightness, red, green, blue depths for a color code, Let me show you how to do it in PHP

PHP Function

Above is a simple to use method which you can use and modify as per your needs.

Let me show you some example usage for this function


Usage 1 – Converting RGB to HEX

This example shows how to use the above function to convert our rgb() values to a HEX code

Above will print #CC8800


Usage 2 – Converting HEX code to RGB

This example usage shows how to utilize above function to convert a HEXadecimal color code to rgb() values

Will print

As you’ve got the RGB values you can adjust them to suit your purpose. You can further shrink this function by atleast 3 more lines.

I hope this helps





Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.