<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 &lt;<a href="mailto:ton@blender.org">ton@blender.org</a>&gt; wrote:<br>
<br>&gt; Revision: 11919<br>&gt;<br>&gt;<br><a href="http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&amp;root=bf-blender&amp;revision=11919">http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&amp;root=bf-blender&amp;revision=11919
</a><br>&gt; Author:&nbsp;&nbsp; ton<br>&gt; Date:&nbsp;&nbsp;&nbsp;&nbsp; 2007-09-02 18:34:02 +0200 (Sun, 02 Sep<br>&gt; 2007)<br>&gt;<br>&gt; Log Message:<br>&gt; -----------<br>&gt; Bugfix #7125<br>&gt;<br>&gt; The conversion from float to char (byte) was not
<br>&gt; correct. It should include<br>&gt; the possibility for colors like 0.999999 to become<br>&gt; 255 still. The correct<br>&gt; multiplication factor I don&#39;t know though... and<br>&gt; this should become a system
<br>&gt; wide definition!<br>&gt;</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 &lt;<a href="mailto:theeth@yahoo.com">
theeth@yahoo.com</a>&gt;<br>You can probably use that too:<br>
<br>#define FTOCHAR(val) val&lt;=0.0f?0: (val&gt;(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 &gt; 0.5. (I dealt with a
<br>similar issue at work, that&#39;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&#39;s truncating on cast, so you can use:<br><br>#define FTOCHAR(val) val&lt;=0.0f?0: (val&gt;(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&#39;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&lt;=0.0f? 0 : (val&gt;(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.&nbsp; It also has the advantage of working over the entire range
of values and not just for 255.&nbsp; 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