Skip to content

Instantly share code, notes, and snippets.

@funnylookinhat
Last active December 28, 2015 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funnylookinhat/7448893 to your computer and use it in GitHub Desktop.
Save funnylookinhat/7448893 to your computer and use it in GitHub Desktop.
RGBA Convert: Using RGBA pixels from images to create a wide array of heightmap values.
/**
* RGBA Convert
* Converting RGBA values to/from heightmap values ( i.e. signed "3 decimal" numbers )
* Expected range +/- 999999.999
*/
function convertToRGBA(value) {
value = parseInt(1000 * (parseFloat(value).toFixed(3)));
var a = value & 255; value = value >>> 8;
var b = value & 255; value = value >>> 8;
var g = value & 255; value = value >>> 8;
var r = value & 255; value = value >>> 8;
return {
r: r,
g: g,
b: b,
a: a
}
}
function convertToFloat(rgba) {
var value = 0 >>> 32;
value += rgba.r; value = value << 8;
value += rgba.g; value = value << 8;
value += rgba.b; value = value << 8;
value += rgba.a;
return value / 1000;
}
@funnylookinhat
Copy link
Author

Adding console.log('SHIFTED : '+value.toString(2)); to the end of each operation line can be a fun lesson in bitwise operators. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment