Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- function hex2rgbs(string $colour, float $alpha = 1) {
- if (!empty($colour)) {
- if($alpha >= 0.95) {
- return $colour;
- }
- else {
- if($colour[0] == '#') {
- $colour = substr($colour, 1);
- }
- if(strlen($colour) == 6) {
- list($r, $g, $b) = array(
- $colour[0] . $colour[1],
- $colour[2] . $colour[3],
- $colour[4] . $colour[5]
- );
- }
- elseif (strlen($colour) == 3) {
- list($r, $g, $b) = array(
- $colour[0] . $colour[0],
- $colour[1] . $colour[1],
- $colour[2] . $colour[2]
- );
- }
- else {
- return false;
- }
- $r = hexdec($r);
- $g = hexdec($g);
- $b = hexdec($b);
- $output = array(
- 'red' => $r,
- 'green' => $g,
- 'blue' => $b
- );
- return 'rgba(' . implode($output, ',') . ',' . $alpha . ')';
- }
- }
- }
- $s = "{color hex=\"fff\" alpha=\"0.8\"} {color hex=\"fff\"}";
- $s = preg_replace_callback("#\{color hex=\"(\w+)\"\s?(alpha=\"([0-9.]+)\")?\}#is", function($matches) {
- return hex2rgbs($matches[1], ($matches[3]) ? $matches[3] : 1);
- }, $s);
- echo $s;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement