CFLib.org – Common Function Library Project

RGBtoHex(r, g, b)

Last updated November 27, 2001

author

Eric Carlisle

Version: 1 | Requires: CF5 | Library: UtilityLib

Description:
Converts an RGB color value into a hexadecimal color value.

Return Values:
Returns a string.

Example:

<cfoutput>
00,151,102: #RGBtoHex(00,51,102)#<BR>
255,255,255: #RGBtoHex(255,255,255)#<BR>
0,0,0: #RGBtoHex(0,0,0)#<BR>
</cfoutput>

Parameters:

Name Description Required
r Red value triplet (0-255) Yes
g Green value triplet (0-255) Yes
b Blue value triplet (0-255) Yes

Full UDF Source:

/**
 * Converts an RGB color value into a hexadecimal color value.
 * 
 * @param r      Red value triplet (0-255) 
 * @param g      Green value triplet (0-255) 
 * @param b      Blue value triplet (0-255) 
 * @return Returns a string. 
 * @author Eric Carlisle (ericc@nc.rr.com) 
 * @version 1, November 27, 2001 
 */
function RGBtoHex(r,g,b){
  Var hexColor="";
  Var hexPart = '';
  Var i=0;
    
  /* Loop through the Arguments array, containing the RGB triplets */
  for (i=1; i lte 3; i=i+1){
    /* Derive hex color part */
    hexPart = formatBaseN(Arguments[i],16);
        
    /* Pad with "0" if needed */
    if (len(hexPart) eq 1){
      hexPart = '0' & hexPart;    
    } 
        
    /* Add hex color part to hexadecimal color string */
    hexColor = hexColor & hexPart;
  }
  return hexColor;
}

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