Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any chance of an API? #10

Open
geotheory opened this issue Nov 19, 2014 · 10 comments
Open

Any chance of an API? #10

geotheory opened this issue Nov 19, 2014 · 10 comments

Comments

@geotheory
Copy link

This is such a useful tool I find myself using it regularly. Is there any chance of an API so we could automate the process for non-javascript applications, or perhaps a R / Python version? I suspect most real data science happens off-browser..

@ehartford
Copy link

or a javascript library, then you could always just spawn a node process that generates your pallette from command line arguments.

@geotheory
Copy link
Author

I see what you did there. You are of course right and I'm certainly being lazy, but I haven't yet cracked calling javascript from R. I also did look at the source code but it seemed a bit too complicated to port..

@ehartford
Copy link

I think I could take a crack at answering this on StackOverflow if you would like to ask it there.

@geotheory
Copy link
Author

That's jolly helpful Eric, thanks I'll do that tomorrow

@ehartford
Copy link

My curiosity got the better of me.

First, copy chroma.js and chroma.palette-gen.js into a directory.

You need to modify chroma.js and remove the first and last line. "(function(){" and "}).call(this);"

Then, make a new file called something like "generatePalette.js" like this:

var fs = require('fs');
eval(fs.readFileSync('./chroma.js', 'utf8'));
eval(fs.readFileSync('./chroma.palette-gen.js', 'utf8'));

// defaults
var hmin  = 0,
    hmax  = 360,
    cmin  = 0,
    cmax  = 3,
    lmin  = 0,
    lmax  = 1.5,
    q     = 50,
    useFV = false, // Force vector or kMeans
    colorsCount = 7;

// if you want to set more variables from the command line arguments, this is where you'd do it.
// I will just set the colorsCount.
if(process.argv.length > 2) {
    colorsCount = parseInt(process.argv[2], 10);
}

var hcondition = hmin < hmax ? 
    function(hcl){return hcl[0]>=hmin && hcl[0]<=hmax} :
    function(hcl){return hcl[0]>=hmin || hcl[0]<=hmax}; 
var ccondition = function(hcl){return hcl[1]>=cmin && hcl[1]<=cmax};
var lcondition = function(hcl){return hcl[2]>=lmin && hcl[2]<=lmax};

function colorspaceSelector(color) {    
    var hcl = color.hcl();
    return hcondition(hcl) && ccondition(hcl) && lcondition(hcl);
};

var colors = paletteGenerator.generate(colorsCount, colorspaceSelector, useFV, q);
colors = paletteGenerator.diffSort(colors);
colors = colors.map( function( color ){ 
    return { color:color, hex:color.hex(), hcl:color.hcl(), lab:color.lab() } 
});

colors.forEach(function(color){
    console.log(color.hex);
});

Now you can run it like node generatePalette.js 4 and it will make 4 colors.

harte037$ node ./genpallette.js 4
#A25540
#95BD55
#9E5EAB
#7F9C9A

So the second part is to run this from R or Python, then you can use the output.
http://stat.ethz.ch/R-manual/R-devel/library/base/html/system.html
http://sweetme.at/2014/02/17/a-simple-approach-to-execute-a-node.js-script-from-python/

@ehartford
Copy link

Btw this is slow so for perf you should pre-generate your palettes and store them as constants. I plan to generate sets of size 1 to 20. I don't imagine I'll ever have more than 20 series' in a chart. And if you are running this script by hand then you don't really need the R / Python integration right?

@hoesler
Copy link

hoesler commented Jan 21, 2015

I created a simple package to generate an Iwanthue palette in R: https://github.com/hoesler/rwantshue
Maybe someone wants to try it out.
Cheers

@geotheory
Copy link
Author

(edited) Very nice work guys. Eric, I now see your method to implement arguments as per colorsCount..

@hoesler does your library implement arguments for hue, chomra or lightness? IWantHue-class page makes no mention. Do you need an updated generatePalette.js?

@hoesler
Copy link

hoesler commented Jan 22, 2015

Indeed, the feature was missing. I added a color_space argument and the list of presets as defined on the iwanthue webpage.

Am 22.01.2015 um 13:01 schrieb Robin Edwards notifications@github.com:

Very nice work guys. Please forgive me however I can't figure out how to input hue, chomra or lightness arguments using either method. Have I missed something in the documentation? hoesler's IWantHue-class page makes no mention or arguments.


Reply to this email directly or view it on GitHub.

@andrewliebchen
Copy link

Forked this project and stripped it down to the color palette generator. You can find the package on npm.

If you use npm and require.js in your project, using iWantHueAPI is pretty easy. From your command line:

npm install iwanthue-api

Then include it in your project:

var iwanthue = require('iwanthue-api');

More documentation is on the Github repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants