Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. <?
  2. function hex2rgbs(string $colour, float $alpha = 1) {
  3. if (!empty($colour)) {
  4. if($alpha >= 0.95) {
  5. return $colour;
  6. }
  7. else {
  8. if($colour[0] == '#') {
  9. $colour = substr($colour, 1);
  10. }
  11. if(strlen($colour) == 6) {
  12. list($r, $g, $b) = array(
  13. $colour[0] . $colour[1],
  14. $colour[2] . $colour[3],
  15. $colour[4] . $colour[5]
  16. );
  17. }
  18. elseif (strlen($colour) == 3) {
  19. list($r, $g, $b) = array(
  20. $colour[0] . $colour[0],
  21. $colour[1] . $colour[1],
  22. $colour[2] . $colour[2]
  23. );
  24. }
  25. else {
  26. return false;
  27. }
  28. $r = hexdec($r);
  29. $g = hexdec($g);
  30. $b = hexdec($b);
  31. $output = array(
  32. 'red' => $r,
  33. 'green' => $g,
  34. 'blue' => $b
  35. );
  36. return 'rgba(' . implode($output, ',') . ',' . $alpha . ')';
  37. }
  38. }
  39. }
  40. $s = "{color hex=\"fff\" alpha=\"0.8\"} {color hex=\"fff\"}";
  41. $s = preg_replace_callback("#\{color hex=\"(\w+)\"\s?(alpha=\"([0-9.]+)\")?\}#is", function($matches) {
  42. return hex2rgbs($matches[1], ($matches[3]) ? $matches[3] : 1);
  43. }, $s);
  44. echo $s;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement