"Colour mapping" - help needed to create RGB value

Here is an adaption of that code.

As you didnt specify how you want RGB to be represented, I just spit out a whole bunch of different values (r,g,b,val,hex) in the payload object.

Ignore the pink image nodes at the bottom - they are to demonstrate it working.

[{"id":"e17b05ed.383be8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"30","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":140,"wires":[["bfca8794.36a6e8"]]},{"id":"a9bd0b3b.93a0b8","type":"jimp-image","z":"f057b3e5.0b042","name":"","data":"payload","dataType":"msg","ret":"img","parameter1":"","parameter1Type":"msg","parameter2":"","parameter2Type":"msg","parameter3":"","parameter3Type":"msg","parameter4":"","parameter4Type":"msg","parameter5":"","parameter5Type":"msg","parameter6":"","parameter6Type":"msg","parameter7":"","parameter7Type":"msg","parameter8":"","parameter8Type":"msg","sendProperty":"image","sendPropertyType":"msg","parameterCount":0,"jimpFunction":"none","selectedJimpFunction":{"name":"none","fn":"none","description":"Just loads the image.","parameters":[]},"x":410,"y":340,"wires":[["e7b89dbf.316d7"]]},{"id":"e7b89dbf.316d7","type":"image viewer","z":"f057b3e5.0b042","name":"","width":"60","data":"image","dataType":"msg","x":530,"y":340,"wires":[[]]},{"id":"bfca8794.36a6e8","type":"change","z":"f057b3e5.0b042","name":"set min30 max75","rules":[{"t":"set","p":"min","pt":"msg","to":"30","tot":"num"},{"t":"set","p":"max","pt":"msg","to":"75","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":200,"wires":[["177d1506.6c226b"]]},{"id":"177d1506.6c226b","type":"function","z":"f057b3e5.0b042","name":"colorise","func":"// debugger\nvar buf = Buffer.from([0,0,0,255]);\nconst minimum = msg.min; //-10w0.0;\nconst maximum = msg.max; //400.0;\nconst val = msg.payload;\n\nmsg.payload = celcius2colours(val);\n\nreturn msg;\n\nfunction celcius2colours(value) {\n    var ratio = 2 * (value-minimum) / (maximum - minimum);\n    b = parseInt(Math.min(Math.max(0, 255*(1 - ratio)),255));\n    r = parseInt(Math.min(Math.max(0, 255*(ratio - 1)),255));\n    g = 255 - b - r;\n    let val = rgbaToInt(r,g,b,255);\n    let hex = val.toString(16);\n    let pl = {\n        r: r, g: g, b: b,\n        val: val,\n        hex: hex,\n        hex2: \"#\" + hex\n    }\n    return pl;\n}\n\n\nfunction rgbaToInt(red,green,blue,alpha){\n    \n    var r = red & 0xFF;\n    var g = green & 0xFF;\n    var b = blue & 0xFF;\n    var a = alpha & 0xFF;\n    \n    buf[0] = r;\n    buf[1] = g;\n    buf[2] = b;\n    return buf.readUInt32BE(0);\n    //return (r << 16) + (g << 8) + (b);\n    //return (r << 24 >>> 0) | (g << 16) | (b << 8) | (a);\n}\n\n\n\n\n\n\n","outputs":1,"noerr":0,"x":540,"y":200,"wires":[["e186474b.006938","f2a80a92.dce3d8"]]},{"id":"f5cf600c.fbc6c","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"40","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":180,"wires":[["bfca8794.36a6e8"]]},{"id":"e186474b.006938","type":"function","z":"f057b3e5.0b042","name":"make image with color","func":"\n\nconst colour = msg.payload.val;\nmsg.payload = {\"w\":8,\"h\":8,\"background\":colour}\nreturn msg;\n\n\n\n\n\n\n\n","outputs":1,"noerr":0,"x":440,"y":300,"wires":[["a9bd0b3b.93a0b8","b0784059.824d7"]]},{"id":"a4f2651f.394388","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"50","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":220,"wires":[["bfca8794.36a6e8"]]},{"id":"3a476896.7570e8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"60","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":260,"wires":[["bfca8794.36a6e8"]]},{"id":"dc2d6e74.eea54","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"70","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":300,"wires":[["bfca8794.36a6e8"]]},{"id":"f2a80a92.dce3d8","type":"debug","z":"f057b3e5.0b042","name":"payload with various colour values","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":800,"y":200,"wires":[]},{"id":"b0784059.824d7","type":"debug","z":"f057b3e5.0b042","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":670,"y":300,"wires":[]},{"id":"d2dc56f9.2db4b8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":80,"wires":[["bfca8794.36a6e8"]]},{"id":"a28fbab0.7351e8","type":"inject","z":"f057b3e5.0b042","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":360,"wires":[["bfca8794.36a6e8"]]},{"id":"b00d8b01.f51fb8","type":"comment","z":"f057b3e5.0b042","name":"test data","info":"","x":180,"y":40,"wires":[]},{"id":"7ab74bb8.209b34","type":"comment","z":"f057b3e5.0b042","name":"for demo purposes only","info":"","x":440,"y":260,"wires":[]},{"id":"135c67c6.9c4508","type":"comment","z":"f057b3e5.0b042","name":"----------------- the important bit ----------------- ","info":"","x":440,"y":160,"wires":[]}]
1 Like