Last active
December 26, 2021 01:54
-
-
Save ishan9299/d87713b43dc04d49fa060711fdc7dd6d to your computer and use it in GitHub Desktop.
Convert hex to 8 bit colors.
This file contains hidden or 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 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