colorChajian.php 873 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. /**
  3. 颜色操作
  4. */
  5. class colorChajian extends Chajian{
  6. /**
  7. 颜色
  8. */
  9. public function color($color,$l=127.5)
  10. {
  11. $r=hexdec(substr($color,1,2));
  12. $g=hexdec(substr($color,3,2));
  13. $b=hexdec(substr($color,5));
  14. $yb=127.5;
  15. if($l > $yb){
  16. $l = $l - $yb;
  17. $r = ($r * ($yb - $l) + 255 * $l) / $yb;
  18. $g = ($g * ($yb - $l) + 255 * $l) / $yb;
  19. $b = ($b * ($yb - $l) + 255 * $l) / $yb;
  20. }else{
  21. $r = ($r * $l) / $yb;
  22. $g = ($g * $l) / $yb;
  23. $b = ($b * $l) / $yb;
  24. }
  25. $nr=$this->tohex($r);
  26. $ng=$this->tohex($g);
  27. $nb=$this->tohex($b);
  28. return '#'.$nr.$ng.$nb;
  29. }
  30. private function tohex($n)
  31. {
  32. $hexch = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
  33. $n = round($n);
  34. $l = $n % 16;
  35. $h = floor(($n / 16)) % 16;
  36. return ''.$hexch[$h].''.$hexch[$l].'';
  37. }
  38. }
粤ICP备19079148号