Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasperkamperman/e7a990d6af49f4cbed32 to your computer and use it in GitHub Desktop.
Save kasperkamperman/e7a990d6af49f4cbed32 to your computer and use it in GitHub Desktop.
# gamma.py with implemented luminance curve (instead of gamma curve)
# now embedded in the original as well
# original: http://github.com/raplin/HexaWS2811/blob/master/gamma.py
#
# more about the luminance calculations:
# http://ledshield.wordpress.com/2012/11/13/led-brightness-to-your-eye-gamma-correction-no/
# http://forum.arduino.cc/index.php/topic,147810.0.html
fout=open("luminance.h","wt")
# adjust me! Each extra bit doubles the table size
ditherBits=4
ditherMSB=1<<(ditherBits-1)
res="/* Dithered gamma correction table - autogenerated by gamma.py */\n#define DITHER_BITS %d\nconst unsigned char gammaTable[]={" % ditherBits
finalAdjust=1 #set this to 1 if you want your values to start at one (1..255). This is a quirky request for FastLED users only
for dither in range(1<<ditherBits):
out=[]
#reverse the low order bits so the dithering is less flickery
ditherValue=0
dread=1<<ditherBits
dout=1
for d in range(ditherBits):
dread>>=1
if dither & dread:
ditherValue|=dout
dout<<=1;
ditherValue=(ditherValue<<(8-ditherBits))
for n in range(256):
# adjust for logarithmic eye response
# gamma=pow(255,(n/256.0))-1
# start luminance implementation ================================
# brightness should be a value from 0 to 100
brightnessScale = map(n,0,255,0.0,100.0)
if brightnessScale>8:
luminance = pow((brightnessScale+16.0)/116.0, 3)
else:
luminance = brightnessScale/903.3
# luminance is a value between 0 and 1
# we keep using the gamma variable
gamma=luminance*256
# end luminance implementation ===================================
gamma=int(gamma*256)
gamma+=ditherValue
out.append( min(255, (gamma>>8)+finalAdjust) )
if dither:
res+=","
res+="\n\t"+(",".join([ "0x%x"%n for n in out]))
res+="\n\t};\n"
println(res);
print >>fout,res
fout.close()
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment