Converting a Web Color to an RGB float array for After Effects

I’m writing a script that needs hexadecimal color input, sometimes referred to as a web color, converted to a value usable for a solid in After Effects. After Effects wants an array of RGB values in the range 0 to 1: [R, G, B]. Luckily Javascript supports bit-wise operators so the conversion is pretty straight forward. I used this function.

var hex = 'ffaadd';
var colors = hexToRGBArray(hex);

alert("red =" + colors[0] + " green=" + colors[1] + " blue=" + colors[2]);

function hexToRGBArray(hex){
	var rgb = parseInt(hex, 16);
	var red   = (rgb >>16) & 0xFF;
	var green = (rgb >>8) & 0xFF;
	var blue  = rgb & 0xFF;

	var colorArray = [red/255.0, green/255.0, blue/255.0];

	return colorArray;

}


Source for this script
Downloadable version of this script

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">