Skip to content

Instantly share code, notes, and snippets.

@ishan9299
Last active Dec 26, 2021
Embed
What would you like to do?
Convert hex to 8 bit colors.
const table = [
'073642',
'dc322f',
'859900',
'b58900',
'268bd2',
'd33682',
'2aa198',
'eee8d5',
'002b36',
'cb4b16',
'586e75',
'657b83',
'839496',
'6c71c4',
'93a1a1',
'fdf6e3',
'fdf6e3'
]
// color = (r*6/256)*36 + (g*6/256)*6 + (b*6/256)
function convert256(item) {
let str = item;
let redHex = str.substring(0, 2);
let greenHex = str.substring(2, 4);
let blueHex = str.substring(4);
let red = parseInt(redHex, 16);
let green = parseInt(greenHex, 16);
let blue = parseInt(blueHex, 16);
let color = 16 + Math.floor(red*6/256)*36 + Math.floor(green*6/256)*6 + Math.floor(blue*6/256)
console.log(Math.floor(color));
}
table.forEach(convert256);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment