Skip to content

Instantly share code, notes, and snippets.

@p01
Forked from stdclass/example.js
Created June 2, 2011 20:09
Show Gist options
  • 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" ]
}
@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