Skip to content

Instantly share code, notes, and snippets.

@ishan9299
Last active December 26, 2021 01:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishan9299/d87713b43dc04d49fa060711fdc7dd6d to your computer and use it in GitHub Desktop.
Save ishan9299/d87713b43dc04d49fa060711fdc7dd6d to your computer and use it in GitHub Desktop.
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