Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
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

Keeping Image Location Secret

The URL of an image embedded in a web page may be kept secret.

Easier Reading of JSON Data

For easier reading of JSON data, convert the JSON into an array.

Fixed Position Image

Position an image within a browser window that won't move even with page scroll.

Visually Centering Images

Sometimes an image that is technically centered doesn't look quite centered when viewed.

Cookie Directory Protection

Protecting subdirectories with a cookie can be an especially good method when access needs to be allowed from various internet connections.

Check SSL Certificate

An easy-to-use SSL checker to see when your secure certificate expires.

Strong Form Protection From Bots

A web page form that is invisible to spam bots.

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
software index page

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