## AS3 Color Utility Functions

Utility for Color functions, like RGB to CMYK, RGB to HSV, HSV to RGB, ColorTransformation, int color value to RGB and Color Calculations. Gist Link

package

{

import flash.geom.ColorTransform;

public class ColorUtil

{

/**

* RGBColorTransform Create an instance of the information.

* @ Param rgb RGB integer value that indicates (0x000000 – 0xFFFFFF)

* @ Param amount of fill adaptive value (0.0 – 1.0)

* @ Param alpha transparency (0.0 – 1.0)

* @ Return a new instance ColorTransform

* */

public static function colorTransform (rgb: uint = 0, amount: Number = 1.0, alpha: Number = 1.0): ColorTransform

{

amount = (amount> 1)? 1: (amount 1)? 1: (alpha > 16) & 0xff) * amount;

var g: Number = ((rgb>> 8 ) & 0xff) * amount;

var b: Number = (rgb & 0xff) * amount;

var a: Number = 1-amount;

return new ColorTransform (a, a, a, alpha, r, g, b, 0);

}/**

* Subtraction.

* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) is subtracted from the return numbers.

* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Return value subtracted Blend

**/

public static function subtract (col1: uint, col2: uint): uint

{

var colA: Array = toRGB (col1);

var colB: Array = toRGB (col2);

var r: uint = Math.max (Math.max (colB [0] – (256-colA [0]), colA [0] – (256-colB [0])), 0);

var g: uint = Math.max (Math.max (colB [1] – (256-colA [1]), colA [1] – (256-colB [1])), 0);

var b: uint = Math.max (Math.max (colB [2] – (256-colA [2]), colA [2] – (256-colB [2])), 0);

return r <<16 | g <<8 | b;

}/**

* Additive color.

* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) Returns the value of the additive mixture.

* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Return the additive color

**/

public static function sum (col1: uint, col2: uint): uint

{

var c1: Array = toRGB (col1);

var c2: Array = toRGB (col2);

var r: uint = Math.min (c1 [0] + c2 [0], 255);

var g: uint = Math.min (c1 [1] + c2 [1], 255);

var b: uint = Math.min (c1 [2] + c2 [2], 255);

return r <<16 | g <<8 | b;

}/**

* Subtractive.

* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) Returns the value of the subtractive color.

* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Return the subtractive

**/

public static function sub (col1: uint, col2: uint): uint

{

var c1: Array = toRGB (col1);

var c2: Array = toRGB (col2);

var r: uint = Math.max (c1 [0]-c2 [0], 0);

var g: uint = Math.max (c1 [1]-c2 [1], 0);

var b: uint = Math.max (c1 [2]-c2 [2], 0);

return r <<16 | g <<8 | b;

}/**

* Comparison (dark).

* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) to compare, RGB lower combined returns a numeric value for each number.

* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Return comparison (dark) values

**/

public static function min (col1: uint, col2: uint): uint

{

var c1: Array = toRGB (col1);

var c2: Array = toRGB (col2);

var r: uint = Math.min (c1 [0], c2 [0]);

var g: uint = Math.min (c1 [1], c2 [1]);

var b: uint = Math.min (c1 [2], c2 [2]);

return r <<16 | g <<8 | b;

}/**

* Comparison (light).

* 2 RGB single number that indicates (0x000000 0xFFFFFF up from) to compare, RGB values combined with higher returns to their numbers.

* @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)

* @ Return comparison (light) value

**/

public static function max (col1: uint, col2: uint): uint

{

var c1: Array = toRGB (col1);

var c2: Array = toRGB (col2);

var r: uint = Math.max (c1 [0], c2 [0]);

var g: uint = Math.max (c1 [1], c2 [1]);

var b: uint = Math.max (c1 [2], c2 [2]);

return r <<16 | g <<8 | b;

}/**

* Values calculated from each RGB * RGB color value.

* @ Param r the red (R) indicating the number (0-255)

* @ Param g green (G) indicates the number (0-255)

* @ Param b blue (B) shows the number (0-255)

* @ Return obtained from the RGB color value for each indicating the number

**/

public static function rgb (r: uint, g: uint, b: uint): uint

{

return r <<16 | g <> 16 & 0xFF;

var g: uint = rgb>> 8 & 0xFF;

var b: uint = rgb & 0xFF;

return [r, g, b];

}/**

* RGB from the respective figures, HSV sequences in terms of returns.

* RGB values are as follows.

* R – a number from 0 to 255

* G – a number from 0 to 255

* B – a number from 0 to 255

*

* HSV values are as follows.

* H – a number between 360-0

* S – number between 0 and 1.0

* V – number between 0 and 1.0

*

* Can not compute, including alpha.

* @ Param r the red (R) indicating the number (0x00 to 0xFF to)

* @ Param g green (G) indicates the number (0x00 to 0xFF to)

* @ Param b blue (B) shows the number (0x00 to 0xFF to)

* @ Return HSV values into an array of [H, S, V]

**/

public static function RGBtoHSV (r: Number, g: Number, b: Number): Array

{

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

var h: Number = 0, s: Number = 0, v: Number = 0;

var x: Number, y: Number;

if (r> = g) x = r; else x = g; if (b> x) x = b;

if (r <= g) y = r; else y = g; if (b <y) y = b;

v = x;

var c: Number = xy;

if (x == 0) s = 0; else s = c / x;

if (s! = 0) {

if (r == x) {

h = (gb) / c;

} else {

if (g == x) {

h = 2 + (br) / c;

} Else {

if (b == x) {

h = 4 + (rg) / c;

}

}

}

h = h * 60;

if (h <0) h = h +360;

}

return [h, s, v];

}

/**

* RGB from the respective figures, HSV sequences in terms of returns.

* RGB values are as follows.

* R – a number from 0 to 255

* G – a number from 0 to 255

* B – a number from 0 to 255

*

* CMYK values are as follows.

* C – a number between 0 to 255 representing cyan

* M – number between 0 to 255 representing magenta

* Y – number between 0 to 255 representing yellow

* K – number between 0 to 255 representing black

*

* Can not compute, including alpha.

* @ Param r the red (R) indicating the number (0x00 to 0xFF to)

* @ Param g green (G) indicates the number (0x00 to 0xFF to)

* @ Param b blue (B) shows the number (0x00 to 0xFF to)

* @ Return CMYK values into an array of [H, S, V]

**/

public static function RGBtoCMYK( r:Number, g:Number, b:Number ):Array

{

var c:Number=0, m:Number=0, y:Number=0, k:Number=0, z:Number=0;

c = 255 – r;

m = 255 – g;

y = 255 – b;

k = 255;if (c < k)

k=c;

if (m < k)

k=m;

if (y < k)

k=y;

if (k == 255)

{

c=0;

m=0;

y=0;

}else

{

c=Math.round(255*(c-k)/(255-k));

m=Math.round (255*(m-k)/(255-k));

y=Math.round (255*(y-k)/(255-k));

}

return [ c, m, y, k ];

}/**

* HSV from each of the RGB values to determine a return as an array.

* RGB values are as follows.

* R – a number from 0 to 255

* G – a number from 0 to 255

* B – a number from 0 to 255

*

* HSV values are as follows.

* H – a number between 360-0

* S – number between 0 and 1.0

* V – number between 0 and 1.0

*

* H is replaced with equivalent numbers in the range of the 360-0 that is out of range.

* Can not compute, including alpha.

*

* @ Param h hue (Hue) number that indicates (to 360-0)

* @ Param s the saturation (Saturation) shows the number (0.0 to 1.0)

* @ Param v lightness (Value) indicates the number (0.0 to 1.0)

* @ Return RGB values into an array of [R, G, B]

**/

public static function HSVtoRGB (h: Number, s: Number, v: Number): Array

{

var r: Number = 0, g: Number = 0, b: Number = 0;

var i: Number, x: Number, y: Number, z: Number;

if (s 1) s = 1; if (v 1) v = 1;

h = h% 360; if (h > 0;

x = v * (1 – s); y = v * (1 – s * (h – i)); z = v * (1 – s * (1 – h + i));

switch (i) {

case 0: r = v; g = z; b = x; break;

case 1: r = y; g = v; b = x; break;

case 2: r = x; g = v; b = z; break;

case 3: r = x; g = y; b = v; break;

case 4: r = z; g = x; b = v; break;

case 5: r = v; g = x; b = y; break;

}

return [r * 255>> 0, g * 255>> 0, b * 255>> 0];

}}

}

