Skip to content

Instantly share code, notes, and snippets.

@p01
Forked from stdclass/example.js
Created June 2, 2011 20:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p01/1005192 to your computer and use it in GitHub Desktop.
Save p01/1005192 to your computer and use it in GitHub Desktop.
Make RGB Colors Lighter / Darker
/*
* Make RGB-Colors lighter / darker
*/
var color = function(c,n,i,d){for(i=3;i--;c[i]=d<0?0:d>255?255:d|0)d=c[i]+n;return c}
/**
* @param array RGB Colors
* @param number Amount
*/
color( [225, 100, 10 ], -90 ); // returns [135,10,0]
color( [225, 100, 10 ], 70 ); // returns [255,170,80]
function(c,n,i,d){for(i=3;i--;c[i]=d<0?0:d>255?255:d|0)d=c[i]+n;return c}
{
"name": "darker color",
"keywords": [ "color", "darker", "lighter" ]
}
@atesgoral
Copy link

Nice one! We're all still waiting for a wolf140b...

@p01
Copy link
Author

p01 commented Jun 3, 2011

Wolf140b? You sir have no idea what you just unleashed.

@maettig
Copy link

maettig commented Nov 10, 2011

Save 1 byte? I'm not sure if this works in all web browsers but I think so.

function(c,n,i,d){for(i in c)d=c[i]+n,c[i]=d<0?0:d>255?255:d|0;return c}

@yckart
Copy link

yckart commented Oct 29, 2014

ES5 version, saves 1 byte:

function(c,n){return c.map(function(d){return(d+=n)<0?0:d>255?255:d|0})}

@yckart
Copy link

yckart commented May 22, 2016

ES6 version, saves 32 byte:

(c,n)=>c.map(d=>(d+=n)<0?0:d>255?255:d|0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment