Skip to content

Instantly share code, notes, and snippets.

@blendsdk
Last active May 23, 2016 08:28
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 blendsdk/1b97b5ed1351974f94fbe501ea201ab3 to your computer and use it in GitHub Desktop.
Save blendsdk/1b97b5ed1351974f94fbe501ea201ab3 to your computer and use it in GitHub Desktop.
// Extract Material Design Colors from the specs website.
// open [https://www.google.com/design/spec/style/color.html#color-color-palette] in FireBug
// Then run
console.clear();
var mdPalette = {
};
var sassPalette = [
];
var colorPalette = document.querySelector('.color-palette');
var colorGroups = colorPalette.querySelectorAll('.color-group');
var lastColorGroup = null;
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
}
: null;
}
function addSassColor(colorName, colorWeight, colorCode) {
var name = ('$color-' + colorName + '-' + colorWeight).toLowerCase() + ': ' + colorCode + ';';
if(colorName !== lastColorGroup) {
sassPalette.push("// " + colorName);
lastColorGroup = colorName;
}
sassPalette.push(name);
}
Array.prototype.forEach.call(colorGroups, function (colorGroup) {
var colorName = null;
Array.prototype.forEach.call(colorGroup.children[0].children, function (colorPart) {
var colorWeight = null;
var colorCode = null;
if (colorPart.children.length === 3) {
colorName = colorPart.children[0].innerHTML;
colorWeight = colorPart.children[1].innerHTML;
colorCode = colorPart.children[2].innerHTML;
} else {
colorWeight = colorPart.children[0].innerHTML;
colorCode = colorPart.children[1].innerHTML;
}
colorName = (colorName || 'BlackAndWhite').replace(' ', '');
mdPalette[colorName] = mdPalette[colorName] || {
}
if (colorWeight[0] !== 'A') {
colorWeight = 'C' + colorWeight;
}
mdPalette[colorName][colorWeight] = colorCode
addSassColor(colorName, colorWeight, colorCode)
});
});
console.log(sassPalette.join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment