Disclosure: Some tools link to SaaS products we may earn a commission from — at no cost to you. All tools are free and run entirely in your browser. See full disclosure →

Reading a Hex Color Code

A CSS hex color code like #FF5733 encodes three 8-bit channels in 6 hexadecimal digits. The first two digits are red, the middle two are green, and the last two are blue. Reading FF as decimal gives 255 (full red). 57 is 87 (moderate green). 33 is 51 (low blue). Combined, this produces a warm orange-red. Once you understand this structure, you can approximate any hex color by eye — dark values start with 0–3, mid-range with 7–8, bright values with C–F.

3-Digit Hex Shorthand

CSS allows a 3-digit shorthand when both digits of each channel are the same: #FFF expands to #FFFFFF (white), #000 to #000000 (black), #F00 to #FF0000 (pure red). To expand: duplicate each digit. #A3C becomes #AA33CC. This converter handles both formats automatically.

Using RGB Values in JavaScript

When working with the Canvas API, WebGL, or image processing libraries, you often need integer RGB values rather than hex strings. Node.js image libraries like Sharp accept rgb(r, g, b) objects. The CSS-in-JS library Emotion's color utility functions work with { r, g, b } objects. Matplotlib and Pillow in Python also accept (r, g, b) tuples.

// JavaScript — parse hex to { r, g, b }
function hexToRgb(hex) {
  hex = hex.replace(/^#/, '');
  if (hex.length === 3) hex = hex.split('').map(c => c + c).join('');
  const r = parseInt(hex.slice(0, 2), 16);
  const g = parseInt(hex.slice(2, 4), 16);
  const b = parseInt(hex.slice(4, 6), 16);
  return { r, g, b };
}
hexToRgb('#FF5733'); // → { r: 255, g: 87, b: 51 }

// Python
def hex_to_rgb(hex_str):
    h = hex_str.lstrip('#')
    return tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
hex_to_rgb('#FF5733')  # → (255, 87, 51)

Accessibility: Contrast Ratios Need RGB

WCAG colour contrast calculations require relative luminance, which is derived from linear RGB values. You must first convert hex to RGB, then linearise each channel (gamma correction), and finally compute luminance. The colour contrast checker on this site does this automatically, but understanding hex-to-RGB conversion is the first step.

Frequently Asked Questions

How do I convert a hex color to RGB in JavaScript?

Use parseInt with radix 16 on each 2-character pair: const r = parseInt(hex.slice(1,3), 16). For example, for #FF5733: r = parseInt('FF', 16) = 255, g = parseInt('57', 16) = 87, b = parseInt('33', 16) = 51.

How do I handle the 3-digit hex shorthand like #FFF?

Expand each digit by doubling it: #FFF becomes #FFFFFF, #A3C becomes #AA33CC. In code: hex.split('').map(c => c + c).join('') converts a 3-character string to 6 characters. Then parse as a normal 6-digit hex.

What does #000000 and #FFFFFF mean in RGB?

#000000 is rgb(0, 0, 0) — pure black, all channels off. #FFFFFF is rgb(255, 255, 255) — pure white, all channels at maximum. Any color with equal R, G, and B values is a shade of grey.

What is an 8-digit hex color (#RRGGBBAA)?

8-digit hex adds a fourth pair for the alpha (opacity) channel: #FF573380 is #FF5733 at 50% opacity (80 hex = 128 decimal ≈ 50% of 255). This is part of the CSS Color Level 4 spec and is widely supported in modern browsers. For older compatibility, use rgba() instead.

How do I detect whether a color is light or dark from its hex code?

Convert to RGB, then compute relative luminance: L = 0.2126 × R/255 + 0.7152 × G/255 + 0.0722 × B/255 (simplified, without gamma correction). If L > 0.5, the color is light; otherwise it is dark. This is used to decide whether to show white or black text over a background color.

Number & Date Converters
Hex to Decimal Decimal to Hex Binary to Decimal Decimal to Binary RGB to Hex Timestamp to Date Date to Timestamp Unix to ISO 8601 ISO 8601 to Unix
💡
Need AI tools for your development workflow?

Explore our reviews of the best AI coding assistants, documentation generators, and developer productivity tools — ranked by real-world usefulness.

Browse AI Tools for Developers →
The DevTools Team
Infinfy Engineering
We build free developer utilities that we actually use ourselves. No accounts, no tracking, no backend — just fast, accurate, in-browser tools. Part of Infinfy Solutions.