Skip to content

Instantly share code, notes, and snippets.

@MightyPork
Last active October 4, 2023 08:55
Show Gist options
  • Save MightyPork/1d9bd3a3fd4eb1a661011560f6921b5b to your computer and use it in GitHub Desktop.
Save MightyPork/1d9bd3a3fd4eb1a661011560f6921b5b to your computer and use it in GitHub Desktop.
Convert ANSI color 0-255 to RGB
const low_rgb = [
'#000000', '#800000', '#008000', '#808000', '#000080', '#800080', '#008080', '#c0c0c0',
'#808080', '#ff0000', '#00ff00', '#ffff00', '#0000ff', '#ff00ff', '#00ffff', '#ffffff'
]
function ansi_rgb(ansi) {
if (ansi < 0 || ansi > 255) return '#000'
if (ansi < 16) return low_rgb[ansi]
if (ansi > 231) {
const s = (ansi - 232) * 10 + 8
return `rgb(${s},${s},${s})`
}
const n = ansi - 16
let b = n % 6
let g = (n - b) / 6 % 6
let r = (n - b - g * 6) / 36 % 6
b = b ? b * 40 + 55 : 0
r = r ? r * 40 + 55 : 0
g = g ? g * 40 + 55 : 0
return `rgb(${r},${g},${b})`
}
@MightyPork
Copy link
Author

low_rgb can change with terminal color theme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment