Last active
August 25, 2024 09:10
-
-
Save mathiasvr/19ce1d7b6caeab230934080ae1f1380e to your computer and use it in GitHub Desktop.
Convert LED brightness to PWM value based on CIE 1931 curve.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate lookup table for converting between perceived LED brightness and PWM | |
# Adapted from: https://jared.geek.nz/2013/feb/linear-led-pwm | |
# See also: https://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/ | |
from sys import stdout | |
TABLE_SIZE = 256 # Number of steps (brightness) | |
RESOLUTION = 2**10 # PWM resolution (10-bit = 1024) | |
def cie1931(L): | |
L *= 100 | |
if L <= 8: | |
return L / 902.3 | |
else: | |
return ((L + 16) / 116)**3 | |
x = range(0, TABLE_SIZE) | |
y = [cie1931(float(L) / (TABLE_SIZE - 1)) * (RESOLUTION - 1) for L in x] | |
stdout.write('const uint16_t CIE[%d] = {' % TABLE_SIZE) | |
for i, L in enumerate(y): | |
if i % 16 == 0: | |
stdout.write('\n') | |
stdout.write('% 5d,' % round(L)) | |
stdout.write('\n};\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script output: