CFLib.org – Common Function Library Project

RandomColorClose(color, closeness)

Last updated June 12, 2003

author

nathan

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Returns a random color close to your given color. You give it the starting color and the range of how close you want it, and it will return a variation on that color. I recommend a closeness of 10 to 50 to make it different enough, but not too much.

Return Values:
Returns a RGB color in the form of a string.

Example:

<table border=1 cellpadding=1 cellspacing=0>
  <tr>
<cfoutput>
<cfset CurrentColor = "ABE32B">
<cfloop from="1" to="50" index="i">
<cfset CurrentColor = RandomColorClose(CurrentColor,25)>
	<td bgcolor="#CurrentColor#">&nbsp;</td>
</cfloop>
</cfoutput>
  </tr>
</table>

Parameters:

Name Description Required
color RGB color, minus the #. Yes
closeness How close the random color should be to the original. Yes

Full UDF Source:

/**
 * Returns a random color close to your given color.
 * 
 * @param color 	 RGB color, minus the #. (Required)
 * @param closeness 	 How close the random color should be to the original. (Required)
 * @return Returns a RGB color in the form of a string. 
 * @author nathan (nathans@dnsfirm.com) 
 * @version 1, June 12, 2003 
 */
function randomColorClose(color,closeness) {
	var redColor = "";
	var greenColor = "";
	var blueColor = "";

	redColor = InputBaseN(Mid(color,1,2),16);
	greenColor = InputBaseN(Mid(color,3,2),16);
	blueColor = InputBaseN(Mid(color,5,2),16);

	// randomize and format back to base 16. min and max functions ensure characters don't leave base 16 size.
	redColor = FormatBaseN(Min(255,Max(0,RandRange(redColor-closeness,redColor+closeness))),16);
	greenColor = FormatBaseN(Min(255,Max(0,RandRange(greenColor-closeness,greenColor+closeness))),16);
	blueColor = FormatBaseN(Min(255,Max(0,RandRange(blueColor-closeness,blueColor+closeness))),16);

	// fix formatting
	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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson