CFLib.org – Common Function Library Project

ColorShade(hexColor, shade)

Last updated March 3, 2010

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

 
Rated 0 time(s). Average Rating: 0

Description:
Returns a lighter hex color if a positive number is used and a darker if a negative number is used. Arguments are a six character hexadecimal value (no #) and the numeric value of how much the color should be incremented. -100 increments the provided color 100% toward black and 100 increments the color 100% toward white. Works well for building gradients using a loop or for generating monochromatic color schemes.

Return Values:
returns a string

Example:

view plain print about
<cfset darkShade = ColorShade("6F23A8", -50)>
<cfset lightShade = ColorShade("6F23A8", 50)>

<cfoutput>
<font color="###darkShade#">#darkShade#</font> is a darker shade of <font color="##6F23A8">6F23A8</font>.<br />
<font color="###lightShade#">#lightShade#</font> is a darker shade of <font color="##6F23A8">6F23A8</font>.<br />
</cfoutput>

Parameters:

Name Description Required
hexColor starting hex color Yes
shade amount hexColor should be incremented(+)/decremented(-) Yes

Full UDF Source:

view plain print about
<!---
 Returns a hexadecimal color value an amount lighter or darker than the provided color.
 
 @param hexColor      starting hex color (Required)
 @param shade      amount hexColor should be incremented(+)/decremented(-) (Required)
 @return returns a string 
 @author Bob Gray (gray.bob98@gmail.com) 
 @version 0, March 3, 2010 
--->

<cffunction name="ColorShade" returntype="string">

<cfargument name="hexColor" type="string" required="yes">
<cfargument name="shade" type="numeric" required="yes">
    
<cfset var red = "">
<cfset var green = "">
<cfset var blue = "">
<cfset var s = ARGUMENTS.shade>
<cfset var incrementedColor = ""> 

<cfset red = Left(ARGUMENTS.hexColor, 2)>
<cfset green = Mid(ARGUMENTS.hexColor, 3, 2)>
<cfset blue = Right(ARGUMENTS.hexColor, 2)>
    
<cfset red = NumberFormat(InputBaseN(red, 16), 00)>
<cfset green = NumberFormat(InputBaseN(green, 16), 00)>
<cfset blue = NumberFormat(InputBaseN(blue, 16), 00)>    
    
<cfset red = IIF(s LT 0, red * (s + 100) / 100, red + (255 - red) * s / 100)>
<cfset green = IIF(s LT 0, green * (s + 100) / 100, green + (255 - green) * s / 100)>
<cfset blue = IIF(s LT 0, blue * (s + 100) / 100, blue + (255 - blue) * s / 100)>
    
<cfset red = UCase(FormatBaseN(red, 16))>
<cfset green = UCase(FormatBaseN(green, 16))>
<cfset blue = UCase(FormatBaseN(blue, 16))>
    
<cfset red = IIF(Len(red) LT 2, DE(0&red), DE(red))>
<cfset green = IIF(Len(green) LT 2, DE(0&green), DE(green))>
<cfset blue = IIF(Len(blue) LT 2, DE(0&blue), DE(blue))>
    
<cfset incrementedColor = UCase(red&green&blue)>
    
<cfreturn incrementedColor>
    
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

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

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

Ben Forta Ben Forta added
GetHostAddress
28 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