/ Published in: ActionScript 3
URL: http://luracast.com/blog/working-with-color-hex-rgb/
Expand |
Embed | Plain Text
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; } function fixColorCode($color:String) :String { var submittedColor:String = $color; var validColor:String; var pattern:RegExp = /#/; submittedColor = $color.replace(pattern,""); pattern = /0x/; if (submittedColor.substring(0,2) != "0x") { validColor = "0x"+submittedColor; } else { validColor = submittedColor; } return validColor; } } }
You need to login to post a comment.
