/* Colors c, c1, c2 are of the form 0x00RRGGBB
 */

typedef unsigned int uint;

uint darker(uint c)
{
	return (c >> 1) & ~0x8080;
}

uint lighter(uint c)
{
	uint lighter = c << 1;
	uint over = lighter & 0x01010100;   /* detect overflow */
	uint mask = over - (over >> 8);     /* mask for saturation */
	return (lighter & 0xfefefe) | mask;
}

uint add(uint c1, uint c2)
{
	uint colG = (c1 & 0xff00) + (c2 & 0xff00);
	uint over = colG & 0x10000;     /* detect overflow */
	uint mask = over - (over >> 8); /* mask for saturation */
	uint colRB = (c1 & 0xff00ff) + (c2 & 0xff00ff);
	over = colRB & 0x01000100;
	mask |= over - (over >> 8);
	return (colG & 0xff00) | (colRB & 0xff00ff) | mask;
}