Hi,

Do you know how to save CMYK Jpeg in AS 3 ? They always are saved in RGB for me …

thanks

no idea .. on this..

OMG! DON NOT EVEN USE ANY OF MATH. FUNCTIONS! THEY ARE SLOW AS HELL!

Cool bananas. :-)

Where to the variables s1 and v1 come from? They are used in HSVtoRGB() before being defined or initialized in the code above.

Thanks for pointing out, have updated the code.. got corrupted by wordpress.. :(

Coolness! Thanks! :-)

I used your HSVtoRGB code (or its algorithm, at least) to map musical intervals to RGB colors. I’m holding S & V constant, and mapping the width of the interval (in cents) to Hue. This makes colors cycle at the octave, just as musical intervals do. I’ll post the code to my blog (www.igetitmusic.com/blog) later this week. Hopefully, someone will find my code to be as useful as I found yours to be.

Apparently, Flex 4 will contain an HSVtoRGB conversion routine, which demonstrates that you’re targeting coding problems that have broad appeal/demand. That ought to be encouraging. :-)

Thanks again! :-)

The HSVtoRGB function looks still corrupted. There’s a “if (h > 0;” statement, and the “i” variable is never declared or initialized…

Dear Helmut,

Thanks, for correcting the error.. have updated the post

Where is the toRGB function?

Actually, i use ImageMagick lib in PHP to do this.

It’s better for me…

If you do stuff in PHP why are you trolling on an AS3 blog?

Go away troll.

Thanks for noting it updated the entry

public static function toRGB( rgb:uint ):Array

{

var r:uint = rgb >> 16 & 0xFF;

var g:uint = rgb >> 8 & 0xFF;

var b:uint = rgb & 0xFF;

return [r,g,b];

}

Hi Deva,

Can you please send the RGBtoCYMK method as it got corrupted by wordpress..

would like to know what is supposed to be here in between if(b??? 0)–> else

k = g;

if (b 0)

{

Hey man. Great code, thank you generously for sharing. Is there any chance you can upload the class and link to the actual AS file, or possibly throw it in a PasteBin? There is a bit of corruption from WordPress, and a couple of the functions aren’t translating correctly. Even if you could email me the class that would be a huge help.

Appreciate all the work that went into creating this. :)

amount = ( amount > 1 ) ? 1 : ( amount 1 ) ? 1 : ( alpha > 16 ) & 0xff ) * amount;

there is no operator in between “amount 1″

hi can u please post the RGBToCyMK and viceversa code here …

Thanks in advance.

Have made a public gist to share the same code to avoid problems with WP

http://gist.github.com/638271

Hi,

first of all thanks for the great code.

I got the code from the github but the are some errors.

some of the “-” are “–” and so on.

I was looking for an CMYK to RGB function but your one isn’t working.

I would like to fix it but I don’t know how to calculate RGB from CMYK

Your code:

public static function CMYKtoRGB( c:Number, m:Number, y:Number, k:Number ):Array

{

c = 255 – c;

m = 255 – m;

y = 255 – y;

k = 255 – k;

return [(255 - c) * (255 - k) / 255,

(255 - m) * (255 - k) / 255, (255 - y) * (255 - k) / 255)];

}

}

1. in the end of the return line there is a closing bracket without any opening bracked before.

2. when K = 0 the your rgb is 0,0,0 because of:

k = 255 – k;

and … * (255 – k) => … * 0

This must be wrong because the color becomes bright without k

Do you have any fix for it?

Here is a working funktion.

I changed the values in a range from 0 to 100 because components of CMYK are given in percent.

/**

* RGB from each of the CMYK values to determine a return as an array.

* CMYK values are as follows.

* C – a number between 0 to 100 representing cyan

* M – number between 0 to 100 representing magenta

* Y – number between 0 to 100 representing yellow

* K – number between 0 to 100 representing black

*

**/

public static function CMYKtoRGB( c:Number, m:Number, y:Number, k:Number ):Array

{

m = m/100;

y = y/100;

k = k/100;

var rgb:Array = new Array();

rgb[0] = Math.floor((1 – Math.min(1, c * (1 – k) + k))*255 + 0.5) ;

rgb[1] = Math.floor((1 – Math.min(1, m * (1 – k) + k))*255 + 0.5) ;

rgb[2] = Math.floor((1 – Math.min(1, y * (1 – k) + k))*255 + 0.5) ;

return rgb;

}

I continually get errors for this line…

in the RGBtoHSV

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

same here still getting errors around that line. Took the class off of gist in raw format too