Last active
December 28, 2015 05:19
-
-
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.
This file contains 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
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding console.log('SHIFTED : '+value.toString(2)); to the end of each operation line can be a fun lesson in bitwise operators. :)