ScienceComputer Science

RGB to HEX Color
converter.

Turn a hex code into RGB, HSL, HSV and CMYK, and see which text color reads on it.

How it's worked out

A hex code is three numbers written in base 16, one each for red, green and blue. Each number takes two digits. The first digit counts sixteens and the second counts ones, and the letters A to F stand for 10 to 15. So the pair 3A is 3 sixteens and 10 ones, which is 58.

channel\displaystyle \text{channel}=16×first digit+second digit\displaystyle {}= 16 \times \text{first digit} + \text{second digit}

Each channel runs from 0 (00) to 255 (FF), the range of one byte. CSS also accepts a short form with three digits, where each digit is written twice: F0F means FF00FF. An eight-digit code adds a fourth pair for alpha, the opacity, where 00 is fully clear and FF is solid.

HSL describes the same color as hue, saturation and lightness. First divide each channel by 255, so it runs from 0 to 1. Call the largest of the three max and the smallest min. Then lightness and saturation are:

L\displaystyle L=max+min2\displaystyle {}= \frac{\text{max} + \text{min}}{2}
S\displaystyle S=max−min1−|2L−1|\displaystyle {}= \frac{\text{max} - \text{min}}{1 - |2L - 1|}

The hue is an angle on the color wheel, from 0° (red) through 120° (green) and 240° (blue) back to 360°. Which channel is largest decides where the angle starts. HSV, also called HSB, uses value V, the largest channel, in place of lightness, and its saturation is (max − min) divided by max. A pure color such as orange has a lightness of 50% but a value of 100%.

The CMYK figures use the simple formula: black K is 1 minus the largest channel, and each ink is what is left after the black, scaled up. It has no printer profile, so treat it as a starting point for a print shop, not a proof.

For text, the tool works out the relative luminance of the color, a brightness figure from 0 for black to 1 for white that weights green most and blue least. The contrast ratio of two colors is then:

contrast\displaystyle \text{contrast}=L1+0.05L2+0.05\displaystyle {}= \frac{L_1 + 0.05}{L_2 + 0.05}

Here L1L_1 is the luminance of the lighter color and L2L_2 of the darker one. The ratio runs from 1 (the same color) up to 21 (black on white).

Worked examples

A brand blue in a style guide

Priya's style guide lists the brand blue as #3A7BD5 and she needs it as rgb() for her components. The pairs 3A, 7B, D5 read as 58, 123, 213, so it is rgb(58, 123, 213), or hsl(215, 65%, 53%) in HSL.

The team planned white button text. On this blue, white reaches 4.22:1, which is enough only for large text. Black text reaches 4.97:1, which passes AA for body text. So white works for a large heading on this blue, but a small label needs black text or a darker blue.

Open this example in the tool

An orange button from a stylesheet

Marco's old stylesheet has a button set to rgb(255, 128, 0). As hex that is #FF8000: 255 is FF, 128 is 80 and 0 is 00. In HSL it is hsl(30, 100%, 50%), a fully saturated orange at the middle of the lightness scale.

The button has white text, which gets only 2.52:1 on this orange and fails every WCAG level. Black text gets 8.34:1, which reaches AAA. Bright oranges and yellows have a high luminance, so dark text is the one that reads.

Open this example in the tool

A half-clear overlay

A designer hands over 00000080 for the shade behind a pop-up. The first three pairs are 00, so the color is black, #000000. The last pair, 80, is alpha: 128 out of 255, an opacity of about 50%.

What you see depends on the page behind it: over white it looks mid gray, over a photo it darkens the photo by about half. The contrast figures treat the code as solid black, so test text against the color it ends up as on the page.

Open this example in the tool

Reading your result

The big code is the six-digit hex form, with alpha added as a fourth pair when your code has one. Under it is the same color as rgb(), ready to paste into CSS. The row below gives HSL and HSV as hue in degrees with two percentages, the simple CMYK as four percentages, and the opacity.

The contrast levels come from WCAG 2.2. Body text needs at least 4.5:1 to pass AA and 7:1 to pass AAA. Large text, meaning 18 point or 14 point bold, needs 3:1 for AA, and so do icons, borders and other interface parts. "AA large only" means the pair is fine for a heading but not for a paragraph.

The two swatches beside your color are two different ideas of a complement. The RGB inverse takes each channel away from 255. The hue complement keeps saturation and lightness and turns the hue half way round the wheel. For a bright, saturated color they are close; for a muted or very light color they can differ a lot.

The shade table keeps your hue and saturation and steps the lightness from 90% down to 10%. That is a quick way to get a lighter background and a darker hover state that still look like the same color.

Common mistakes

  • Reading the alpha pair as a percentage. In 00000050 the 50 is hex, which is 80 out of 255, about 31% opacity, not 50%. Half opacity is 80.
  • Taking the RGB inverse as the complement of a gray. The inverse of 808080 is 7F7F7F, almost the same gray. Turning the hue does nothing either (808080), because a gray has no hue to turn.
  • Judging contrast by eye. White on #3A7BD5 looks clear on a bright screen, yet it is 4.22:1, below the 4.5:1 that body text needs.
  • Sending the CMYK figures to a printer as final. The formula here has no paper or ink profile. Bright screen blues and greens often cannot be printed at all, so ask for a proof.
  • Mixing up lightness and value. In HSL, 100% lightness is always white. In HSV, 100% value is the brightest form of the hue, so a pure red is hsv(0, 100%, 100%) but hsl(0, 100%, 50%).

Questions people ask

Split the six digits into three pairs. For each pair, multiply the first digit by 16 and add the second, counting A as 10 through F as 15. A pair of FF is always 255 and 80 is 128, which helps you sanity check an answer.

Sources