Color Conversion
RGB Mapping
Integer to RGB
Algorithm Design
Data Representation

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 00 and 255255. 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 (R,G,B)(R, G, B) where each component occupies one byte (8 bits). Together, the three components form a 24-bit value capable of representing 224=16,777,2162^{24} = 16{,}777{,}216 distinct colors. Any integer can be mapped into this space by reducing it modulo 2242^{24}.

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

  1. Reduce to 24-bit range: Compute n=nmod224n' = n \bmod 2^{24} (equivalently, n' = n & 0xFFFFFF).
  2. Extract Red: R=(n16)&255R = (n' \gg 16) \mathbin{\&} 255
  3. Extract Green: G=(n8)&255G = (n' \gg 8) \mathbin{\&} 255
  4. Extract Blue: B=n&255B = n' \mathbin{\&} 255

Example

Convert the integer 123456123456 to RGB:

  1. 123456123456 in hexadecimal is 0x01E240
  2. R=(0x01E24016)&255=1R = (0\text{x}01\text{E}240 \gg 16) \mathbin{\&} 255 = 1
  3. G=(0x01E2408)&255=226G = (0\text{x}01\text{E}240 \gg 8) \mathbin{\&} 255 = 226
  4. B=0x01E240&255=64B = 0\text{x}01\text{E}240 \mathbin{\&} 255 = 64
  5. Result: RGB(1, 226, 64)

Code Example

python
1def int_to_rgb(n: int) -> tuple[int, int, int]:
2    n = n % (256 ** 3)  # reduce to 24-bit range
3    r = (n >> 16) & 0xFF
4    g = (n >> 8) & 0xFF
5    b = n & 0xFF
6    return (r, g, b)
7
8print(int_to_rgb(123456))  # (1, 226, 64)

Method 2: Modular Arithmetic

An alternative approach uses division and modulo to distribute the integer across three components sequentially.

Algorithm

  1. Compute B=nmod256B = n \bmod 256
  2. Compute G=n/256mod256G = \lfloor n / 256 \rfloor \bmod 256
  3. Compute R=n/65536mod256R = \lfloor n / 65536 \rfloor \bmod 256

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 22222222 to RGB:

  1. B=2222mod256=174B = 2222 \bmod 256 = 174
  2. G=2222/256mod256=8mod256=8G = \lfloor 2222 / 256 \rfloor \bmod 256 = 8 \bmod 256 = 8
  3. R=2222/65536mod256=0R = \lfloor 2222 / 65536 \rfloor \bmod 256 = 0
  4. Result: RGB(0, 8, 174)

Summary Table

StepBit ExtractionModular Arithmetic
Reduce rangen&0xFFFFFFn \mathbin{\&} \text{0xFFFFFF}nmod224n \bmod 2^{24}
Red(n16)&255(n \gg 16) \mathbin{\&} 255n/65536mod256\lfloor n / 65536 \rfloor \bmod 256
Green(n8)&255(n \gg 8) \mathbin{\&} 255n/256mod256\lfloor n / 256 \rfloor \bmod 256
Bluen&255n \mathbin{\&} 255nmod256n \bmod 256

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 (R,G,B)(R, G, B), you can reconstruct the original reduced integer as R65536+G256+BR \cdot 65536 + G \cdot 256 + B.
  • 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.


Course illustration
Course illustration

All Rights Reserved.