CFLib.org – Common Function Library Project

ClosestWebSafeColor(hexInput)

Last updated November 21, 2001

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

 
Rated 0 time(s). Average Rating: 0

Description:
Takes a hex color in, and returns the closest web safe hexadecimal color. Returns a NULL (empty) string if input hex string is invalid. Thanks to Eric Carlisle (IsWebSafeColor)

Return Values:
Returns a string.

Example:

view plain print about
<CFOUTPUT>
003366, #closestWebSafeColor('003366')#<br>
aabbcc, #closestWebSafeColor('aabbcc')#<br>
112233, #closestWebSafeColor('112233')#<br>
eeffgg, #closestWebSafeColor('eeffgg')#<br>
fffffcc, #closestWebSafeColor('fffffcc')#<br>
##c0c00c, #closestWebSafeColor('##c0c00c')#
</CFOUTPUT>

Parameters:

Name Description Required
hexInput Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Returns the closest web safe hexadecimal color for a given hexadecimal color.
 * 
 * @param hexInput       
 * @return Returns a string. 
 * @author Tony Brandner (brandner@canada.com) 
 * @version 1, November 21, 2001 
 */

function closestWebSafeColor(hexInput) {
  var cleanHexInput = replace(hexInput,'##','','ALL');
  var hexOutput = '';
  var i = 0;
  if (Len(ReReplace(cleanHexInput, "[0-9abcdefABCDEF]""","ALL")) eq 0 and Len(cleanHexInput) eq 6) {
    for (i=1; i lte 5; i=i+2) {
      closestMatch = 51 * Round((InputBaseN(mid(cleanHexInput,i,2),16)/51));
      if (closestMatch eq 0) {
        hexOutput = hexOutput & '00';
    } 
      else {
        hexOutput = hexOutput & FormatBaseN(closestMatch,16);
      }
    }
    return hexOutput;
  } 
  else {
    return 'invalid';
  }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

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

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

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