Skip to content

Commit a95fdeb

Browse files
committed
Add support for rgb(a) colors
1 parent 7ee457d commit a95fdeb

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,18 @@ function getImageUrl(rule) {
678678
* @return {String|null}
679679
*/
680680
function getColor(decl) {
681-
return decl.value.match(/(?:\s*)(#([0-9a-f]{3}){1,2})/gi);
681+
var regexes = ['(#([0-9a-f]{3}){1,2})', 'rgba?\\([^\\)]+\\)'];
682+
var matches = null;
683+
684+
lodash.forEach(regexes, function(regex) {
685+
var regex = new RegExp(regex, 'gi');
686+
687+
if (regex.test(decl.value)) {
688+
matches = decl.value.match(regex);
689+
}
690+
});
691+
692+
return matches;
682693
}
683694

684695
/**
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.selector-a { background-image: url(sprite.basic-color.png); background-position: 0 0; }
22
.selector-b { background-image: url(sprite.basic-color.png); background-position: -25px 0; background-color: #000; }
3+
.selector-c { background-image: url(sprite.basic-color.png); background-position: -25px 0; background-color: rgba(255, 255, 255, .2); }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.selector-a { background-image: url(circle.png); }
22
.selector-b { background: #000 url(square.png) no-repeat 0 0; }
3+
.selector-c { background: rgba(255, 255, 255, .2) url(square.png) no-repeat 0 0; }

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tape('should create a sprite and update references in stylesheet', function(t) {
4747
});
4848
});
4949

50-
tape('should extract hex color as background-color', function(t) {
50+
tape('should extract hex, rgba and rgb colors as background-color', function(t) {
5151
t.plan(2);
5252

5353
var pluginOpts = {

0 commit comments

Comments
 (0)