Convert RGB as a float into unsigned chars

  • Thread starter Adam Teasdale Hartshorne
  • Start date
A

Adam Teasdale Hartshorne

I need to convert a colour defined as floats of R,G and B channels into
the same R,G and B channels but defined by unsigned char's. Any help?

Adam
 
M

mlimber

Adam said:
I need to convert a colour defined as floats of R,G and B channels into
the same R,G and B channels but defined by unsigned char's. Any help?

Adam

Presumably the values are floats because they're in the range [0..1] or
something like that. If so, just scale it up:

#include <limits>

// ...
typedef unsigned char uchar;
const uchar r = uchar( std::numeric_limits<uchar>::max * r_float );
// etc.

Cheers! --M
 
G

Greg

mlimber said:
Adam said:
I need to convert a colour defined as floats of R,G and B channels into
the same R,G and B channels but defined by unsigned char's. Any help?

Adam

Presumably the values are floats because they're in the range [0..1] or
something like that. If so, just scale it up:

#include <limits>

// ...
typedef unsigned char uchar;
const uchar r = uchar( std::numeric_limits<uchar>::max * r_float );
// etc.

Cheers! --M

Lastly, I would just verify that the maximum and minimum values (white
and black) in the floating point representation scale to to
{255,255,255} and {0,0,0} exactly.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top