Master Chief
Master Chief

Reputation: 75

Float RGB values vs ubyte RGB values for colour

(ubyte being unsigned char)

What is the point of using float values for RGB values in for instance a colour class? Does it offer more accuracy? What about space? What are the performance benefits/hindrances of using float vs ubyte?

Upvotes: 4

Views: 1401

Answers (2)

Mats Petersson
Mats Petersson

Reputation: 129524

In all likelyhood, it does offer more precision. As mentioned in the other answer, there are also benefits in not having to convert back and forth, which in turn has a consequence on loosing intermediate results - imagine that you have a pixel that is 1.0 in float (255 in a byte), and you multiply by 0.65. Then convert it back to integer, that's 166.75 - but we round down to 166. So we lose the 0.75. If we do some further math, the error may get bigger... And of course, if some intermediate step causes our value to go (way) beyond 255 (1.0) or below 0 (0.0), it's perfectly possible to retain the new value for some time, and then "fix it up" later if needed.

Of course, you can't store more data in less space (generally), so a typical float is 4 times bigger than a byte. Some GPU's have 16-bit floats, which hold 11 bits of mantissa and 4-5 bits of exponent, which makes them good for most kinds of simple pixel math. But it's still double the size, but not double the "preciseness" (a colleague of mine spent a lot of time trying to get an algorithm using 16-bit floats to match the results of 32-bit floats, and never quite got there).

Upvotes: 3

Paul R
Paul R

Reputation: 213220

Pro:

  • doesn't need to be converted to/from float when doing math
  • preserves intermediate values much more accurately when applying a number of processing steps sequentially
  • has much greater dynamic range and resolution
  • float is natural format for GPU etc

Con:

  • requires more storage

Upvotes: 4

Related Questions