简体   繁体   English

将浮点值映射到JS中的十六进制范围?

[英]Map floating point value to hexadecimal range in JS?

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 对于x的最小值0x0000相当于0.00000 ,和0xffff相当于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? 假设我的值为0.6829,如何将其映射到Javascript中的十六进制值0x....

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: 我之所以需要它,是因为我想通过UDP向飞利浦Hue Lights发送消息,有关我的问题的文档如下,请注意,我现在使用的是XY颜色空间而不是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. 支持的色彩空间格式为RGB和xy + Brightness。 Every individual color component has a 16 bit resolution on the API. 每个单独的颜色组件在API上的分辨率均为16位。 RGB values are converted into xy+Brightness by the Hue bridge. 通过色相桥将RGB值转换为xy +亮度。

The x and y values supported by the lamps have a 12-bits resolution, and brightness 11 bits. 灯支持的x和y值具有12位分辨率,亮度为11位。 This means that the 16 bit resolution on the API will be truncated. 这意味着API的16位分辨率将被截断。

To get the best color consistency over various types of lamps (ie gamuts) it is best to send xy+Brightness values, as these are hardware independent. 为了在各种类型的灯(即色域)上获得最佳的颜色一致性,最好发送xy + Brightness值,因为这些值与硬件无关。

For xy, the minimum value of 0x0000 is equivalent to 0.00000, and 0xffff is equivalent to 1.00000 对于xy,最小值0x0000等于0.00000,0xffff等于1.00000

The used color space format is defined in the “Color space” byte part of the message header. 使用的颜色空间格式在消息头的“颜色空间”字节部分中定义。

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. 当然,使用二进制运算符也可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM