Skip to content

Instantly share code, notes, and snippets.

@leonderijke
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonderijke/f4714ab410761194a253 to your computer and use it in GitHub Desktop.
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.
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