Algorithm to convert any positive integer to an RGB value
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a positive integer to an RGB value involves mapping a number that potentially has an infinite range into a three-dimensional color space with finite limits. RGB values are the standard for digital color representation, where each component (Red, Green, Blue) takes an integer value between and . This article explores two practical algorithms for this conversion: direct bit extraction and modular arithmetic.
Understanding the RGB Color Space
An RGB color is a triplet where each component occupies one byte (8 bits). Together, the three components form a 24-bit value capable of representing distinct colors. Any integer can be mapped into this space by reducing it modulo .
Method 1: Direct Bit Extraction
The most common and efficient approach treats the integer as a 24-bit value and extracts each 8-bit component using bitwise operations.
Algorithm
- Reduce to 24-bit range: Compute (equivalently,
n' = n & 0xFFFFFF). - Extract Red:
- Extract Green:
- Extract Blue:
Example
Convert the integer to RGB:
- in hexadecimal is
0x01E240 - Result:
RGB(1, 226, 64)
Code Example
Method 2: Modular Arithmetic
An alternative approach uses division and modulo to distribute the integer across three components sequentially.
Algorithm
- Compute
- Compute
- Compute
This is mathematically equivalent to Method 1 but expressed without bitwise operators, making it useful in languages or contexts where bit manipulation is awkward.
Example
Convert the integer to RGB:
- Result:
RGB(0, 8, 174)
Summary Table
| Step | Bit Extraction | Modular Arithmetic |
| Reduce range | ||
| Red | ||
| Green | ||
| Blue |
Handling Large Numbers
For very large integers, both methods work correctly because the modulo or bitwise AND operation reduces the value into the 24-bit range before extraction. The mapping is deterministic: the same integer always produces the same color.
Considerations
- Perceptual uniformity: These mappings are mathematically clean but do not produce perceptually uniform color distributions. Nearby integers may map to visually similar or very different colors depending on which bits change. For data visualization, consider using a perceptually uniform colormap (like viridis or inferno) instead.
- Reversibility: The mapping is reversible within the 24-bit range. Given , you can reconstruct the original reduced integer as .
- Negative integers: If your input can be negative, take the absolute value first or use unsigned modular arithmetic.
Conclusion
Mapping integers to RGB values using bit extraction is the standard approach, offering both clarity and performance. The modular arithmetic variant is equivalent and works in any language. Both methods provide a deterministic, reversible translation from numerical data into the RGB color space, useful for data visualization, generative art, and procedural color assignment.

