Working with color HEX & RGB

If you need to convert color values between RGB and HEX you will find the following functions useful. Original post had only ActionScript 1 version and now I have updated it to add ActionScript 3 version as well 🙂

package com.luracast {

    /**
     * Set of funtions for handling color values in different format
     * @author R.Arul Kumaran [[email protected]]
     * For more code keep visiting [www.luracast.com/all/blog]
     */
    public class ColorUtil {
        /**
         * returns a string representing the HEX value
         * for the specified R,G,B values
         * @param r red value (0-255)
         * @param g green value (0-255)
         * @param b blue value (0-255)
         * @return HEX String
         * @example
         * trace(ColorUtil.getHexStr(255,255,255));
         * //traces "ffffff"
         */
        public static function getHexStr(r:uint, g:uint, b:uint):String {
            return twoDigit(r.toString(16)) +
				twoDigit(g.toString(16)) +
				twoDigit(b.toString(16));
        }

        /**
         * returns the HEX value for the specified R,G,B values
         * @param r red value (0-255)
         * @param g green value (0-255)
         * @param b blue value (0-255)
         * @return HEX number
         * @example
         * trace(ColorUtil.getHex(255,255,255));
         * //traces 16777215
         */
        public static function getHex(r:uint, g:uint, b:uint):uint {
            return r << 16 | g << 8 | b;
        }

        /**
         * returns the RGB (as Object) for the
         * specified HEX value
         * @param Hex Hexadecimal number
         * @return Object with properties r, g, and b
         * @example
         * trace(ColorUtil.HexToRGB(0x0c0c0c).r);
         * //traces 12 (the red value)
         */
        public static function Hex2RGB(Hex:uint):Object {
            return { r: HEX >> 16, g: (HEX >> 8) &
				0xff, b: HEX & 0xff };
        }

        /**
         * adds "0" in front if the string is only
         * one digit. Also useful for converting date time strings
         * @param str number given as string
         * @return converted string with a 0 prefix when needed
         * @example
         * var myDate = new Date();
         * var timeStr = ColorUtil.twoDigit(myDate.getHours())+
         * ColorUtil.twoDigit(myDate.getMinutes());
         * trace(timeStr);
         * //traces "01:09"
         */
        public static function twoDigit(str:String):String {
            return str.length == 1 ? "0" + str : str;
        }

    }
}
/*
 ************************************************************
   Developed by R.Arul Kumaran [[email protected]]
   for more code keep visiting [www.luracast.com/all/blog]
 ************************************************************
 */
//Set of funtions for handling color values in different format

/*-----------------------------------------------------
   //getHexStr: returns a string representing the
   //HEX value for the specified R,G,B values
 */
_global.getHexStr = function(r, g, b) {
    return twoDigit(r.toString(16)) +
		twoDigit(g.toString(16)) +
		twoDigit(b.toString(16));
};
/*
   //Usage:-
   trace(getHexStr(255,255,255));
   //traces "ffffff"
 */
/*-----------------------------------------------------
   //getHex: returns the HEX value for the
   //specified R,G,B values
 */
_global.getHex = function(r, g, b) {
    return r << 16 | g << 8 | b;
};
/*
   //Usage:-
   trace(getHex(255,255,255));
   //traces 16777215
 */
/*-----------------------------------------------------
   //HexToRGB: returns the RGB (as Object) for the
   //specified HEX value
 */
_global.HexToRGB = function(HEX) {
    return { r: HEX >> 16, g: (HEX >> 8) &
		0xff, b: HEX & 0xff };
};

/*
   //Usage:-
   trace(HexToRGB(0x0c0c0c).r);
   //traces 12 (the red value)
 */
/*-----------------------------------------------------
   //twoDigit: adds "0" in front if the string is only
   // one digit also useful for converting date time strings
 */
function twoDigit(str) {
    return str.length == 1 ? "0" + str : str;
}
/*
   //Example:-
   var myDate = new Date();
   var timeStr = twoDigit(myDate.getHours())+
		twoDigit(myDate.getMinutes());
   trace(timeStr);
   //traces "01:09"
 */

One comment

  1. Thanks! I was looking for a siemplr way to do this. However if you want the typical representation where leading 0s are not dropped, then I’ve modified the script to do so. E.g. you want it to be represented as 0x00AA00 and not 0xAA00.var c:uint = 0x00AA00;trace (c);var s:String = c.toString(16).toUpperCase();while(s.length < 6){ s = "0" + s;}s = "0x" + s;trace (s);trace (uint(s));

Leave a Reply