Skip to content

Instantly share code, notes, and snippets.

@vivkin
Created September 6, 2014 21:45
Show Gist options
  • Save vivkin/614ab80699cb213fa4b3 to your computer and use it in GitHub Desktop.
Save vivkin/614ab80699cb213fa4b3 to your computer and use it in GitHub Desktop.
from math import pow
def linear_to_srgb(c):
if c < 0.0031308:
return 12.92 * c
return 1.055 * pow(c, 0.41666) - 0.055
def srgb_to_linear(c):
if c < 0.04045:
return c / 12.92
return pow((c + 0.055) / 1.055, 2.4)
def lut(fn):
return [int(255.0 * fn(i / 255.0) + 0.5) for i in range(256)]
print 'static const uint8_t LINEAR_TO_SRGB[256] = {' + ', '.join(str(i) for i in lut(linear_to_srgb)) + '};'
print 'static const uint8_t SRGB_TO_LINEAR[256] = {' + ', '.join(str(i) for i in lut(srgb_to_linear)) + '};'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment