AS3 ColorUtil Class (inc. RGBtoCMYK, CMYKtoRGB, subtractive colour & addative colour)


/ Published in: ActionScript 3
Save to your folder(s)

Full credit for this class goes to Deva Raj (@nsdevaraj). Many thanks.


Copy this code and paste it in your HTML
  1. package
  2. {
  3. import flash.geom.ColorTransform;
  4. public class ColorUtil
  5. {
  6. /**
  7. * RGBColorTransform Create an instance of the information.
  8. * @ Param rgb RGB integer value that indicates (0x000000 - 0xFFFFFF)
  9. * @ Param amount of fill adaptive value (0.0 - 1.0)
  10. * @ Param alpha transparency (0.0 - 1.0)
  11. * @ Return a new instance ColorTransform
  12. * */
  13. public static function colorTransform (rgb: uint = 0, amount: Number = 1.0,
  14. alpha: Number = 1.0): ColorTransform
  15. {
  16. amount = (amount> 1)? 1: (amount <0)? 0: amount;
  17. alpha = (alpha> 1)? 1: (alpha <0)? 0: alpha;
  18. var r: Number = ((rgb>> 16) & 0xff) * amount;
  19. var g: Number = ((rgb>> 8) & 0xff) * amount;
  20. var b: Number = (rgb & 0xff) * amount;
  21. var a: Number = 1-amount;
  22. return new ColorTransform (a, a, a, alpha, r, g, b, 0);
  23. }
  24.  
  25. /**
  26. * Subtraction.
  27. * 2 RGB single number that indicates (0x000000 0xFFFFFF up from) is subtracted
  28.   * from the return numbers.
  29. * @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
  30. * @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
  31. * @ Return value subtracted Blend
  32. **/
  33. public static function subtract (col1: uint, col2: uint): uint
  34. {
  35. var colA: Array = toRGB (col1);
  36. var colB: Array = toRGB (col2);
  37. var r: uint = Math.max (Math.max (colB [0] - (256-colA [0]),
  38. colA [0] - (256-colB [0])), 0);
  39. var g: uint = Math.max (Math.max (colB [1] - (256-colA [1]),
  40. colA [1] - (256-colB [1])), 0);
  41. var b: uint = Math.max (Math.max (colB [2] - (256-colA [2]),
  42. colA [2] - (256-colB [2])), 0);
  43. return r <<16 | g <<8 | b;
  44. }
  45.  
  46. /**
  47. * Additive color.
  48. * 2 RGB single number that indicates (0x000000 0xFFFFFF up from) Returns the value
  49.   * of the additive mixture.
  50. * @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
  51. * @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
  52. * @ Return the additive color
  53. **/
  54. public static function sum (col1: uint, col2: uint): uint
  55. {
  56. var c1: Array = toRGB (col1);
  57. var c2: Array = toRGB (col2);
  58. var r: uint = Math.min (c1 [0] + c2 [0], 255);
  59. var g: uint = Math.min (c1 [1] + c2 [1], 255);
  60. var b: uint = Math.min (c1 [2] + c2 [2], 255);
  61. return r <<16 | g <<8 | b;
  62. }
  63.  
  64. /**
  65. * Subtractive.
  66. * 2 RGB single number that indicates (0x000000 0xFFFFFF up from) Returns the value
  67.   * of the subtractive color.
  68. * @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
  69. * @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
  70. * @ Return the subtractive
  71. **/
  72. public static function sub (col1: uint, col2: uint): uint
  73. {
  74. var c1: Array = toRGB (col1);
  75. var c2: Array = toRGB (col2);
  76. var r: uint = Math.max (c1 [0]-c2 [0], 0);
  77. var g: uint = Math.max (c1 [1]-c2 [1], 0);
  78. var b: uint = Math.max (c1 [2]-c2 [2], 0);
  79. return r <<16 | g <<8 | b;
  80. }
  81.  
  82. /**
  83. * Comparison (dark).
  84. * 2 RGB single number that indicates (0x000000 0xFFFFFF up from) to compare,
  85.   * RGB lower combined returns a numeric value for each number.
  86. * @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
  87. * @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
  88. * @ Return comparison (dark) values
  89. **/
  90. public static function min (col1: uint, col2: uint): uint
  91. {
  92. var c1: Array = toRGB (col1);
  93. var c2: Array = toRGB (col2);
  94. var r: uint = Math.min (c1 [0], c2 [0]);
  95. var g: uint = Math.min (c1 [1], c2 [1]);
  96. var b: uint = Math.min (c1 [2], c2 [2]);
  97. return r <<16 | g <<8 | b;
  98. }
  99.  
  100. /**
  101. * Comparison (light).
  102. * 2 RGB single number that indicates (0x000000 0xFFFFFF up from) to compare,
  103.   * RGB values combined with higher returns to their numbers.
  104. * @ Param col1 RGB numbers show (0x000000 0xFFFFFF up from)
  105. * @ Param col2 RGB numbers show (0x000000 0xFFFFFF up from)
  106. * @ Return comparison (light) value
  107. **/
  108. public static function max (col1: uint, col2: uint): uint
  109. {
  110. var c1: Array = toRGB (col1);
  111. var c2: Array = toRGB (col2);
  112. var r: uint = Math.max (c1 [0], c2 [0]);
  113. var g: uint = Math.max (c1 [1], c2 [1]);
  114. var b: uint = Math.max (c1 [2], c2 [2]);
  115. return r <<16 | g <<8 | b;
  116. }
  117.  
  118. /**
  119. * Values calculated from each RGB * RGB color value.
  120. * @ Param r the red (R) indicating the number (0-255)
  121. * @ Param g green (G) indicates the number (0-255)
  122. * @ Param b blue (B) shows the number (0-255)
  123. * @ Return obtained from the RGB color value for each indicating the number
  124. **/
  125. public static function rgb (r: uint, g: uint, b: uint): uint
  126. {
  127. return r <<16 | g <<8 | b;
  128. }
  129.  
  130. /**
  131. * HSV calculated from the numbers of each RGB color value.
  132. * @ Param h hue (Hue) number that indicates (to 360-0)
  133. * @ Param s the saturation (Saturation) shows the number (0.0 to 1.0)
  134. * @ Param v lightness (Value) indicates the number (0.0 to 1.0)
  135. * @ Return obtained from the RGB color value for each indicating the number
  136. **/
  137. public static function hsv (h: int, s: Number, v: Number): uint
  138. {
  139. return rgb.apply (null, HSVtoRGB (h, s, v));
  140. }
  141.  
  142. /**
  143. * RGB figures show (0x000000 0xFFFFFF up from) the
  144. * R, G, B returns an array divided into a number from 0 to 255, respectively.
  145. *
  146. * @ Param rgb RGB numbers show (0x000000 0xFFFFFF up from)
  147. * @ Return array indicates the value of each color [R, G, B]
  148. **/
  149. public static function toRGB (rgb: uint): Array
  150. {
  151. var r: uint = rgb>> 16 & 0xFF;
  152. var g: uint = rgb>> 8 & 0xFF;
  153. var b: uint = rgb & 0xFF;
  154. return [r, g, b];
  155. }
  156.  
  157. /**
  158. * RGB from the respective figures, HSV sequences in terms of returns.
  159. * RGB values are as follows.
  160. * R - a number from 0 to 255
  161. * G - a number from 0 to 255
  162. * B - a number from 0 to 255
  163. *
  164. * HSV values are as follows.
  165. * H - a number between 360-0
  166. * S - number between 0 and 1.0
  167. * V - number between 0 and 1.0
  168. *
  169. * Can not compute, including alpha.
  170. * @ Param r the red (R) indicating the number (0x00 to 0xFF to)
  171. * @ Param g green (G) indicates the number (0x00 to 0xFF to)
  172. * @ Param b blue (B) shows the number (0x00 to 0xFF to)
  173. * @ Return HSV values into an array of [H, S, V]
  174. **/
  175. public static function RGBtoHSV (r: Number, g: Number, b: Number): Array
  176. {
  177. r / = 255; g / = 255; b / = 255;
  178. var h: Number = 0, s: Number = 0, v: Number = 0;
  179. var x: Number, y: Number;
  180. if (r> = g) x = r; else x = g; if (b> x) x = b;
  181. if (r <= g) y = r; else y = g; if (b <y) y = b;
  182. v = x;
  183. var c: Number = xy;
  184. if (x == 0) s = 0; else s = c / x;
  185. if (s! = 0) {
  186. if (r == x) {
  187. h = (gb) / c;
  188. } else {
  189. if (g == x) {
  190. h = 2 + (br) / c;
  191. } Else {
  192. if (b == x) {
  193. h = 4 + (rg) / c;
  194. }
  195. }
  196. }
  197. h = h * 60;
  198. if (h <0) h = h +360;
  199. }
  200. return [h, s, v];
  201. }
  202.  
  203. /**
  204. * RGB from the respective figures, HSV sequences in terms of returns.
  205. * RGB values are as follows.
  206. * R - a number from 0 to 255
  207. * G - a number from 0 to 255
  208. * B - a number from 0 to 255
  209. *
  210. * CMYK values are as follows.
  211. * C - a number between 0 to 255 representing cyan
  212. * M - number between 0 to 255 representing magenta
  213. * Y - number between 0 to 255 representing yellow
  214. * K - number between 0 to 255 representing black
  215. *
  216. * Can not compute, including alpha.
  217. * @ Param r the red (R) indicating the number (0x00 to 0xFF to)
  218. * @ Param g green (G) indicates the number (0x00 to 0xFF to)
  219. * @ Param b blue (B) shows the number (0x00 to 0xFF to)
  220. * @ Return CMYK values into an array of [H, S, V]
  221. **/
  222. public static function RGBtoCMYK( r:Number, g:Number, b:Number ):Array
  223. {
  224. var c:Number=0, m:Number=0, y:Number=0, k:Number=0, z:Number=0;
  225. c = 255 – r;
  226. m = 255 – g;
  227. y = 255 – b;
  228. k = 255;
  229.  
  230. if (c < k)
  231. k=c;
  232. if (m < k)
  233. k=m;
  234. if (y < k)
  235. k=y;
  236. if (k == 255)
  237. {
  238. c=0;
  239. m=0;
  240. y=0;
  241. }else
  242. {
  243. c=Math.round(255*(c-k)/(255-k));
  244. m=Math.round (255*(m-k)/(255-k));
  245. y=Math.round (255*(y-k)/(255-k));
  246. }
  247. return [ c, m, y, k ];
  248. }
  249. /**
  250. * HSV from each of the RGB values to determine a return as an array.
  251. * RGB values are as follows.
  252. * R - a number from 0 to 255
  253. * G - a number from 0 to 255
  254. * B - a number from 0 to 255
  255. *
  256. * HSV values are as follows.
  257. * H - a number between 360-0
  258. * S - number between 0 and 1.0
  259. * V - number between 0 and 1.0
  260. *
  261. * H is replaced with equivalent numbers in the range of the 360-0 that is out of range.
  262. * Can not compute, including alpha.
  263. *
  264. * @ Param h hue (Hue) number that indicates (to 360-0)
  265. * @ Param s the saturation (Saturation) shows the number (0.0 to 1.0)
  266. * @ Param v lightness (Value) indicates the number (0.0 to 1.0)
  267. * @ Return RGB values into an array of [R, G, B]
  268. **/
  269. public static function HSVtoRGB (h: Number, s: Number, v: Number): Array
  270. {
  271. var r: Number = 0, g: Number = 0, b: Number = 0;
  272. var i: Number, x: Number, y: Number, z: Number;
  273. if (s <0) s = 0; if (s> 1) s = 1; if (v <0) v = 0; if (v> 1) v = 1;
  274. h = h% 360; if (h <0) h + = 360; h / = 60;
  275. i = h>> 0;
  276. x = v * (1 - s); y = v * (1 - s * (h - i)); z = v * (1 - s * (1 - h + i));
  277. switch (i) {
  278. case 0: r = v; g = z; b = x; break;
  279. case 1: r = y; g = v; b = x; break;
  280. case 2: r = x; g = v; b = z; break;
  281. case 3: r = x; g = y; b = v; break;
  282. case 4: r = z; g = x; b = v; break;
  283. case 5: r = v; g = x; b = y; break;
  284. }
  285. return [r * 255>> 0, g * 255>> 0, b * 255>> 0];
  286. }
  287. /**
  288. * RGB from each of the CMYK values to determine a return as an array.
  289. * CMYK values are as follows.
  290. * C - a number between 0 to 255 representing cyan
  291. * M - number between 0 to 255 representing magenta
  292. * Y - number between 0 to 255 representing yellow
  293. * K - number between 0 to 255 representing black
  294. *
  295. **/
  296. public static function CMYKtoRGB( c:Number, m:Number, y:Number, k:Number ):Array
  297. {
  298. c = 255 - c;
  299. m = 255 - m;
  300. y = 255 - y;
  301. k = 255 - k;
  302. return [(255 - c) * (255 - k) / 255,
  303. (255 - m) * (255 - k) / 255, (255 - y) * (255 - k) / 255)];
  304. }
  305. }
  306. }

URL: http://nsdevaraj.wordpress.com/2009/04/17/as3-color-utility-functions/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.