IMAGE MANIPULATION…

July 30, 2009 at 3:08 AM (Actionscript)

Continuing my previous post, now, lets see some basic codes (there could be some better advanced codes) that we can use in image manipulation(most of the codes posted here are found in web either directly as AS or translated to AS from other proggramming language)

//!- Adjust the luminocity of a color value

//!- this function manipulates the luminosity of a color value
//- by manipulating h, s values the hue, saturaton of color values can be changed

function doLight(col, offset) {
	var h, s, l, hsl, rgb;
	hsl = rgbToHSL(col);
	h = hsl.h;
	s = hsl.s;
	l = hsl.l;
	h = Math.round(h*100000)/100000;
	s = Math.round(s*100000)/100000;
	l = Math.round(l*100000)/100000;
	l = l+offset;
	l = l>1 ? 1 : l;
	l = l<0 ? 0 : l;
	h = h>1 ? 1 : h;
	h = h<0 ? 0 : h;
	s = s>1 ? 1 : s;
	s = s<0 ? 0 : s;
	rgb = hslToRGB(h, s, l);
	var hex = getHEX(rgb.r, rgb.g, rgb.b);
	return hex;
}

//!-Adjust the brightness of color value

function brightness(col,offset) {
	//!- brightness of a pixel is changed by adding a offset to original color value
	var rgb = getRGB(col);
	var r = rgb.r;
	var g = rgb.g;
	var b = rgb.b;
	r+=offset;
	g+=offset;
	b+=offset;
	r=r>255?255:r<0?0:r;
	g=g>255?255:g<0?0:g;
	b=b>255?255:b<0?0:b;
	var hex = getHEX(r, g, b);
	return hex;
}

//!- Adjust contrast of a color value

function contrast(col,_cVal) {
	//!-algorithm courtesy Mitch Dusina,
	//- http://www.developerfusion.com/article/5441/image-manipulation-brightness-and-contrast/3/
	var rgb = getRGB(col);
	var r = rgb.r;
	var g = rgb.g;
	var b = rgb.b;
	cVal=Math.pow((100 + _cVal/2) / 100, 2);
	r/=255;
	g/=255;
	b/=255;
	r-=0.5;
	g-=0.5;
	b-=0.5;

	r*=cVal;
	g*=cVal;
	b*=cVal;
	r+=0.5;
	g+=0.5;
	b+=0.5;
	r*=255;
	g*=255;
	b*=255;
	r=r>255?255:r<0?0:r;
	g=g>255?255:g<0?0:g;
	b=b>255?255:b<0?0:b;

	var hex = getHEX(r, g, b);
	return hex;
}

//!-Invert a color value

function invert(col) {
	var rgb = getRGB(col);
	var r, g, b;
	r = 255-rgb.r;
	g = 255-rgb.g;
	b = 255-rgb.b;
	var hex = getHEX(r, g, b);
	return hex;
}

//!- Apply mono tone to a color value

function monoChrome(pixCol, chrome) {
	var rgb = getRGB(pixCol);//color value to be changed
	var rgb2 = getRGB(chrome);//monotone to be applied

	var r = Math.min(rgb.r ,rgb2.r);
	var g =  Math.min(rgb.g , rgb2.g);
	var b =  Math.min(rgb.b , rgb2.b);
	var hex = getHEX(r, g, b);

	//!- simply you can use
	//hex=pixCol & chrome
	//trace(hex.toString(16))
	return hex;
}

Permalink Leave a Comment

Next page »

%d bloggers like this: