Avoid undefined out-of-range conversions from double to unsigned char

Change-Id: I7cf4af81d477865aa0a93c8aa071a8785677c572