Guide

How to Convert RGB to HEX

A HEX colour code is the same three numbers as an RGB colour, written in base 16. Once you see the pattern, you can convert in your head.

Updated

Try it now: RGB to HEX Converter. It runs in your browser, with no signup.

The conversion in three steps

Each RGB channel is a whole number from 0 to 255. Each one becomes exactly two hexadecimal digits, and the three pairs are joined after a #.

  • Divide the channel by 16. The whole part is the first hex digit.
  • The remainder is the second hex digit.
  • Digits 10 to 15 are written A to F. Repeat for red, green and blue.
rgb(59, 130, 246)
59  = 3 * 16 + 11  ->  3B
130 = 8 * 16 + 2   ->  82
246 = 15 * 16 + 6  ->  F6

#3B82F6

Alpha (transparency)

An 8-digit HEX code adds a fourth pair for alpha. Multiply the alpha value (0 to 1) by 255, round, and convert like any other channel. rgba(59, 130, 246, 0.5) gives 0.5 * 255 = 127.5, which rounds to 128, or 80 in hex, so the code is #3B82F680. Modern CSS also accepts the space-separated form rgb(59 130 246 / 50%).

3-digit shorthand

A 3-digit code such as #FA0 doubles every digit, so it means #FFAA00. A colour can only be shortened when each pair has two identical digits. #3B82F6 has no shorthand.

Code for the common languages

// JavaScript
const toHex = (r, g, b) =>
  "#" + [r, g, b].map(n => n.toString(16).padStart(2, "0")).join("").toUpperCase();
toHex(59, 130, 246);   // "#3B82F6"

# Python
r, g, b = 59, 130, 246
print("#{:02X}{:02X}{:02X}".format(r, g, b))   # #3B82F6

// C#
int r = 59, g = 130, b = 246;
string hex = 
quot;#{r:X2}{g:X2}{b:X2}"; // #3B82F6

Common mistakes

The RGB to HEX Converter accepts rgb(), rgba() and plain r, g, b input, and shows an error for values outside 0 to 255.

  • Values above 255 or below 0 do not map to two hex digits. Check the range first.
  • Forgetting the leading zero: 5 must be written 05, not 5.
  • Mixing percentages and numbers: rgb(100%, 50%, 0%) is not rgb(100, 50, 0).
  • Pasting a string such as rgb(59, 130, 246) into code that expects three separate numbers.

Frequently asked questions

Is HEX case sensitive?

No. #3b82f6 and #3B82F6 are the same colour. Pick one style and keep it consistent.

How do I convert HEX back to RGB?

Split the code into three pairs and convert each pair from base 16 to decimal: 3B is 59, 82 is 130, F6 is 246.

What does an 8-digit HEX code mean?

The last two digits are the alpha channel, where 00 is fully transparent and FF is fully opaque.

Tools used in this guide

onlinedevelopertool.com is a collection of 54 free, browser-based utilities for the small jobs that interrupt real work: formatting a JSON response, decoding a token, converting a timestamp, generating a UUID or checking a hash. Each tool has its own page with a short explanation, a worked example and answers to common questions, so you can use it and learn what it does at the same time.

There is no account to create and nothing to install. Tools run in your browser, so the text you paste is processed on your own device. The one exception is the Website Status Checker, which sends a request from your browser to the address you enter.

Three steps: paste your input, click the tool, copy the result, all inside your browser
Popular developer utilities: JWT decoder, UUID generator, Base64 decoder, SHA-256 hash, regex tester and timestamp converter

What you can do here

  • JSON Tools

    Format, validate, beautify and minify JSON, generate C#, TypeScript, Java, Python, Go, PHP and Kotlin models from it, or convert between JSON, YAML and CSV.

  • Encoders & Decoders

    URL, Base64 and HTML encoding and decoding, plus a JWT decoder for reading token claims.

  • Generators & Hashes

    Generate UUIDs and JWTs, validate UUIDs, and create MD5, SHA-256 and SHA-512 hashes.

  • Web Tools

    Timestamps, color codes, query strings and cron expressions - the small things web work keeps throwing at you.

  • Text & Data

    Format XML and YAML, count text, change case, make slugs, test regex, diff two texts and convert number bases.

  • Website Checking

    Check whether a website or URL is reachable from your browser.

Quick developer reference

  • JSON

    Keys and strings must use double quotes, and trailing commas and comments are not allowed. Those are the three most common reasons a payload fails to parse.

    Validate JSON
  • JWT

    A JSON Web Token is three Base64URL parts joined by dots: header, payload and signature. Decoding shows the claims but does not prove the token is genuine; only signature verification does.

    Decode a JWT
  • Base64

    Base64 is an encoding, not encryption. Anyone can reverse it, and it makes data roughly one third larger, so never use it to hide secrets.

    Decode Base64
  • UUID

    A version 4 UUID is made of random bits and written as 8-4-4-4-12 hexadecimal characters. Collisions are astronomically unlikely, which is why they work well as identifiers.

    Generate a UUID
  • Timestamps

    A Unix timestamp counts time since 1 January 1970 UTC. Ten digits usually means seconds and thirteen digits usually means milliseconds.

    Convert a timestamp
  • Hashes

    MD5 is fine for quick checksums but is not safe for security. Prefer SHA-256 or SHA-512, and remember that hashing is one-way and is not encryption.

    Create a SHA-256 hash

Missing a tool you need? Visit the contact page and tell us what you reach for most often. You can also browse every tool or read the About page to see how the site is built and what it does with your data.