Skip to content

Commit 735705e

Browse files
author
Kevin Townsend
committed
Added interim over/underflow protection
1 parent f82f166 commit 735705e

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

adafruit_tcs34725.py

+10
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,20 @@ def color_rgb_bytes(self):
236236
red, green, blue component values as bytes (0-255).
237237
"""
238238
r, g, b, clear = self.color_raw
239+
# Avoid divide by zero errors ... if clear = 0 return black
240+
if clear == 0:
241+
return (0, 0, 0)
239242
# pylint: disable=bad-whitespace
240243
red = int(pow((int((r/clear) * 256) / 255), 2.5) * 255)
241244
green = int(pow((int((g/clear) * 256) / 255), 2.5) * 255)
242245
blue = int(pow((int((b/clear) * 256) / 255), 2.5) * 255)
246+
# Handle possible 8-bit overflow
247+
if red > 255:
248+
red = 255
249+
if green > 255:
250+
green = 255
251+
if blue > 255:
252+
blue = 255
243253
return (red, green, blue)
244254

245255
@property

0 commit comments

Comments
 (0)