CFLib.org – Common Function Library Project

IsColor(color)

Last updated May 09, 2009

author

Jon Hartmann

Version: 0 | Requires: CF8 | Library: StrLib

Description:
Checkes to see if a color is a valid 3 or 6 character hex color, or one of the ColdFusion safe color keywords (aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow).

Return Values:
returns a Boolean

Example:

Is #fff a valid color? <cfdump var="#IsColor('##fff')#"><br />
Is #0fff a valid color? <cfdump var="#IsColor('##0fff')#"><br />
Is #000fff a valid color? <cfdump var="#IsColor('##000fff')#"><br />
Is blue a valid color? <cfdump var="#IsColor('blue')#"><br />
Is brown a valid color? <cfdump var="#IsColor('brown')#">

Parameters:

Name Description Required
color string of color to test Yes

Full UDF Source:

<!---
 Checkes to see if a color is a valid 3 or 6 character hex color, or one of the ColdFusion safe color keywords.
 
 @param color      string of color to test (Required)
 @return returns a Boolean 
 @author Jon Hartmann (jon.hartmann@gmail.com) 
 @version 0, May 9, 2009 
--->
<cffunction name="IsColor" access="public" output="false" returntype="boolean">
    <cfargument name="color" type="string" required="true" />
        
    <cfset var local = structNew() />
    <cfset local.colorNames = "aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,white,yellow" />
    <cfset local.regex = "^(##([\dA-F]{3}|[\dA-F]{6})|([\dA-F]{3}|[\dA-F]{6}))$" />
    <cfset local.returnValue = false />
        
    <cfif listFind(local.colorNames, arguments.color) OR arrayLen(REMatchNoCase(local.regex, arguments.color)) gt 0>
        <cfset local.returnValue = true />
    </cfif>
    
    <cfreturn local.returnValue />
</cffunction>

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