Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. <?php
  2.  
  3. function hex2rgba($hex, $a = 0)
  4. {
  5. $hex = ltrim($hex, '#');
  6.  
  7. if (strlen($hex) == 3) {
  8. $hex = $this->completeHex($hex);
  9. }
  10.  
  11. $this->checkHexFormat($hex);
  12.  
  13. $hexArray = str_split($hex,2); //for get r-g-b values
  14.  
  15. $hexArray = array_map(function ($item) {
  16. return hexdec($item);
  17. }, $hexArray);
  18.  
  19. $hexArray['alpha'] = $a;
  20.  
  21. return "rgb($hexArray[0], $hexArray[1], $hexArray[2], $hexArray[alpha])";
  22. }
  23.  
  24. function completeHex($hex)
  25. {
  26. return $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
  27. }
  28.  
  29. function checkHexFormat($hex)
  30. {
  31. if (strlen($hex) !== 6) {
  32. throw new Exception('hex code can be 6 digit');
  33. }
  34.  
  35. if (ctype_xdigit($hex) == false)
  36. {
  37. throw new Exception('unknown format');
  38. }
  39. }
  40.  
  41. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement