AS3 - RGB class for RGB to Hex


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

Can also be used with [HSL](http://snipplr.com/view/34817/as3--hsl-to-rgb-class/) class I posted for conversion from HSL to RGB.


Copy this code and paste it in your HTML
  1. /**
  2.  * @author Rivaledsouls
  3.  * @version 1.0
  4.  * Feel free to use this code as you wish as long as you cite
  5.  * me, Rivaledsouls, for use of the code in any projects where
  6.  * you share the source.
  7.  * (c) 2010
  8.  */
  9.  
  10. package utils.color {
  11.  
  12. public class RGB {
  13.  
  14. private var color:uint;
  15. private var red:int;
  16. private var green:int;
  17. private var blue:int;
  18. /**
  19. * Contructs an RGB object for containing Red Green and Blue values, and
  20. * converting them to a Hex color
  21. * @param r the Red value (0-255)
  22. * @param g the Green value (0-255)
  23. * @param b the Blue value (0-255)
  24. */
  25. public function RGB(r:Number=0, g:Number=0, b:Number=0) {
  26. Red = r;
  27. Green = g;
  28. Blue = b;
  29. }
  30.  
  31. /*
  32. * Getters for each channel, converts from Hex
  33. */
  34. public function get Red():int {
  35. return color >> 16;
  36. }
  37. public function get Green():int {
  38. return (color >> 8) & 0xFF;
  39. }
  40. public function get Blue():int {
  41. return color & 0x00FF;
  42. }
  43. /*
  44. *Setters for each channel, converts to Hex
  45. */
  46. public function set Red(Value:int):void {
  47. red = (Value>255)? 255 : ((Value<0)?0:Value);
  48. color=getHex(red,green,blue);
  49. }
  50. public function set Green(Value:int):void {
  51. green = (Value>255)? 255 : ((Value<0)?0:Value);
  52. color=getHex(red,green,blue);
  53. }
  54. public function set Blue(Value:int):void {
  55. blue = (Value>255)? 255 : ((Value<0)?0:Value);
  56. color=getHex(red,green,blue);
  57. }
  58. /*
  59. * Getter and setter for Hex value
  60. */
  61. public function get Hex():uint {
  62. return color;
  63. }
  64. public function set Hex(Value:uint):void {
  65. color=Value;
  66. }
  67.  
  68. /**
  69. * A static method for directly converting from Red,Green,and Blue values
  70. * to Hex.
  71. * @param r the Red value (0-255)
  72. * @param g the Green value (0-255)
  73. * @param b the Blue value (0-255)
  74. * @return the Hex value of the specified RGB color
  75. */
  76. public static function getHex(r:int, g:int, b:int):uint{
  77. return (r<<16)|(g << 8)|b;
  78. }
  79. }
  80.  
  81. }

URL: http://snipplr.com/view/34817/as3--hsl-to-rgb-class/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.