Last active
August 18, 2016 15:39
-
-
Save byelipk/bcbdb7540456f3fdb09dbf08b8e3b0ab to your computer and use it in GitHub Desktop.
Takes a hex code as input and returns the name of the color as determined by http://chir.ag/projects/name-that-color/. Runs from the command line. Requires Phantom JS
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
var page = require('webpage').create(); | |
var system = require('system'); | |
if (system.args.length === 1) { | |
console.log('Usage: path/to/get-color-name.js "#FA13CC"'); | |
phantom.exit(); | |
} | |
// NOTE | |
// Hex code must be 6 chars long with an optional | |
var rxp = new RegExp(/^#[a-fA-F0-9]{6}$/); | |
var hexCode = system.args[1]; | |
if (!rxp.test(hexCode)) { | |
console.log('Invalid hex code'); | |
phantom.exit(); | |
} | |
var url = 'http://chir.ag/projects/name-that-color/' + hexCode; | |
page.onConsoleMessage = function(msg) { | |
console.log(msg); | |
}; | |
console.log("Opening URL: " + url); | |
page.open(url, function (status) { | |
console.log("Page is loaded..."); | |
console.log("Getting name. This may take a few moments..."); | |
window.setTimeout(function() { | |
var colorName = page.evaluate(function() { | |
var element = document.querySelector("h2#colorname"); | |
if (!element) { | |
return "ERROR: The element 'h2#colorname' was missing."; | |
} | |
var content = element.textContent; | |
/** | |
NOTE | |
Some colors have modifiers. The only two | |
I know about right now are: | |
solid | |
approx | |
*/ | |
return content.replace(/(solid|approx\.)$/, ''); | |
}); | |
console.log(colorName); | |
phantom.exit(); | |
}, 1000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment