Skip to content

Commit 94867fd

Browse files
committed
Optimize output of color values
Useful to manipulate colors in integers in PHP code. But once output, there really is no difference. And this: #fff is much more space-efficient than this: rgb(255,255,255) (at least for this example, the former is 400% more efficient)
1 parent a80739f commit 94867fd

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

lib/Sabberworm/CSS/Value/Color.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,21 @@ public function getColorDescription() {
2121
return $this->getName();
2222
}
2323

24-
}
24+
public function __toString() {
25+
if (isset($this->aComponents['a'])) {
26+
return parent::__toString();
27+
}
28+
29+
$out = sprintf(
30+
'%o2x%02x%02x',
31+
$this->aComponents['r'],
32+
$this->aComponents['g'],
33+
$this->aComponents['b'],
34+
);
35+
36+
return (($out[0] == $out[1]) && ($out[2] == $out[3]) && ($out[4] == $out[5]))
37+
? '#' . $out[0] . $out[2] . $out[4]
38+
: '#' . $out;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)