Created
December 1, 2014 22:30
-
-
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.
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
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