Selecting Contrasting Text Color Based On Background Color

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>Selecting Contrast Text Color</title>
 
	<!--- Style sheets. --->
	<style type="text/css">
 
		body {
			font: 10px verdana ;
			margin: 0px 0px 0px 0px ;
			}
 
		p {
			border: 1px solid #000000 ;
			border-width: 0px 1px 1px 0px ;
			float: left ;
			line-height: 20px ;
			margin: 0px 0px 0px 0px ;
			text-align: center ;
			width: 9% ;
			}
 
	</style>
</head>
<body>
 
	<cfoutput>
 
 
		<!--- Loop over all colors. --->
		<cfloop
			index="intColor"
			from="1"
			to="#ArrayLen( arrColors )#"
			step="1">
 
			<!--- Get the current background color. --->
			<cfset strHEX = arrColors[ intColor ] />
 
 
			<!---
				We are gonna get the decimal values for the
				different HEX parts (R,G,B). However, we
				don't really care about all the numbers, we
				only care about digits 1, 3, and 5 as those
				carry the most weight.
			--->
 
			<!--- Get red in decimal format. --->
			<cfset intRed = InputBaseN(
				Mid( strHEX, 1, 1 ),
				16
				) />
 
			<!--- Get green in decimal format. --->
			<cfset intGreen = InputBaseN(
				Mid( strHEX, 3, 1 ),
				16
				) />
 
			<!--- Get blue in decimal format. --->
			<cfset intBlue = InputBaseN(
				Mid( strHEX, 5, 1 ),
				16
				) />
 
 
			<!---
				Now that we have the background HEX color
				in RGB values, we are going to find the most
				appropriate foreground (text) color to use.
			--->
			<cfif (
				<!--- Very green values. --->
				(intGreen GT 9) OR
 
				<!--- Very light values. --->
				((intRed + intGreen + intBlue) GT 30)
				)>
 
				<!---
					For very green and very light colors,
					we want to contrast that with black text.
				--->
				<cfset strColor = "000000" />
 
			<cfelse>
 
				<!--- Default to white. --->
				<cfset strColor = "FFFFFF" />
 
			</cfif>
 
 
			<p style="background-color: ###strHEX# ;">
 
				<span style="color: ###strColor# ;">
					#strHEX#<br />
					#intRed#:#intGreen#:#intBlue#
				</span>
 
			</p>
 
		</cfloop>
 
	</cfoutput>
 
</body>
</html>

For Cut-and-Paste