Home  >  Article  >  Backend Development  >  PHP color value conversion PHP realizes the conversion of hexadecimal numbers and color values

PHP color value conversion PHP realizes the conversion of hexadecimal numbers and color values

WBOY
WBOYOriginal
2016-07-25 08:56:131260browse
  1. /**

  2. * Conversion between color values ​​and hexadecimal numbers
  3. * edit: WWW.JBXUE.COM
  4. */
  5. function toHex($N) {
  6. if ($N==NULL) return "00";
  7. if ($N ==0) return "00";
  8. $N=max(0,$N);
  9. $N=min($N,255);
  10. $N=round($N);
  11. $string = "0123456789ABCDEF" ;
  12. $val = (($N-$N%16)/16);
  13. $s1 = $string{$val};
  14. $val = ($N%16);
  15. $s2 = $string{$val };
  16. return $s1.$s2;
  17. }

  18. //Convert color value to hexadecimal number

  19. function rgb2hex($r,$g,$b){
  20. return toHex( $r).toHex($g).toHex($b);
  21. }
  22. //Convert hexadecimal numbers to color values
  23. function hex2rgb($N){
  24. $dou = str_split($N,2);
  25. return array(
  26. "R" => hexdec($dou[0]),
  27. "G" => hexdec($dou[1]),
  28. "B" => hexdec($dou[2])
  29. );
  30. }

  31. echo rgb2hex(106,48,48);

  32. echo '

    ';
  33. print_r(hex2rgb("6A3030"));
  34. ?>

Copy code


Statement:
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