diff --git a/src/Parser.php b/src/Parser.php index e67b7c8b..1eda928f 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -1706,22 +1706,46 @@ protected function map(&$out) protected function color(&$out) { $color = [Type::T_COLOR]; + $s = $this->count; + + if ($this->match('(#([0-9a-f]+))', $m)) { + $nofValues = strlen($m[2]); + $hasAlpha = $nofValues === 4 || $nofValues === 8; + $channels = $hasAlpha ? [4, 3, 2, 1] : [3, 2, 1]; + + switch ($nofValues) { + case 3: + case 4: + $num = hexdec($m[2]); + + foreach ($channels as $i) { + $t = $num & 0xf; + $color[$i] = $t << 4 | $t; + $num >>= 4; + } + break; - if ($this->match('(#([0-9a-f]{6})|#([0-9a-f]{3}))', $m)) { - if (isset($m[3])) { - $num = hexdec($m[3]); + case 6: + case 8: + $num = hexdec($m[2]); - foreach ([3, 2, 1] as $i) { - $t = $num & 0xf; - $color[$i] = $t << 4 | $t; - $num >>= 4; - } - } else { - $num = hexdec($m[2]); + foreach ($channels as $i) { + $color[$i] = $num & 0xff; + $num >>= 8; + } + break; + + default: + $this->seek($s); - foreach ([3, 2, 1] as $i) { - $color[$i] = $num & 0xff; - $num >>= 8; + return false; + } + + if ($hasAlpha) { + if ($color[4] === 255) { + $color[4] = 1; // fully opaque + } else { + $color[4] = round($color[4] / 255, 3); } } diff --git a/tests/inputs/scss_css.scss b/tests/inputs/scss_css.scss index 7daa89a3..8dee304f 100644 --- a/tests/inputs/scss_css.scss +++ b/tests/inputs/scss_css.scss @@ -982,4 +982,6 @@ foo { a: foo(); b: bar baz-bang() bip; } - +.alpha { + color: #1f2526bf; + background-color: #1f2526ff; } diff --git a/tests/outputs/scss_css.css b/tests/outputs/scss_css.css index b26a1c5b..22fcf63f 100644 --- a/tests/outputs/scss_css.css +++ b/tests/outputs/scss_css.css @@ -767,3 +767,7 @@ foo { foo { a: foo(); b: bar baz-bang() bip; } + +.alpha { + color: rgba(31, 37, 38, 0.749); + background-color: #1f2526; } diff --git a/tests/outputs_numbered/scss_css.css b/tests/outputs_numbered/scss_css.css index 6da0ddac..4d89beb6 100644 --- a/tests/outputs_numbered/scss_css.css +++ b/tests/outputs_numbered/scss_css.css @@ -786,3 +786,7 @@ foo { foo { a: foo(); b: bar baz-bang() bip; } +/* line 985, inputs/scss_css.scss */ +.alpha { + color: rgba(31, 37, 38, 0.749); + background-color: #1f2526; }