CFLib.org – Common Function Library Project

RandomColorClose(color, closeness)

Last updated June 12, 2003

Version: 1 | Requires: ColdFusion 5 | Library: UtilityLib

 
Rated 0 time(s). Average Rating: 0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson