Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,783 answers

573 users

How to convert RGB to hex in PHP

3 Answers

0 votes
function RGBtoHEX(int $r, int $g, int $b) {   
    return dechex( (($r & 0xff) << 16) + (($g & 0xff) << 8) + ($b & 0xff) );
}
 

$r = 80;
$g = 125;
$b = 210;
     
echo RGBtoHEX($r, $g, $b);

 

 
/*
run:
 
507dd2
 
*/

 





answered Dec 29, 2021 by avibootz
0 votes
$r = 80;
$g = 125;
$b = 210;
     
echo sprintf("#%02x%02x%02x", $r, $g, $b);

 
 
 
/*
run:
 
#507dd2
 
*/

 





answered Dec 29, 2021 by avibootz
0 votes
$r = 90;
$g = 195;
$b = 215;
     
echo sprintf("#%02X%02X%02X", $r, $g, $b);

 
 
 
/*
run:
 
#5AC3D7
 
*/

 





answered Dec 29, 2021 by avibootz

Related questions

1 answer 55 views
55 views asked Dec 29, 2021 by avibootz
1 answer 48 views
1 answer 86 views
86 views asked Sep 3, 2015 by avibootz
1 answer 104 views
104 views asked Sep 2, 2015 by avibootz
2 answers 107 views
107 views asked Dec 30, 2021 by avibootz
...