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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,008 questions

51,955 answers

573 users

How to convert from RGB to HEX with 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 Sep 2, 2015 by avibootz
edited Jan 27, 2025 by avibootz
0 votes
$r = 80;
$g = 125;
$b = 210;
      
echo sprintf("0x%02X%02X%02X", $r, $g, $b);

  
/*
run:
  
0x507DD2
  
*/

 



answered Jan 27, 2025 by avibootz
0 votes
function rgb2hex($rgb) {
   $hex = "0x";
   $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
 
   return $hex; 
}
 
$hex = rgb2hex(array( 0, 255, 0 ));

echo $hex;

  
/*
run:
  
0x00ff00
  
*/

 



answered Jan 27, 2025 by avibootz

Related questions

1 answer 126 views
126 views asked Dec 29, 2021 by avibootz
2 answers 347 views
1 answer 50 views
1 answer 79 views
1 answer 71 views
1 answer 83 views
1 answer 78 views
78 views asked Jan 27, 2025 by avibootz
...