Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created July 24, 2014 00:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuhei/981d16b3e5d58a8c5f0e to your computer and use it in GitHub Desktop.
Save shuhei/981d16b3e5d58a8c5f0e to your computer and use it in GitHub Desktop.
Extract color palette of Google's Material Design. Run on http://www.google.com/design/spec/style/color.html
// Create an Array from an Array-like Object such as NodeList.
function toArray(arrayLike) {
return Array.prototype.slice.call(arrayLike);
}
// Create an Object from an Array of objects.
function toObject(objects, keyName, valueName) {
return objects.reduce(function(acc, obj) {
acc[obj[keyName]] = obj[valueName];
return acc;
}, {});
}
function pickColor(item) {
var shade = item.querySelector('.shade').innerText;
var hex = item.querySelector('.hex').innerText;
return { shade: shade, hex: hex };
}
function pickGroupColors(group) {
var colorName = group.querySelector('li.main-color .name').innerText;
var name = colorName.toLowerCase().split(' ').join('-');
var items = toArray(group.querySelectorAll('li.color:not(.main-color)'));
var colors = toObject(items.map(pickColor), 'shade', 'hex');
return { name: name, colors: colors };
}
var groups = toArray(document.querySelectorAll('.color-group'));
var colorSet = toObject(groups.map(pickGroupColors), 'name', 'colors');
console.log(JSON.stringify(colorSet));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment