Skip to content

Commit

Permalink
Fixes #632 - partial support for #rrggbbaa
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed Apr 24, 2019
1 parent 7c6dc5c commit a384906
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
50 changes: 37 additions & 13 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 3 additions & 1 deletion tests/inputs/scss_css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -982,4 +982,6 @@ foo {
a: foo();
b: bar baz-bang() bip; }


.alpha {
color: #1f2526bf;
background-color: #1f2526ff; }
4 changes: 4 additions & 0 deletions tests/outputs/scss_css.css
Original file line number Diff line number Diff line change
Expand Up @@ -767,3 +767,7 @@ foo {
foo {
a: foo();
b: bar baz-bang() bip; }

.alpha {
color: rgba(31, 37, 38, 0.749);
background-color: #1f2526; }
4 changes: 4 additions & 0 deletions tests/outputs_numbered/scss_css.css
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

0 comments on commit a384906

Please sign in to comment.