Skip to content

Commit 1240746

Browse files
Merge pull request #37 from silviaCC/simpletest_and_color_rgb_update
Update to the test file of the TCS34725 color sensor
2 parents 239f5ba + 6f4fb3a commit 1240746

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

adafruit_tcs34725.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,18 @@ def color_rgb_bytes(self):
134134
red, green, blue component values as bytes (0-255).
135135
"""
136136
r, g, b, clear = self.color_raw
137+
137138
# Avoid divide by zero errors ... if clear = 0 return black
138139
if clear == 0:
139140
return (0, 0, 0)
141+
142+
# Each color value is normalized to clear, to obtain int values between 0 and 255.
143+
# A gamma correction of 2.5 is applied to each value as well, first dividing by 255,
144+
# since gamma is applied to values between 0 and 1
140145
red = int(pow((int((r / clear) * 256) / 255), 2.5) * 255)
141146
green = int(pow((int((g / clear) * 256) / 255), 2.5) * 255)
142147
blue = int(pow((int((b / clear) * 256) / 255), 2.5) * 255)
148+
143149
# Handle possible 8-bit overflow
144150
if red > 255:
145151
red = 255

examples/tcs34725_simpletest.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,28 @@
1212
i2c = board.I2C() # uses board.SCL and board.SDA
1313
sensor = adafruit_tcs34725.TCS34725(i2c)
1414

15+
# Change sensor integration time to values between 2.4 and 614.4 milliseconds
16+
# sensor.integration_time = 150
17+
18+
# Change sensor gain to 1, 4, 16, or 60
19+
# sensor.gain = 4
20+
1521
# Main loop reading color and printing it every second.
1622
while True:
23+
# Raw data from the sensor in a 4-tuple of red, green, blue, clear light component values
24+
# print(sensor.color_raw)
25+
26+
color = sensor.color
27+
color_rgb = sensor.color_rgb_bytes
28+
print(
29+
"RGB color as 8 bits per channel int: #{0:02X} or as 3-tuple: {1}".format(
30+
color, color_rgb
31+
)
32+
)
33+
1734
# Read the color temperature and lux of the sensor too.
1835
temp = sensor.color_temperature
1936
lux = sensor.lux
20-
print("Temperature: {0}K Lux: {1}".format(temp, lux))
37+
print("Temperature: {0}K Lux: {1}\n".format(temp, lux))
2138
# Delay for a second and repeat.
2239
time.sleep(1.0)

0 commit comments

Comments
 (0)