BIP39 Seed Word to Color Mapping
A deterministic algorithm that maps BIP39 seed word indices (0-2047) to visually distinct hex color codes.
Algorithm Overview
This function generates 2048 unique colors optimized for maximum visual distinction between sequential indices. It's particularly useful for visual verification of seed words in cryptocurrency wallets.
function indexToRgb(n) {
// Use golden ratio conjugate for optimal hue spacing
const i = ((n % 2048) + 2048) % 2048;
const hue = (i * 0.6180339887498949) % 1; // fractional part only
// Alternate high/low saturation & brightness to maximize contrast
const parity = i & 1;
const saturation = parity ? 0.95 : 0.65; // high or medium
const brightness = parity ? 0.85 : 0.98; // slightly darker or very bright
const h = hue * 360;
const s = saturation;
const v = brightness;
const c = v * s;
const hPrime = h / 60;
const x = c * (1 - Math.abs((hPrime % 2) - 1));
let r, g, b;
if (hPrime < 1) { r = c; g = x; b = 0; }
else if (hPrime < 2) { r = x; g = c; b = 0; }
else if (hPrime < 3) { r = 0; g = c; b = x; }
else if (hPrime < 4) { r = 0; g = x; b = c; }
else if (hPrime < 5) { r = x; g = 0; b = c; }
else { r = c; g = 0; b = x; }
const m = v - c;
r = Math.round((r + m) * 255);
g = Math.round((g + m) * 255);
b = Math.round((b + m) * 255);
const hex = '#' + [r,g,b].map(x => x.toString(16).padStart(2,'0')).join('').toUpperCase();
const lum = r*0.299 + g*0.587 + b*0.114;
return { hex, isDark: lum < 140 };
} How It Works
1. Index Normalization
const i = ((n % 2048) + 2048) % 2048; Ensures the input wraps correctly for negative numbers and values outside 0-2047 range.
2. Golden Ratio Hue Distribution
const hue = (i * 0.6180339887498949) % 1; Uses the golden ratio conjugate (φ - 1 ≈ 0.618) to distribute hues across the color wheel. This mathematical constant provides optimal spacing because consecutive indices land on opposite sides of the color spectrum, maximizing perceptual difference.
3. Alternating Saturation & Brightness
const parity = i & 1;
const saturation = parity ? 0.95 : 0.65;
const brightness = parity ? 0.85 : 0.98;
Even/odd indices alternate between:
- Odd indices: High saturation (95%), medium brightness (85%) → vivid, slightly darker colors
- Even indices: Medium saturation (65%), high brightness (98%) → softer, lighter colors
This ensures adjacent indices differ in both hue AND intensity, enhancing distinguishability.
4. HSV to RGB Conversion
Standard HSV→RGB conversion algorithm producing 0-255 RGB values.
5. Luminance Calculation
const lum = r*0.299 + g*0.587 + b*0.114;
return { hex, isDark: lum < 140 }; Calculates perceived brightness using standard luminance weights. The isDark flag (threshold: 140) helps determine appropriate text color for overlays.
Properties
- Deterministic: Same index always produces same color
- Complete coverage: Utilizes full color spectrum
- Maximum distinction: Adjacent indices are visually different in both hue and intensity
- Accessibility-aware: Includes luminance calculation for contrast considerations
Example Output
indexToRgb(0) // { hex: '#FAFAFA', isDark: false }
indexToRgb(1) // { hex: '#F24F00', isDark: true }
indexToRgb(2) // { hex: '#FAFAE5', isDark: false }
indexToRgb(100) // { hex: '#F20057', isDark: true } Use Cases
- Visual verification of BIP39 seed words
- Color-coding cryptocurrency addresses
- Creating visual mnemonics for word lists
- Any application requiring 2048 distinct, memorable colors
License
MIT License
Copyright 2025 nakamoto.be
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Visit nakamoto.be for additional Bitcoin calculators and resources to help you make informed decisions about your cryptocurrency investments.