package com.sc.utils { /** * @author csmith - chris@somethingcolorful.com */ public class Color { /** * @param hex * @return *

Returns a Hex to Decimal as a String

*/ public static function hex2dec( hex:String ) : String { var bytes:Array = []; while( hex.length > 2 ) { var byte:String = hex.substr( -2 ); hex = hex.substr(0, hex.length-2 ); bytes.splice( 0, 0, int("0x"+byte) ); } return bytes.join(" "); } /** * @param d * @return *

Returns a String value of a Decimal to Hex

*/ public static function d2h( d:int ) : String { var c:Array = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' ]; if( d > 255 ) d = 255; var l:int = d / 16; var r:int = d % 16; return c[l]+c[r]; } /** * @param dec * @return *

Returns a Hex from a Decimal

*/ public static function dec2hex( dec:String ) : String { var hex:String = "0x"; var bytes:Array = dec.split(" "); for( var i:int = 0; i < bytes.length; i++ ) hex += d2h( int(bytes[i]) ); return hex; } /** * @param hex - hexidecimal string "#000000" or "0x000000" * @param total - number of colors returned * @param darkest - decimal from 0-1 * @param lightest - decimal from 1-2 * @return - array of hex color strings *

Get a random Color that is the same "TINT" as the color you provided.

*/ public static function getRandomPalateOf( hex:String, total:int=10, darkest:Number=0, lightest:Number=2 ) : Array { var palate:Array = new Array(); for(var j:int=0;jReturns a hex color on the opposite side of the colorwheel at the same radius

*/ public static function getComplimentaryColor( hex:String ) : String { var a:Array = hex2dec(hex).split(" "); var r:int=a[0], g:int=a[1], b:int=a[2]; // convert var hsl:Array = rgb2hsl(r,g,b); var h:Number = hsl[0], s:Number = hsl[1], l:Number = hsl[2]; // opposite hue h = h + .5; if(h > 1) h-=1; // convert var rgb:Array = hsl2rgb(h,s,l); // convert to dec return dec2hex(rgb[0]+" "+rgb[1]+" "+rgb[2]); } /** * @param v1 * @param v2 * @param vh * @return * Convert a Hue to a Red, Green or Blue */ public static function hue2rgb(v1:Number,v2:Number,vh:Number) : Number { if(vh < 0) vh+=1; if(vh > 1) vh-=1; if((6 * vh) < 1) return (v1 + (v2 - v1) * 6 * vh); if((2 * vh) < 1) return (v2); if((3 * vh) < 2) return (v1 + (v2-v1) * (((2 / 3) - vh) * 6)); return v1; } /** * @param r * @param g * @param b * @return * Convert an RGB value into an HSL (Hue Saturation Lightness) value */ public static function rgb2hsl(r:int,g:int,b:int) : Array { var r2:int, g2:int, b2:int; var min:Number = Math.min(r,g,b); // min of rgb var max:Number = Math.max(r,g,b); // max of rgb var max2:Number = max-min; // delta of rgb var l:Number = (max + min) / 2, h:Number = 0, s:Number = 0; // lightness, hue, saturation // if(max2==0){ h=0, s=0; }else{ // chromatic data if(l<.5) s = max2 / (max+min); else s = max2 / (2-max-min); r2 = (((max - r) / 6) + (max2 / 2)) / max2; g2 = (((max - g) / 6) + (max2 / 2)) / max2; b2 = (((max - b) / 6) + (max2 / 2)) / max2; if(r == max) h = b2 - g2; else if (g == max) h = (1/3) + r2-b2; else if (b == max) h = (2/3) + g2-r2; if(h < 0) h+=1; if(h > 1) h-=1; } return [h,s,l]; } /** * @param h * @param s * @param l * @return * Convert a (Hue Saturation Lightness) to an RGB value */ public static function hsl2rgb(h:Number,s:Number,l:Number) : Array { var r:int=0,g:int=0,b:int=0,v1:Number=0,v2:Number=0; if(s==0){ r = l*255, g = l*255, b = l*255; }else{ if(l<.5) v2 = l * (1+s); else v2 = (l+s) - (s*l); v1 = 2*l - v2; r = hue2rgb(v1, v2, h + (1/3) ); g = hue2rgb(v1, v2, h ); b = hue2rgb(v1, v2, h - (1/3) ); } return [r,g,b]; } /** * @param hex - hex color string *

Returns a hex color on the opposite side of the colorwheel

*/ public static function getInverseColorOf( hex:String ) : String { var a:Array = hex2dec(hex).split(" "); var s:String = ""; for(var i:int=0;i<3;i++){ s += int(255*(1-(a[i]/255))); if(i<2) s += " "; } return dec2hex(s); } /** * @param hex - hex color string * @param total - total colors * @param weight - offset.. use this from 0-255 to change the weight of the color harmonies * @return - an array of hex color strings that harmonize with the hex color provided * */ public static function getHarmonyColors(hex:String, weight:int = 255) : Array { var rgb:Array = hex2dec(hex).split(" "); var r:String = rgb[0]; var g:String = rgb[1]; var b:String = rgb[2]; var a:Array = new Array(); var w:String = weight.toString(); a.push( dec2hex(r+" "+g+" "+w) ); a.push( dec2hex(r+" "+w+" "+b) ); a.push( dec2hex(w+" "+g+" "+b) ); a.push( dec2hex(r+" "+w+" "+w) ); a.push( dec2hex(w+" "+g+" "+w) ); a.push( dec2hex(w+" "+w+" "+b) ); return a; } /** * @param hex * @param percent * @return *

Returns a lighter shade of the hex color provided. From 1-2

*/ public static function getLighterShadeOf( hex:String, percent:Number=1 ) : String { var a:Array = hex2dec(hex).split(" "); var s:String = ""; for(var i:int=0;i<3;i++){ s += Math.min(int(Number(a[i])*percent),255); if(i<2) s += " "; } return dec2hex(s); } /** * @param hex * @param percent * @return *

Returns a darker shade of the hex color provided. Percent is 0-1.

*/ public static function getDarkerShadeOf( hex:String, percent:Number=1 ) : String { var a:Array = hex2dec(hex).split(" "); var s:String = ""; for(var i:int=0;i<3;i++){ s += int(Number(a[i])*percent); if(i<2) s += " "; } return dec2hex(s); } /** * @param n * @return *

Random number

*/ public static function random(n:Number) : Number { return Math.random() * n; } } }