Skip to content

Instantly share code, notes, and snippets.

@hexagon5un
Last active January 12, 2024 20:14
Show Gist options
  • Save hexagon5un/3df734ad08d8dc8d9ace0491ef97cc58 to your computer and use it in GitHub Desktop.
Save hexagon5un/3df734ad08d8dc8d9ace0491ef97cc58 to your computer and use it in GitHub Desktop.
## Creates a gamma-corrected lookup table
import math
def gamma(nsteps, gamma):
gammaedUp = [math.pow(x, gamma) for x in range(nsteps)]
return [x/max(gammaedUp) for x in gammaedUp]
def rounder(topValue, gammas):
return [min(topValue, round(x*topValue)) for x in gammas]
if __name__ == "__main__":
myGamma = 2.3
steps = 16
output = open("gamma.h", "w")
output.write("/* %d-step brightness table: gamma = %s */ \n\n" % (steps, myGamma))
output.write("const uint8_t gamma_table[%d] = {\n" % steps)
for value in rounder(255, gamma(steps, myGamma)):
output.write("\t %d,\n" % value)
output.write("};\n")
output.close()
@Steve8291
Copy link

Thanks for putting this together. It was very helpful.
I think there is a typo in line 14. Should read:
output = open("gamma.h", "w")

@hexagon5un
Copy link
Author

Thanks for putting this together. It was very helpful. I think there is a typo in line 14. Should read: output = open("gamma.h", "w")

Thanks, and fixed.
BTW: that's no typo -- that's history! I must have written this before Python 3. Back then, open was file.

Surprised the rest still works, but it does. Woot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment