Skip to content

Commit a384906

Browse files
committed
Fixes #632 - partial support for #rrggbbaa
1 parent 7c6dc5c commit a384906

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

src/Parser.php

+37-13
Original file line numberDiff line numberDiff line change
@@ -1706,22 +1706,46 @@ protected function map(&$out)
17061706
protected function color(&$out)
17071707
{
17081708
$color = [Type::T_COLOR];
1709+
$s = $this->count;
1710+
1711+
if ($this->match('(#([0-9a-f]+))', $m)) {
1712+
$nofValues = strlen($m[2]);
1713+
$hasAlpha = $nofValues === 4 || $nofValues === 8;
1714+
$channels = $hasAlpha ? [4, 3, 2, 1] : [3, 2, 1];
1715+
1716+
switch ($nofValues) {
1717+
case 3:
1718+
case 4:
1719+
$num = hexdec($m[2]);
1720+
1721+
foreach ($channels as $i) {
1722+
$t = $num & 0xf;
1723+
$color[$i] = $t << 4 | $t;
1724+
$num >>= 4;
1725+
}
1726+
break;
17091727

1710-
if ($this->match('(#([0-9a-f]{6})|#([0-9a-f]{3}))', $m)) {
1711-
if (isset($m[3])) {
1712-
$num = hexdec($m[3]);
1728+
case 6:
1729+
case 8:
1730+
$num = hexdec($m[2]);
17131731

1714-
foreach ([3, 2, 1] as $i) {
1715-
$t = $num & 0xf;
1716-
$color[$i] = $t << 4 | $t;
1717-
$num >>= 4;
1718-
}
1719-
} else {
1720-
$num = hexdec($m[2]);
1732+
foreach ($channels as $i) {
1733+
$color[$i] = $num & 0xff;
1734+
$num >>= 8;
1735+
}
1736+
break;
1737+
1738+
default:
1739+
$this->seek($s);
17211740

1722-
foreach ([3, 2, 1] as $i) {
1723-
$color[$i] = $num & 0xff;
1724-
$num >>= 8;
1741+
return false;
1742+
}
1743+
1744+
if ($hasAlpha) {
1745+
if ($color[4] === 255) {
1746+
$color[4] = 1; // fully opaque
1747+
} else {
1748+
$color[4] = round($color[4] / 255, 3);
17251749
}
17261750
}
17271751

tests/inputs/scss_css.scss

+3-1
Original file line numberDiff line numberDiff line change
@@ -982,4 +982,6 @@ foo {
982982
a: foo();
983983
b: bar baz-bang() bip; }
984984

985-
985+
.alpha {
986+
color: #1f2526bf;
987+
background-color: #1f2526ff; }

tests/outputs/scss_css.css

+4
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,7 @@ foo {
767767
foo {
768768
a: foo();
769769
b: bar baz-bang() bip; }
770+
771+
.alpha {
772+
color: rgba(31, 37, 38, 0.749);
773+
background-color: #1f2526; }

tests/outputs_numbered/scss_css.css

+4
Original file line numberDiff line numberDiff line change
@@ -786,3 +786,7 @@ foo {
786786
foo {
787787
a: foo();
788788
b: bar baz-bang() bip; }
789+
/* line 985, inputs/scss_css.scss */
790+
.alpha {
791+
color: rgba(31, 37, 38, 0.749);
792+
background-color: #1f2526; }

0 commit comments

Comments
 (0)