如何编写PHP代码将任意颜色转换为它的反色?

2026-04-05 16:332阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计164个文字,预计阅读时间需要1分钟。

如何编写PHP代码将任意颜色转换为它的反色?

这段PHP代码可以用来将一个颜色转换为与它相反的颜色。例如,白色变成黑色,蓝色变成黄色。以下是改写后的代码:

phpfunction color_inverse($color) { $color=str_replace('#', '', $color); $r=hexdec(substr($color, 0, 2)); $g=hexdec(substr($color, 2, 2)); $b=hexdec(substr($color, 4, 2)); $newColor='#'; $newColor .=dechex(255 - $r); $newColor .=dechex(255 - $g); $newColor .=dechex(255 - $b); return $newColor;}

这段php代码可以把一个颜色变成与之相反的颜色编码,如:白色变成黑色,蓝色变成黄色

function color_inverse($color){ $color = str_replace('#', '', $color); if (strlen($color) != 6){ return '000000'; } $rgb = ''; for ($x=0;$x<3;$x++){ $c = 255 - hexdec(substr($color,(2*$x),2)); $c = ($c < 0) ? 0 : dechex($c); $rgb .= (strlen($c) < 2) ? '0'.$c : $c; } return '#'.$rgb; } //使用范例: // black -> white print color_inverse('#000000'); // --> returns #ffffff // blue -> yellow print color_inverse('#0000FF'); // --> #FFFF00

如何编写PHP代码将任意颜色转换为它的反色?

本文共计164个文字,预计阅读时间需要1分钟。

如何编写PHP代码将任意颜色转换为它的反色?

这段PHP代码可以用来将一个颜色转换为与它相反的颜色。例如,白色变成黑色,蓝色变成黄色。以下是改写后的代码:

phpfunction color_inverse($color) { $color=str_replace('#', '', $color); $r=hexdec(substr($color, 0, 2)); $g=hexdec(substr($color, 2, 2)); $b=hexdec(substr($color, 4, 2)); $newColor='#'; $newColor .=dechex(255 - $r); $newColor .=dechex(255 - $g); $newColor .=dechex(255 - $b); return $newColor;}

这段php代码可以把一个颜色变成与之相反的颜色编码,如:白色变成黑色,蓝色变成黄色

function color_inverse($color){ $color = str_replace('#', '', $color); if (strlen($color) != 6){ return '000000'; } $rgb = ''; for ($x=0;$x<3;$x++){ $c = 255 - hexdec(substr($color,(2*$x),2)); $c = ($c < 0) ? 0 : dechex($c); $rgb .= (strlen($c) < 2) ? '0'.$c : $c; } return '#'.$rgb; } //使用范例: // black -> white print color_inverse('#000000'); // --> returns #ffffff // blue -> yellow print color_inverse('#0000FF'); // --> #FFFF00

如何编写PHP代码将任意颜色转换为它的反色?