1

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.

9
  • Should 0xffff be the representation of exactly 1.0, or of 0.99998? Commented Jun 24, 2018 at 18:22
  • It should be 1.0 exactly! Commented Jun 24, 2018 at 18:22
  • 0xffff is 65535, so 0.6829 * 65535 is 44753.8515, rounded: 44753, which is 0xAED1. Commented Jun 24, 2018 at 18:24
  • @ChrisG Care to elaborate? Why is 0xffff 65535 for example? Commented Jun 24, 2018 at 18:28
  • That's just how hex numbers work. Didn't you look up the conversion? Anyway, JS understands hex, for instance alert(0xffff * 0.6829) works fine and displays 44753.8515. What do you need the end result for? If this is just about the number, the format doesn't matter since its the same amount, regardless of being expressed in hex or dec. Commented Jun 24, 2018 at 18:33

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Why not secondByte = hexVal & 255 and firstByte = hexVal >> 8 though? Dividing by 255 instead of 256 is extra odd
@harold Right, fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.