Skip to content

Instantly share code, notes, and snippets.

@kasperkamperman
Created October 27, 2014 15:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasperkamperman/3c3f72208366ed885f2f to your computer and use it in GitHub Desktop.
Save kasperkamperman/3c3f72208366ed885f2f to your computer and use it in GitHub Desktop.
Render a lookup table with luminance data for eye response.
/*
Render a lookup table with luminance data for eye response.
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
if you can't use dithering it would be smart to add 1 to the output for FastLED 3.0
since a value of 1 still is 0 due the inner workings of FastLED when dithering is turned off.
*/
int [] luminanceLUT = new int[256];
boolean addOneForFastLED = false;
void setup()
{
println("const uint8_t luminanceLUT[] = {");
for(int i = 0; i < 256; i++)
{
// brightness should be a value from 0 to 100
float brightnessScale = map(i,0,255,0.0,100.0);
float luminance;
if(i>7) luminance = pow((brightnessScale+16.0)/116.0, 3);
else luminance = brightnessScale/903.3;
//luminance is a value between 0 and 1
luminanceLUT[i] = floor(luminance * 255);
if(addOneForFastLED) luminanceLUT[i] = constrain(luminanceLUT[i] + 1,0,255);
//String writeValue = "0x"+hex(luminanceLUT[i],2);
String writeValue = ""+luminanceLUT[i];
if(i!=255) writeValue = writeValue + ", ";
writeValue = writeValue + printWhiteSpace(luminanceLUT[i]);
if(i%16==0) print("\t");
if(i%16==15) println(writeValue);
else print(writeValue);
}
println("}");
exit();
}
String printWhiteSpace(int val)
{
String whitespace = "";
if(val<10) whitespace = " ";
else if(val<100) whitespace = " ";
else whitespace = "";
return whitespace;
}
/* output of the script above (FastLED is false)
const uint8_t luminanceLUT[] = {
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3,
3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6,
6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11,
11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 16, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24,
25, 25, 26, 26, 27, 28, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34,
35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 43, 44, 44, 45, 46,
47, 48, 49, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78,
79, 80, 82, 83, 84, 85, 87, 88, 89, 90, 92, 93, 94, 96, 97, 99,
100, 101, 103, 104, 106, 107, 108, 110, 111, 113, 114, 116, 118, 119, 121, 122,
124, 125, 127, 129, 130, 132, 134, 135, 137, 139, 141, 142, 144, 146, 148, 149,
151, 153, 155, 157, 159, 161, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180,
182, 185, 187, 189, 191, 193, 195, 197, 200, 202, 204, 206, 208, 211, 213, 215,
218, 220, 222, 225, 227, 230, 232, 234, 237, 239, 242, 244, 247, 249, 252, 255
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment