0
$\begingroup$

Rosanswers logo

I found an example in the codebase that uses some messy bit-twiddling to get the individual r,g,b fields out of a packed rgb float. Is there a nice utility function for doing this?

(I'm trying to avoid unnecessary copy-pasting)


Originally posted by mattbell on ROS Answers with karma: 11 on 2011-02-18

Post score: 1


Original comments

Comment by mattbell on 2011-02-27:
Here's what I ended up doing: uint32_t rgb_val_; memcpy(&rgb_val_, &(cloud_rgb->points[i].rgb), sizeof(float));

    uint8_t r_ = (uint8_t)((rgb_val_ >> 16) & 0x000000ff); 
    uint8_t g_ = (uint8_t)((rgb_val_ >> 8) & 0x000000ff);
    uint8_t b_ = (uint8_t)((rgb_val_) & 0x000000ff);
$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

The sample code on http://www.ros.org/wiki/rviz/DisplayTypes/PointCloud provides two ways to do the reverse. Shouldn't be too hard to flip it around.

In C++,

int rgb = 0xff0000; 
float float_rgb = *reinterpret_cast(&rgb);

In Python,

float_rgb = struct.unpack('f', struct.pack('i', 0xff0000))[0]

Originally posted by David Lu with karma: 10932 on 2011-02-19

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

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