Posted By

darkbaron1912 on 10/25/10


Tagged

color


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

sexyflash


color


 / Published in: ActionScript 3
 

URL: http://luracast.com/blog/working-with-color-hex-rgb/

  1. package com.luracast {
  2.  
  3. /**
  4.   * Set of funtions for handling color values in different format
  5.   * @author R.Arul Kumaran [[email protected]]
  6.   * For more code keep visiting [www.luracast.com/all/blog]
  7.   */
  8. public class ColorUtil {
  9. /**
  10.   * returns a string representing the HEX value
  11.   * for the specified R,G,B values
  12.   * @param r red value (0-255)
  13.   * @param g green value (0-255)
  14.   * @param b blue value (0-255)
  15.   * @return HEX String
  16.   * @example
  17.   * trace(ColorUtil.getHexStr(255,255,255));
  18.   * //traces "ffffff"
  19.   */
  20. public static function getHexStr(r:uint, g:uint, b:uint):String {
  21. return twoDigit(r.toString(16)) +
  22. twoDigit(g.toString(16)) +
  23. twoDigit(b.toString(16));
  24. }
  25.  
  26. /**
  27.   * returns the HEX value for the specified R,G,B values
  28.   * @param r red value (0-255)
  29.   * @param g green value (0-255)
  30.   * @param b blue value (0-255)
  31.   * @return HEX number
  32.   * @example
  33.   * trace(ColorUtil.getHex(255,255,255));
  34.   * //traces 16777215
  35.   */
  36. public static function getHex(r:uint, g:uint, b:uint):uint {
  37. return r << 16 | g << 8 | b;
  38. }
  39.  
  40. /**
  41.   * returns the RGB (as Object) for the
  42.   * specified HEX value
  43.   * @param Hex Hexadecimal number
  44.   * @return Object with properties r, g, and b
  45.   * @example
  46.   * trace(ColorUtil.HexToRGB(0x0c0c0c).r);
  47.   * //traces 12 (the red value)
  48.   */
  49. public static function Hex2RGB(Hex:uint):Object {
  50. return { r: HEX >> 16, g: (HEX >> 8) &
  51. 0xff, b: HEX & 0xff };
  52. }
  53.  
  54. /**
  55.   * adds "0" in front if the string is only
  56.   * one digit. Also useful for converting date time strings
  57.   * @param str number given as string
  58.   * @return converted string with a 0 prefix when needed
  59.   * @example
  60.   * var myDate = new Date();
  61.   * var timeStr = ColorUtil.twoDigit(myDate.getHours())+
  62.   * ColorUtil.twoDigit(myDate.getMinutes());
  63.   * trace(timeStr);
  64.   * //traces "01:09"
  65.   */
  66. public static function twoDigit(str:String):String {
  67. return str.length == 1 ? "0" + str : str;
  68. }
  69. function fixColorCode($color:String) :String
  70. {
  71. var submittedColor:String = $color;
  72. var validColor:String;
  73. var pattern:RegExp = /#/;
  74.  
  75. submittedColor = $color.replace(pattern,"");
  76.  
  77. pattern = /0x/;
  78. if (submittedColor.substring(0,2) != "0x") {
  79. validColor = "0x"+submittedColor;
  80. } else {
  81. validColor = submittedColor;
  82. }
  83.  
  84. return validColor;
  85.  
  86. }
  87.  
  88. }
  89. }

Report this snippet  

You need to login to post a comment.