Home  >  Article  >  Backend Development  >  PHP code to convert a color to its inverse color

PHP code to convert a color to its inverse color

WBOY
WBOYOriginal
2016-07-25 08:45:311130browse

This php code can change a color into the opposite color encoding, such as: white becomes black, blue becomes yellow

  1. function color_inverse($color){
  2. $color = str_replace('# ', '', $color);
  3. if (strlen($color) != 6){ return '000000'; }
  4. $rgb = '';
  5. for ($x=0;$x<3;$x++ ){
  6. $c = 255 - hexdec(substr($color,(2*$x),2));
  7. $c = ($c < 0) ? 0 : dechex($c);
  8. $rgb . = (strlen($c) < 2) ? '0'.$c : $c;
  9. }
  10. return '#'.$rgb;
  11. }
  12. //Usage example:
  13. // black -> white
  14. print color_inverse('#000000');
  15. // --> returns #ffffff
  16. // blue -> yellow
  17. print color_inverse('#0000FF');
  18. // --> #FFFF00
Copy code

Convert to, and vice versa, PHP


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