Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[PHP] Convert hex color codes to RGB filter_list
Author
Message
[PHP] Convert hex color codes to RGB #1
I just wrote that small function for a friend of mine and thought it might be worth sharing it.
Is anybody interested in how hex color codes work and how to convert them to RGB? I mean that's just some basic stuff but many people use hex color codes everyday without even knowing how to build them.

Code:
function convertToRGB ($hexValue)
{
   if(ctype_xdigit(substr($hexValue, 1)))
   {
       switch(strlen($hexValue))
       {
           case 4:
               $r = hexdec(substr($hexValue, 1, 1).substr($hexValue, 1, 1));
               $g = hexdec(substr($hexValue, 2, 1).substr($hexValue, 2, 1));
               $b = hexdec(substr($hexValue, 3, 1).substr($hexValue, 3, 1));
               return 'rgb(' . $r . ', ' . $g . ', ' . $b . ')';
               
           case 7:
               $r = hexdec(substr($hexValue, 1, 2));
               $g = hexdec(substr($hexValue, 3, 2));
               $b = hexdec(substr($hexValue, 5, 2));
               return 'rgb(' . $r . ', ' . $g . ', ' . $b . ')';
               
           default:
               throw new Exception('Invalid length!');
       }
   } else {
       throw new Exception('Invalid value!');
   }
}

Reply

RE: [PHP] Convert hex color codes to RGB #2
Although there are many online tools that serve the same function, nonetheless, I'll keep this on hand.

Thanks.
[Image: AD83g1A.png]

Reply

RE: [PHP] Convert hex color codes to RGB #3
(03-23-2018, 05:42 AM)mothered Wrote: Although there are many online tools that serve the same function, nonetheless, I'll keep this on hand.

Thanks.

Well this allows you to build your own online tool hehe, even tho I’d use JS for such a tool.

Reply

RE: [PHP] Convert hex color codes to RGB #4
(03-23-2018, 07:07 AM)chunky Wrote:
(03-23-2018, 05:42 AM)mothered Wrote: Although there are many online tools that serve the same function, nonetheless, I'll keep this on hand.

Thanks.

Well this allows you to build your own online tool hehe, even tho I’d use JS for such a tool.

I'd just do it in my head...


(11-02-2018, 02:51 AM)Skullmeat Wrote: Ok, there no real practical reason for doing this, but that's never stopped me.

Reply

RE: [PHP] Convert hex color codes to RGB #5
(03-23-2018, 07:25 AM)Ender Wrote:
(03-23-2018, 07:07 AM)chunky Wrote:
(03-23-2018, 05:42 AM)mothered Wrote: Although there are many online tools that serve the same function, nonetheless, I'll keep this on hand.

Thanks.

Well this allows you to build your own online tool hehe, even tho I’d use JS for such a tool.

I'd just do it in my head...

You won’t believe how many people are too retarded to turn a 2 digit hexadecimal number into a decimal one in their head...
Kinda sad since it’s not hard at all. I mean it’s okay if you never heard anything about the hexadecimal system but some people in my course are still too retarded even tho we covered this topic ages ago.

Reply

RE: [PHP] Convert hex color codes to RGB #6
(03-23-2018, 07:38 AM)chunky Wrote: You won’t believe how many people are too retarded to turn a 2 digit hexadecimal number into a decimal one in their head...

Some users are not mentally-Inclined, but with some effort, much to their surprise they can In fact solve something as simple as this.

Of course, there are those who simply cannot put two and two together. I have no qualms nor anything against those who lack competency- we all have our weaknesses.
[Image: AD83g1A.png]

Reply

RE: [PHP] Convert hex color codes to RGB #7
(03-23-2018, 10:07 AM)mothered Wrote:
(03-23-2018, 07:38 AM)chunky Wrote: You won’t believe how many people are too retarded to turn a 2 digit hexadecimal number into a decimal one in their head...

Some users are not mentally-Inclined, but with some effort, much to their surprise they can In fact solve something as simple as this.

Of course, there are those who simply cannot put two and two together. I have no qualms nor anything against those who lack competency- we all have our weaknesses.

Well you simply gotta want to understand it, its just basic math.

Reply

RE: [PHP] Convert hex color codes to RGB #8
Thank you bro for sharing.
Die  But Don't Lie
“Oh Abu Dharr! Don’t look at the smallness of the sin but look at the one you disobeyed.” Prophet Muhammad (pbuh)
[Image: p_237m2jx1.png]
Click for Free VPN

Reply







Users browsing this thread: 1 Guest(s)