RGB to HEX Converter
Convert standard red, green, blue color profiles into short hexadecimal codes.
How to Use
Enter the Red channel value (0–255)
Input the red component of your color from 0 (no red) to 255 (full red).
Enter the Green channel value (0–255)
Input the green component from 0 to 255.
Enter the Blue channel value (0–255)
Input the blue component from 0 to 255 to complete the RGB triplet.
Copy the hex output
Get the #RRGGBB hex string ready to paste into CSS, HTML, SVG, or design tools.
From Decimal Channels to Hexadecimal Notation: The Science of RGB to HEX
While graphics software, camera sensors, and canvas APIs capture and process visual light intensities as raw decimal numbers, web browsers, CSS stylesheets, SVG documents, and digital brand style guides universally favor Hexadecimal color strings. Hex codes provide a compact, standardized 6-character representation that prevents syntax verbosity in stylesheets and design tokens.
The RGB to HEX Converter translates decimal Red, Green, and Blue channel coordinates (ranging from 0 to 255) into standard 6-digit hex notation (e.g., #1E293B). The tool handles zero-padding validation, opacity alpha values, and format formatting entirely within your browser.
The Conversion Algorithm: Integer Division and Modulo
Translating an 8-bit integer into a 2-character hexadecimal string requires converting a base-10 value into base-16. For each channel ($C$):
- Clamp Bounds: Verify that the input integer lies strictly within the 8-bit range ($0 le C le 255$). If a user inputs values outside this range, they must be clamped to prevent integer overflow.
- First Hex Character (High Nibble): Perform integer division by 16:
Digit 1 = Math.floor(C / 16) - Second Hex Character (Low Nibble): Calculate the remainder using the modulo operator:
Digit 2 = C % 16 - Symbol Mapping: Map values 10 through 15 to letters
AthroughF(10 ➔ A, 11 ➔ B, 12 ➔ C, 13 ➔ D, 14 ➔ E, 15 ➔ F).
The Single-Digit Zero-Padding Bug in Web Development
A classic bug in frontend development occurs when programmers write manual conversion scripts using JavaScript's native Number.prototype.toString(16):
// Flawed naive conversion:
const r = (255).toString(16); // "ff"
const g = (7).toString(16); // "7" <-- DANGER: Single character!
const b = (15).toString(16); // "f" <-- DANGER: Single character!
const hex = "#" + r + g + b; // Result: "#ff7f" (Invalid 4-character hex!)
When an RGB channel value is less than 16 (such as 7), its base-16 representation is a single digit. Concatenating raw strings without zero-padding produces an invalid 4 or 5-character string that breaks CSS rendering. To ensure validity, single-digit outputs must always be zero-padded using padStart(2, '0'): yielding the correct #FF070F.
Bitwise Shift Conversion Shortcut
In high-performance game development, WebGL rendering, and production JavaScript libraries, developers avoid string allocations by utilizing bitwise binary shifts:
function rgbToHex(r, g, b) {
// Bitwise shift: R shifted 16 bits left, G shifted 8 bits, B unshifted
// Adding 0x1000000 ensures leading zeroes are retained
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
}
RGB vs. Hex Application Reference Table
| Development Context | Recommended Format | Engineering Rationale |
|---|---|---|
| CSS Stylesheets & Tailwind Tokens | HEX (#RRGGBB) | Compact string representation, smaller bundle sizes, universal IDE color picker support. |
| HTML5 2D Canvas Context | RGB / RGBA | Required when dynamically manipulating alpha channels or computing color blends in animation loops. |
| WebGL Shaders & Three.js | Normalized Floats (0.0 – 1.0) | GPU vector mathematics require division by 255.0 to represent color intensity in floating-point matrix units. |
| Figma / Sketch Asset Handoff | HEX | Universal copy-paste standard between design wireframes and production code. |
Real-World Examples & Use Cases
CSS and HTML Stylesheet Authoring
CSS color properties (color, background-color, border-color, box-shadow) accept hex codes as the most concise notation. When an image editing tool or color picker provides an RGB readout (e.g. from color sampling a screenshot), developers need to convert to hex before writing it into their stylesheet. A designer extracting a brand color from a photo may get rgb(34, 197, 94) from their eyedropper — converting to #22C55E produces the clean hex value for the design system.
Design File Color Specification
Design-to-developer handoffs require consistent color notation. Figma, Adobe XD, and Sketch all display and export colors in hex notation within their inspect panels. When a developer receives colors from a design tool that exposed RGB values (older photo editing workflows, color calibration data, or color science pipelines), converting to hex ensures the values integrate cleanly into CSS design tokens, Tailwind config files, and component library theme objects that standardize on hex notation.
Programmatic Color Generation
Applications that generate colors programmatically — gradient generators, color scheme algorithms, procedural art, data visualization — often compute RGB channel values mathematically before needing the hex code representation for display. A chart library computing a 10-step color ramp from red to blue interpolates R, G, B values at each step and must convert each to hex for CSS output. Color blending, shade generation, and tint calculation all produce numerical RGB values that need hex conversion for browser rendering.
SVG and Canvas Drawing Code
SVG fill and stroke attributes use hex color strings: fill='#3B82F6'. When SVG colors are computed from data values (e.g. choropleth maps where darker shades represent higher values), the RGB channel values are calculated mathematically and need to be formatted as hex strings for the SVG attribute. Similarly, Canvas 2D contexts accept hex strings for fillStyle and strokeStyle, making RGB-to-hex conversion a routine step in dynamic visualization and generative graphics code.
How It Works
RGB to HEX conversion formula: Each channel (0–255) is converted to a 2-digit base-16 number: Hex digit = channel.toString(16).padStart(2, '0') R: decimal → 2-digit hex (uppercase) G: decimal → 2-digit hex (uppercase) B: decimal → 2-digit hex (uppercase) Result: '#' + RR + GG + BB Examples: (59, 130, 246): R: 59 → 3B G: 130 → 82 B: 246 → F6 Result: #3B82F6 (255, 255, 255) → #FFFFFF (white) (0, 0, 0) → #000000 (black) (128, 128, 128) → #808080 (medium gray) Decimal to Hex conversion: 0-9 maps directly; 10=A, 11=B, 12=C, 13=D, 14=E, 15=F For values 16+: divide by 16, first digit = quotient, second = remainder 130 ÷ 16 = 8 remainder 2 → 82
Frequently Asked Questions
Why does the hex code always have exactly 6 characters?▼
What RGB value represents white, black, and gray?▼
How many unique colors can RGB represent?▼
Can I use RGB values in CSS directly instead of converting to hex?▼
Why do some hex codes appear to have repeated characters like #AABBCC?▼
Related Tools
Explore other tools in this category.
HEX to RGB Converter
Convert hexadecimal web color codes to RGB system values instantly.
Color Picker
Select dynamic colors and extract both HEX and RGB strings simultaneously.
CSS Gradient Generator
Create beautiful linear and radial CSS gradients with a live visual editor and instant code output.
Glassmorphism Generator
Generate the popular frosted glass CSS effect with blur, transparency, and border controls.
Color Palette Generator
Generate harmonious color palettes from any base color using color theory rules.