PHP function converts hexadecimal color code to RGB color value_PHP tutorial

WBOY
Release: 2016-07-13 09:53:31
Original
986 people have browsed it

php function converts hexadecimal color codes into RGB color values

Use php function to convert 16-prohibited color codes into RGB color values.

/** 
* function 16进制颜色转换为RGB色值
* author www.phpernote.com
*/ 
function hex2rgb($hexColor){
	$color=str_replace('#','',$hexColor);
	if (strlen($color)> 3){
		$rgb=array(
			'r'=>hexdec(substr($color,0,2)),
			'g'=>hexdec(substr($color,2,2)),
			'b'=>hexdec(substr($color,4,2))
		);
	}else{
		$color=str_replace('#','',$hexColor);
		$r=substr($color,0,1). substr($color,0,1);
		$g=substr($color,1,1). substr($color,1,1);
		$b=substr($color,2,1). substr($color,2,1);
		$rgb=array( 
			'r'=>hexdec($r),
			'g'=>hexdec($g),
			'b'=>hexdec($b)
		);
	}
	return $rgb;
}
Copy after login

For example:

print_r(hex2rgb('#F03'));
//输出:Array ( [r] => 255 [g] => 0 [b] => 51 )
Copy after login

Articles you may be interested in

  • php How to convert br newline character in html to newline character in text input
  • php Convert br newline character in string Convert multiple consecutive spaces into one space
  • php converts full-width characters in a string to half-width characters
  • How js converts the returned string into json format data
  • php uses the session_set_save_handler() function to save the session to the MySQL database
  • php method to convert the IP address to a real address
  • php uses the ZipArchive function to compress and decompress files
  • PHP implements restriction of domain names to protect source code from being copied

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000763.htmlTechArticlephp function converts the hexadecimal color code into RGB color value using the php function to convert the 16 forbidden color code into RGB color value. /** * function converts hexadecimal color to RGB color value* author www.php...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template