Home > Backend Development > PHP Tutorial > 给定一个颜色值,如何把它转换成一个深一点的值

给定一个颜色值,如何把它转换成一个深一点的值

WBOY
Release: 2016-06-13 12:07:40
Original
994 people have browsed it

给定一个颜色值,怎么把它转换成一个深一点的值?
a = "#333333"
需要这个函数:把a的色彩变深一点,比如a="#222222"

------解决思路----------------------

$a = "#333333";<br />$rgb = unpack('C*', pack('H*', substr($a, 1)));<br />$hsb = call_user_func_array('rgb2hsb', $rgb);<br />$hsb[2] *= 0.67;<br />$rgb = call_user_func_array('hsb2rgb', $hsb);<br />echo '#' . join('', array_map('dechex', $rgb));<br /><br /><br />function rgb2hsb($rgbR, $rgbG, $rgbB) {<br />    $rgbR &= 255;<br />    $rgbG &= 255;<br />    $rgbB &= 255;<br />    $rgb = array($rgbR, $rgbG, $rgbB );<br />    sort($rgb);<br />    $max = $rgb[2];<br />    $min = $rgb[0];<br /> <br />    $hsbB = $max / 255.0;<br />    $hsbS = $max == 0 ? 0 : ($max - $min) / $max;<br /> <br />    $hsbH = 0;<br />    if ($max == $rgbR && $rgbG >= $rgbB) {<br />        $hsbH = ($rgbG - $rgbB) * 60 / (($m = $max - $min) ? $m : 1);<br />    } else if ($max == $rgbR && $rgbG < $rgbB) {<br />        $hsbH = ($rgbG - $rgbB) * 60 / ($max - $min) + 360;<br />    } else if ($max == $rgbG) {<br />        $hsbH = ($rgbB - $rgbR) * 60 / ($max - $min) + 120;<br />    } else if ($max == $rgbB) {<br />        $hsbH = ($rgbR - $rgbG) * 60 / ($max - $min) + 240;<br />    }<br /> <br />    return array( $hsbH, $hsbS, $hsbB );<br />}<br /> <br />function hsb2rgb($h, $s, $v) {<br />    $r = $g = $b = 0;<br />    $i = ($h / 60) % 6;<br />    $f = ($h / 60) - $i;<br />    $p = $v * (1 - $s);<br />    $q = $v * (1 - $f * $s);<br />    $t = $v * (1 - (1 - $f) * $s);<br />    switch ($i) {<br />    case 0:<br />        $r = $v;<br />        $g = $t;<br />        $b = $p;<br />        break;<br />    case 1:<br />        $r = $q;<br />        $g = $v;<br />        $b = $p;<br />        break;<br />    case 2:<br />        $r = $p;<br />        $g = $v;<br />        $b = $t;<br />        break;<br />    case 3:<br />        $r = $p;<br />        $g = $q;<br />        $b = $v;<br />        break;<br />    case 4:<br />        $r = $t;<br />        $g = $p;<br />        $b = $v;<br />        break;<br />    case 5:<br />        $r = $v;<br />        $g = $p;<br />        $b = $q;<br />        break;<br />    default:<br />        break;<br />    }<br />    return array( intval($r * 255), intval($g * 255), intval($b * 255) );<br />}<br />
Copy after login
#222222

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