A fast trick to lighten/darken colors



2 Comments

Share





Running example


/**
 * Scales r-g-b channels by 'scale' factor, having the r-g-b proportions saved
 * @param	color:uint		color to be scaled (i.e. lighten or darken)
 * @param	scale:Number	the scale factor (values -1 to 1) -1 = absolute dark; 1 = absolute light;
 * @return	uint			scaled color
 */
function scaleColor(color:uint, scale:Number):uint
{
	var r:int = (color & 0xFF0000) >> 16;
	var g:int = (color & 0x00FF00) >> 8;
	var b:int = color & 0x0000FF;
	r += (255 * scale)*(r/(r+g+b)); r = (r > 255) ? 255 : r; r = (r < 0) ? 0 : r;
	g += (255 * scale)*(g/(r+g+b)); g = (g > 255) ? 255 : g; g = (g < 0) ? 0 : g;
	b += (255 * scale)*(b/(r+g+b)); b = (b > 255) ? 255 : b; b = (b < 0) ? 0 : b;
	return (r << 16 &amp;amp;amp;amp; 0xff0000) + (g << 8 &amp;amp;amp;amp; 0x00ff00) + (b &amp;amp;amp;amp; 0x0000ff);
}

View the complete source code

2 Comments (+add yours?)

  1. Yop
    Dec 18, 2011 @ 12:42:04

    cool, thx :)

  2. jloa
    Dec 18, 2011 @ 14:08:37

    you are welcome

Leave a Reply

Spam protection by WP Captcha-Free