<div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">--- Ton Roosendaal <<a href="mailto:ton@blender.org">ton@blender.org</a>> wrote:<br>
<br>> Revision: 11919<br>><br>><br><a href="http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11919">http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=11919
</a><br>> Author: ton<br>> Date: 2007-09-02 18:34:02 +0200 (Sun, 02 Sep<br>> 2007)<br>><br>> Log Message:<br>> -----------<br>> Bugfix #7125<br>><br>> The conversion from float to char (byte) was not
<br>> correct. It should include<br>> the possibility for colors like 0.999999 to become<br>> 255 still. The correct<br>> multiplication factor I don't know though... and<br>> this should become a system
<br>> wide definition!<br>></blockquote><br><div><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">From: Martin Poirier <<a href="mailto:theeth@yahoo.com">
theeth@yahoo.com</a>><br>You can probably use that too:<br>
<br>#define FTOCHAR(val) val<=0.0f?0: (val>(1.0f - 0.5f /<br>255.0f)?255: (char)(255.0f*val))<br><br>Multiplying by 255.99 will give values 1 higher than<br>they should be for float values > 0.5. (I dealt with a
<br>similar issue at work, that's more or less what we<br>used).<br><br>Martin</blockquote><div><br>... <br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
That was assuming rounding, of course, in this case,<br>it's truncating on cast, so you can use:<br><br>#define FTOCHAR(val) val<=0.0f?0: (val>(1.0f - 1.0f /<br>255.0f)?255: (char)(255.0f*val))<br><br>The 0.5 was to make it round in the correct direction.
<br><br>Martin</blockquote><div><br>Isn't the cast truncate bug a problem over the whole range of values? Not only is it important to round up anything 254.5 (1.0f - 0.5f /255.0f) or higher to 255, but you need to address for instance, a value like
0.49f which would work out to 124.95 and get truncated to 124. <br></div></div>I suggest using this:<br>
<br>
#define FTOCHAR(val) val<=0.0f? 0 : (val>(1.0f - 0.5f /255.0f)? 255 : (char)((255.0f*val)+0.5f))<br>
<br>
Adding .5 allows val to properly round up when the char cast truncates
the float. It also has the advantage of working over the entire range
of values and not just for 255. Here, checking for 254.5 (1.0f - 0.5f /255.0f) instead of 1.0f is just more efficient as any value higher will round up to 255 anyway...<br><br>
I found this tip a long time ago on the web when I needed a set of
conversion functions for my seq plugins...and it has worked well there.<br>
<br>
rob