Last active
August 29, 2015 14:10
-
-
Save leonderijke/f4714ab410761194a253 to your computer and use it in GitHub Desktop.
Quick and dirty way of find unique RGB(A) colors in a CSS file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require("lodash"); | |
var fs = require("fs"); | |
var parser = require("css"); | |
var file = fs.readFileSync("./css/pagedetail.css", "utf-8").toString(); | |
var parsed = parser.parse(file); | |
var uniqueColors = _.chain(parsed.stylesheet.rules) | |
// Get declarations for each rule | |
.pluck("declarations") | |
// Remove falsey values from array | |
.compact() | |
// Flatten nested declarations | |
.flatten() | |
// Get the value from each declaration | |
.pluck("value") | |
// Retrieve all rgb(a) strings from the value | |
.map(function(value) { | |
return value.match(/rgba?\([^\)]*\)/g); | |
}) | |
// Flatten nested rgb(a) strings | |
.flatten() | |
// Just the unique colors please | |
.uniq() | |
// Get the result array from the LoDash object | |
.value(); | |
console.log(uniqueColors.join("\n")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment