Skip to content

Instantly share code, notes, and snippets.

@wirepair
Created November 30, 2017 05:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wirepair/b831cf168882c7013b68c1703bda5250 to your computer and use it in GitHub Desktop.
Save wirepair/b831cf168882c7013b68c1703bda5250 to your computer and use it in GitHub Desktop.
convert 24 bit to 16 bit and vice versa via an html page
<!DOCTYPE html>
<html>
<head>
<title>24bit 16bit color converter</title>
<script>
window.addEventListener('load', function()
{
let i24 = document.getElementById('in24');
let i16 = document.getElementById('in16');
i24.addEventListener('change', function ()
{
let ret = "";
try
{
ret = convert24to16(i24.value);
}
catch (e)
{
ret = e;
}
document.getElementById("in16").value = ret;
});
i16.addEventListener('change', function()
{
let ret = "";
try
{
ret = convert16to24(i16.value);
}
catch (e)
{
ret = e;
}
document.getElementById("in24").value = ret;
});
});
function convert24to16(input)
{
let RGB888 = parseInt(input.replace(/^#/, ''), 16);
let r = (RGB888 & 0xFF0000) >> 16;
let g = (RGB888 & 0xFF00) >> 8;
let b = RGB888 & 0xFF;
r = (r * 249 + 1014) >> 11;
g = (g * 253 + 505) >> 10;
b = (b * 249 + 1014) >> 11;
let RGB565 = 0;
RGB565 = RGB565 | (r << 11);
RGB565 = RGB565 | (g << 5);
RGB565 = RGB565 | b;
return "0x"+RGB565.toString(16);
}
function convert16to24(input)
{
let RGB565 = parseInt(input.replace(/^#/, ''), 16);
let r = (RGB565 & 0xF800) >> 11;
let g = (RGB565 & 0x07E0) >> 5;
let b = RGB565 & 0x1F;
r = (r * 527 + 23) >> 6;
g = (g * 259 + 33) >> 6;
b = (b * 527 + 23) >> 6;
let RGB888 = 0;
RGB888 = RGB888 | (r << 16);
RGB888 = RGB888 | (g << 8);
RGB888 = RGB888 | (b);
return "0x"+RGB888.toString(16);
}
</script>
</head>
<body>
<div>conversion for 24 bit and 16 bit colors, converted (C# to JS) from <a href="https://github.com/dynphys/rgb-converter">https://github.com/dynphys/rgb-converter</a> by idawson.</div>
<label for="in24">24-bit: </label><input type="text" id="in24"/>
<br>
<label for="in16">16-bit: </label><input type="text" id="in16"/>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment