CFLib.org – Common Function Library Project

RandomColor()

Last updated May 13, 2003

author

Nathan Strutz

Version: 2 | Requires: CF5 | Library: UtilityLib

Description:
Simply place RandomColor() anywhere you want a truly random HTML color. Fun for the whole family!

Return Values:
Returns a string.

Example:

<cfoutput>
<table border=1 cellspacing=0>
  <tr>
<cfloop from=1 to=10 index="z">
    <td bgcolor="#RandomColor()#">&nbsp;</td>
</cfloop>
  </tr>
  <tr>
<cfloop from=1 to=10 index="z">
    <td bgcolor="#RandomColor()#">&nbsp;</td>
</cfloop>
  </tr>
</table>
</cfoutput>

Parameters:

No arguments.

Full UDF Source:

/**
 * Returns a completely random color. Beautiful, isn't it?
 * Version 2 by Raymond Camden
 * 
 * @return Returns a string. 
 * @author Nathan Strutz (nathans@dnsfirm.com) 
 * @version 2, May 13, 2003 
 */
function randomColor() {
    var redColor = formatBaseN(randRange(0,255),16);
    var greenColor = formatBaseN(randRange(0,255),16);
    var blueColor = formatBaseN(randRange(0,255),16);
    
    if(len(redColor) is 1) redColor = "0" & redColor;
    if(len(greenColor) is 1) greenColor = "0" & greenColor;
    if(len(blueColor) is 1) blueColor = "0" & blueColor;

    return "##" & redColor & greenColor & blueColor;
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson