Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Hex to HSL in AS3.0 for photoshop action

with 2 comments

function HexToHSL( hex:uint ):Array
{
var r:Number = ( hex & 0xff0000 ) >> 16;
var g:Number = ( hex & 0x00ff00 ) >> 8;
var b:Number = ( hex & 0x0000ff );

r = r / 255;
g = g / 255;
b = b / 255;

var max:Number = Math.max( r, g, b )
var min:Number = Math.min( r, g, b );

var h:Number, s:Number;
var l:Number = (max + min) / 2;

if ( max == min ) {
h = s = 0;// achromatic
} else {
var d:Number = max – min;
s = l > 0.5 ? d / ( 2 – max – min ) : d / ( max + min );
switch ( max ) {
case r :
h = (g – b) / d + (g < b ? 6 : 0);
break;
case g :
h = (b – r) / d + 2;
break;
case b :
h = (r – g) / d + 4;
break;
}
h/=6;
}

h = Math.round( h*360 );
s = Math.round( s*100 );
l = Math.round( l*100 );

l = Math.round( -100 + ( 200*l/100 ) );//this is for PhotoShop action;

return [h, s, l];
}

var hsl:Object = HexToHSL( 0xFFFFFF );
trace(hsl);//0,0,100

Advertisements

Written by Deepanjan Das

July 14, 2010 at 4:04 PM

2 Responses

Subscribe to comments with RSS.

  1. Very nice article, thanks! I’ve subscribed to your RSS feed. Please keep up posting.

    The Blogs Rocket

    July 18, 2010 at 9:54 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: