RGB to Hex Color Converter

Use the tool below to convert RGB to Hex (hexidecimal). The R, G and B textboxes allow values of 0-255.

Resulting Color: 

RGB to Hex Color Converter Script

Here's the RGB to Hex Color Converter script that I'm using on this site. As is often the case with programming, I borrowed a script and then modified to my preferences

        function rgbToHex(R, G, B) { return toHex(R) + toHex(G) + toHex(B) }

        function toHex(N) {
        if (N === null) return "00";
        N = parseInt(N); if (N === 0 || isNaN(N)) return "00";
        N = Math.max(0, N); N = Math.min(N, 255); N = Math.round(N);
        return "0123456789ABCDEF".charAt((N - N % 16) / 16)
          + "0123456789ABCDEF".charAt(N % 16);}

Introduction to the Hexadecimal System

In both computer science and mathmatics, hexadecimal or hex, is a positional numeral system with a base of 16 written using the symbols 0-9 to represent characters zero to nine and A-F to represent values 10-15. The numbers 0-9 represent the same value as in the decimal system but starting with the letter A, the value increments by one up to the value of 15 for each letter. So A = 10, B = 11, C = 12, ... F = 15.

Hexadecimal Color Codes

Hexadecimal Color codes are six character sequences that represent a possible 16.7 million color combinations. These six character color codes represent two hex codes for each color value of the RGB color model. In RGB, R stands for red, G for green, and B for blue. This means that the first two hex digits represent 'red', the second set of two hex digits represent the color green, and the third set of two hex digits represent the color blue.

The RGB Color Model

The RGB color model represents a possible 16.7 million color combinations. By adjusting the amount of red, green, and blue, any one of 16.7 possible million colors can be achieved. Each RGB value is represented by a number from 0 to 255. For example, the RGB values representing color white is 255, 255, 255. Black is at the opposite end of the values 0, 0, 0.

Converting RGB to Hex

Now it's time to convert RGB to Hex. So just how do you come up with the two hex values for a single RGB value? It's easier than you might think. Let's say your red value is 77. First take your RGB value and divide it by 16.

77 / 16 = 4.812

The whole number, 4, becomes the first part of your hex code. Now take the remainder and multiply by 16 and then round up.

.812 x 16 = 12.99 rounded up is 13

We know that the first part of our hex code is 4 so all we need to do is determine the value representing 13. If you refer to the first part of this page you'll see that 13 is represented by a D. Our resulting hex code is 4D. Simply repeat the same calculation for Green and Blue and you have a complete hex color code.

RGB to Hex Color Converter Copyright 2016 ©, RGBtoHex.com