Reputation: 1523
Let's say we have the following:
For x, the minimum value of 0x0000
is equivalent to 0.00000
, and 0xffff
is equivalent to 1.00000
Let's say I have a value of 0.6829, how would I map this to the hexadecimal value of 0x....
in Javascript?
EDIT:
The reason I need this is because I want to send a message to my Philips Hue Lights over UDP, the relevant documentation for my question is as follows, note, I am now using the XY color space instead of RGB:
Example of a stream message:
Example
{
'H', 'u', 'e', 'S', 't', 'r', 'e', 'a', 'm', //protocol
0x01, 0x00, //version 1.0
0x07, //sequence number 7
0x00, 0x00, //Reserved write 0’s
0x00, //color mode RGB
0x00, // Reserved, write 0’s
0x00, 0x00, 0x01, //light ID 1
0xff, 0xff, 0x00, 0x00, 0x00, 0x00, //red
0x00, 0x00, 0x02, //light ID 2
0x00, 0x00, 0x00, 0x00, 0xff, 0xff //blue
}
The color space formats supported are RGB and xy+Brightness. Every individual color component has a 16 bit resolution on the API. RGB values are converted into xy+Brightness by the Hue bridge.
The x and y values supported by the lamps have a 12-bits resolution, and brightness 11 bits. This means that the 16 bit resolution on the API will be truncated.
To get the best color consistency over various types of lamps (i.e. gamuts) it is best to send xy+Brightness values, as these are hardware independent.
For xy, the minimum value of 0x0000 is equivalent to 0.00000, and 0xffff is equivalent to 1.00000
The used color space format is defined in the “Color space” byte part of the message header.
Upvotes: 1
Views: 351
Reputation:
Looks like all you need is to separate the final number into two bytes:
function getBytes(val) {
const hexVal = Math.round(val * 0xffff);
const secondByte = hexVal % 0x100;
const firstByte = (hexVal - secondByte) / 0x100;
return [firstByte, secondByte];
}
console.log(getBytes(0.6829));
Edit: thanks, fixed. Using binary operators will work as well of course.
Upvotes: 2