Skip to content

Commit 42cf3c9

Browse files
committed
Update TC34725 color temp calculcation to catch divide by zero and return no value.
1 parent a8ea1eb commit 42cf3c9

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Adafruit_TCS34725/Adafruit_TCS34725.py

+8
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,18 @@ def calculateColorTemperature(rgb):
196196
Y = (-0.32466 * rgb['r']) + (1.57837 * rgb['g']) + (-0.73191 * rgb['b'])
197197
Z = (-0.68202 * rgb['r']) + (0.77073 * rgb['g']) + ( 0.56332 * rgb['b'])
198198

199+
# Check for divide by 0 (total darkness) and return None.
200+
if (x + y + z) == 0:
201+
return None
202+
199203
# 2. Calculate the chromaticity co-ordinates
200204
xc = (X) / (X + Y + Z)
201205
yc = (Y) / (X + Y + Z)
202206

207+
# Check for divide by 0 again and return None.
208+
if (0.1858 - yc) == 0:
209+
return None
210+
203211
# 3. Use McCamy's formula to determine the CCT
204212
n = (xc - 0.3320) / (0.1858 - yc)
205213

Adafruit_TCS34725/Adafruit_TCS34725_Example.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
colorTemp = tcs.calculateColorTemperature(rgb)
1717
lux = tcs.calculateLux(rgb)
1818
print rgb
19-
print "Color Temperature: %d K" % colorTemp
19+
if colorTemp is None:
20+
print 'Too dark to determine color temperature!'
21+
else:
22+
print "Color Temperature: %d K" % colorTemp
2023
print "Luminosity: %d lux" % lux
2124
tcs.setInterrupt(True)
2225
sleep(1)

0 commit comments

Comments
 (0)