Last active
June 4, 2024 19:11
-
-
Save MightyPork/1d9bd3a3fd4eb1a661011560f6921b5b to your computer and use it in GitHub Desktop.
Convert ANSI color 0-255 to RGB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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})` | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
low_rgb can change with terminal color theme