New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

int-bits

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

int-bits

converts int bits to float and back

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

int-bits

stable

Converts int bits to a float32 and back. Useful for packing and unpacking RGBA bits into a single float (e.g. for interleaving float data in WebGL). This technique only works on browsers that implement typed arrays correctly.

var pack = require('int-bits')

//gets a single ABGR representation of 4 color bytes
var bits = (a << 24 | b << 16 | g << 8 | r)

//pack to a float value for use with a Float32Array
var rgba = pack(bits & 0xfeffffff)

You can also unpack a float32 to int bits. Since there is some precision loss in the high bits, it's best to bias the alpha channel toward 0 and 255.

var unpack = require('int-bits').unpack

var bits = unpack(rgba)
var A = (bits & 0xff000000) >>> 24
var B = (bits & 0x00ff0000) >>> 16
var G = (bits & 0x0000ff00) >>> 8
var R = (bits & 0x000000ff)

//fix precision loss with high bits
A = Math.floor(A*(255/254))

Usage

NPM

f = require('int-bits')(bits)

f = require('int-bits').pack(bits)

Converts the int bits into a float32 value using typed arrays.

i = require('int-bits').unpack(f)

Converts the float32 value back to int bits using typed arrays.

License

MIT, see LICENSE.md for details.

Keywords

int

FAQs

Package last updated on 22 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts