diff --git a/adafruit_tcs34725.py b/adafruit_tcs34725.py index e4e379f..d0b5f68 100644 --- a/adafruit_tcs34725.py +++ b/adafruit_tcs34725.py @@ -134,12 +134,18 @@ def color_rgb_bytes(self): red, green, blue component values as bytes (0-255). """ r, g, b, clear = self.color_raw + # Avoid divide by zero errors ... if clear = 0 return black if clear == 0: return (0, 0, 0) + + # Each color value is normalized to clear, to obtain int values between 0 and 255. + # A gamma correction of 2.5 is applied to each value as well, first dividing by 255, + # since gamma is applied to values between 0 and 1 red = int(pow((int((r / clear) * 256) / 255), 2.5) * 255) green = int(pow((int((g / clear) * 256) / 255), 2.5) * 255) blue = int(pow((int((b / clear) * 256) / 255), 2.5) * 255) + # Handle possible 8-bit overflow if red > 255: red = 255 diff --git a/examples/tcs34725_simpletest.py b/examples/tcs34725_simpletest.py index 3823499..7a3059d 100644 --- a/examples/tcs34725_simpletest.py +++ b/examples/tcs34725_simpletest.py @@ -12,11 +12,28 @@ i2c = board.I2C() # uses board.SCL and board.SDA sensor = adafruit_tcs34725.TCS34725(i2c) +# Change sensor integration time to values between 2.4 and 614.4 milliseconds +# sensor.integration_time = 150 + +# Change sensor gain to 1, 4, 16, or 60 +# sensor.gain = 4 + # Main loop reading color and printing it every second. while True: + # Raw data from the sensor in a 4-tuple of red, green, blue, clear light component values + # print(sensor.color_raw) + + color = sensor.color + color_rgb = sensor.color_rgb_bytes + print( + "RGB color as 8 bits per channel int: #{0:02X} or as 3-tuple: {1}".format( + color, color_rgb + ) + ) + # Read the color temperature and lux of the sensor too. temp = sensor.color_temperature lux = sensor.lux - print("Temperature: {0}K Lux: {1}".format(temp, lux)) + print("Temperature: {0}K Lux: {1}\n".format(temp, lux)) # Delay for a second and repeat. time.sleep(1.0)