burger menu icon
WillMaster

WillMasterBlog > CSS

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Converting HEX and RGB Color Codes

HEX colors in HTML begin with a "#" character and are followed with either 3 or 6 hexadecimal digits. Hexadecimal digits range from 0 to F. Hexadecimal digits are case-insensitive. Example: #32Fe9A

RGB colors in HTML are specified with the rgb() or rgba() function. The function requires 3 numbers that range from 0 to 255, separated with commas. Example: rgb(50,254,154)

This article contains source code for a converter. You'll find it further below.

Here is a live implementation. Type a color in the left field and tap the "➜" button. Don't forget to bookmark if you think you may be using this page later.

The input field on the left may contain either a HEX color code or a RGB color code.

When you tap the button in the middle, the input field on the right will contain the converted code.

The converted color code may be copied from the right input field. When the right input field is tapped on, the content is selected and ready to copy.

HEX color code may be specified in the left input field with or without the leading "#" character. An example is #32FE9A

RGB color code is specified in the left input field with 3 numbers separated with commas. Optionally, the code may be specified as a full rgb() or rgba() color code. Examples are rgb(50,254,154) and rgba(50,254,154,.5) (rgba() for transparencies)

Conversions need to be done once in a while. As an example, you may prefer to use the HEX color code and have only the RGB available. This converter can do the conversion for you.

As another example, when you have a HEX color code and want to specify a degree of transparency, the HEX color needs to be converted so it can be used as a rgba() color. (Other conversions are possible, but the HEX color by itself can not be used for transparency.) A degree of transparency can be applied to text or background colors with a rgba() color specification.

Here is the source code for the converter. No modifications are required. Copy the code and paste it into your web page source code where you want the converter to publish.

<input 
   type="text" 
   id="conv_input" 
   style="width:10.6em; font-size:1rem; font-family:monospace;"> 
<input 
   onclick="ConvertNumber()" 
   type="button" 
   value="&#10140;"> 
<input 
   readonly="readonly" 
   onclick="select()" 
   type="text" 
   id="conv_response" 
   style="width:10.6em; font-size:1rem; font-family:monospace;">

<script style="text/javascript">
function dec2hex(d)
{
   d = parseInt(d);
   var h = d.toString(16);
   if( h.length < 2 ) { h = "0" + h; }
   return h.toUpperCase();
}
function hex2dec(h)
{
   if( h.length>1 && h.match(/^0/) ) { h = h.substr(1); }
   d = parseInt(h,16);
   return String(d);
}
function ConvertNumber()
{
   var response = document.getElementById("conv_response");
   n = document.getElementById("conv_input").value;
   n = n.replace(/rgba?/i,"");
   n = n.replace(/[^0-9a-f,]/ig,"");
   if( ! n.length ) { response.value=""; return; }
   if( n.match(/\,/) ) // rgb
   {
      var ta = n.split(/\,+/);
      if(ta.length!=3) { response.value="error"; return; }
      isok = true;
      for( var i=0; i<3; i++ )
      {
         if( ta[i]<0 || ta[i]>255 ) { isok = false; }
      }
      if( ! isok ) { response.value="error"; return; }
      var sta = new Array();
      for( var i=0; i<3; i++ ) { sta.push(dec2hex(ta[i])); }
      response.value = "#" + sta.join("");
      return;
   }
   var s = new String();
   if(n.length==3)
   {
      s = "";
      for( var i=0; i<3; i++ ) { s+=n.substr(i,1)+n.substr(i,1); }
      n = s;
   }
   s = "";
   if( n.length != 6 ) { response.value="error"; return; }
   var sta = new Array();
   for( var i=0; i<3; i++ )
   {
      s = n.substr((i*2),2);
      sta.push(hex2dec(s));
   }
   response.value = "rgb(" + sta.join(",") + ")";
}
</script>

The converter is JavaScript so it will work online or offline. Once loaded into a browser, no additional server resources are required.

The converter is speedy. And it is forgiving with your format. The "#" character is optional — just specify 3 or 6 hexadecimal characters. The rgb() or rgba() parts of color coding are also optional — you can get away with just specifying 3 numbers separated with commas.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this blog post helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method


All information in WillMaster Blog articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

Recent Articles in the Library

File Uploader

This file uploader is complete as one PHP script. No external modules or classes are required. Optional login for private use.

Fun Jumper

This fun jumper is easy to implement. Paste the code into one of your web pages. When you are ready to define your own content, replace the image tag.

The HTML optgroup Tag

The HTML <optgroup> tag allows you to group items in a dropdown list.

Determining Div Location

I generally get the location and dimension numbers of a div with the JavaScript getBoundingClientRect() function. Then access the numbers I need from the function's return value.

Site-wide Login System

A site-wide log-in system that lets pretty much any directory be password protected.

Image Show

With the no-custimizations-required script, it is now relatively easy to show off a series of images on your web page.

A Variables Dump

This PHP function returns details about the variable that is being dumped.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2026 Will Bontrager Software LLC