views:

54

answers:

1

i would like to implement 'drawing modes' (in my own graphics library).

That is drawing with AND, OR, etc However i am storing colors using floats, each channel between 0 and 1.0 Do i have to first convert each color channel to 0-255 before i can use the AND, OR, etc drawing modes? and then convert back to float (0.0-1.0) ?

Or is there another way of doing it?

thanks

+2  A: 

I believe the question is not clear enough. AND, OR, etc. are boolean operators. Many languages support bitwise versions as well. So, you first need to define what exactly is the meaning of AND-ing or OR-ing two color values. What is Red AND Green? Is it Black?

If the answer to the above question is positive, then you probably want to apply these operators in the bitwise sense on the (integer) RGB representation of your colors. In this case, you do need to: 1. Convert the floats to (8-bit or other resolution) integers 2. Pack the 3 channels (or 4 with Alpha) into one word (probably 32-bit integer) 3. Apply the bitwise operator 4. Unpack the channels and convert back to floats.

Note that in conversion of floats to int you first need to multiply your float value by MAX_COLOR (255 in your example), and then cast. Otherwise, you end up with all channels being 0. Opposite when you convert back to floats, first cast then divide by MAX_COLOR to normalize your values.

ysap