Skip to content

Instantly share code, notes, and snippets.

@chrisma
Created December 1, 2014 22:30
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 chrisma/d181d1222de2f7fb11f4 to your computer and use it in GitHub Desktop.
Save chrisma/d181d1222de2f7fb11f4 to your computer and use it in GitHub Desktop.
Extract information about colors (name, rgb, hex, hsv, hsl) from Wikipedia to JSON.
result = [];
$('.wikitable tbody tr').each(function(){
var color = {};
color['name'] = $(this).find('th').text();
var tds = $(this).find('td');
color['hex'] = tds.first().text();
var rgb = [];
tds.slice(1,4).each(function(){
rgb.push(parseInt($(this).attr('title').replace('/255','')));
});
color['rgb'] = rgb;
var hue = parseInt(tds.slice(4,5).contents().filter(function(){
return this.nodeType == 3;
}).text().replace('°',''));
var saturation_hsl = parseInt(tds.slice(5,6).text().replace('%',''));
var lightness = parseInt(tds.slice(6,7).text().replace('%',''));
color['hsl'] = [hue, saturation_hsl, lightness];
var saturation_hsv = parseInt(tds.slice(7,8).text().replace('%',''));
var value = parseInt(tds.slice(8,9).text().replace('%',''));
color['hsv'] = [hue, saturation_hsv, value];
result.push(color);
});
var json = JSON.stringify(result, null, 2);
//copy (to the clipboard) only works in the Chrome Dev console
copy(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment