Guest User

Untitled

a guest
Jan 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Change the brightness of the passed in color
  5. *
  6. * $diff should be negative to go darker, positive to go lighter and
  7. * is subtracted from the decimal (0-255) value of the color
  8. *
  9. * @param string $hex color to be modified
  10. * @param string $diff amount to change the color
  11. * @return string hex color
  12. */
  13. public function hex_color_mod($hex, $diff) {
  14. $rgb = str_split(trim($hex, '# '), 2);
  15.  
  16. foreach ($rgb as &$hex) {
  17. $dec = hexdec($hex);
  18. if ($diff >= 0) {
  19. $dec += $diff;
  20. }
  21. else {
  22. $dec -= abs($diff);
  23. }
  24. $dec = max(0, min(255, $dec));
  25. $hex = str_pad(dechex($dec), 2, '0', STR_PAD_LEFT);
  26. }
  27.  
  28. return '#'.implode($rgb);
  29. }
Add Comment
Please, Sign In to add comment