Skip to content

Commit

Permalink
use a logarithmic brightness adjustment instead of a linear divider
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzuger committed Mar 23, 2021
1 parent 6a908a9 commit a4fdc4d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
39 changes: 39 additions & 0 deletions tlc5947/color.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ rgb12 rgb8torgb12(rgb8 c){
return _c;
}

static const uint16_t logLUT[2][12] = { {
0, 353, 1109, 1990, 2614, 3495, 4120, 5000, 5775, 6990, 8495, 10000},
{ 0, 1500, 4000, 6000, 7000, 8000, 8500, 9000, 9300, 9600, 9800, 10000 }
};

static float log_brightness(float f_linval){
uint8_t i;
uint16_t ipf;
uint16_t linval = (uint16_t)(f_linval * ((float)10000));

if(linval >= logLUT[1][11]){
ipf = logLUT[0][11];
}else{
for(i = 0; i < 11; i++){
if(linval < logLUT[1][i]){
break;
}
}

ipf = (linval - logLUT[1][i - 1]) * 10 / (logLUT[1][i] - logLUT[1][i - 1]);
ipf = logLUT[0][i - 1] + (logLUT[0][i] - logLUT[0][i - 1]) * ipf / 10;
}

return (float)(((float)ipf) / ((float)10000));
}


rgb12 rgb12_brightness(rgb12 c, float brightness){
rgb12 _c;

float b = log_brightness(brightness);

_c.r = (uint16_t)((float)c.r * b);
_c.g = (uint16_t)((float)c.g * b);
_c.b = (uint16_t)((float)c.b * b);

return _c;
}

void default_white_balance(white_balance_matrix m){
for(uint8_t i = 0; i < 3; i++)
m[i] = 1.0;
Expand Down
2 changes: 2 additions & 0 deletions tlc5947/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ rgb8 rgb12torgb8(rgb12 c)__attribute__ ((const));

rgb12 rgb8torgb12(rgb8 c)__attribute__ ((const));

rgb12 rgb12_brightness(rgb12 c, float brightness);

void default_white_balance(white_balance_matrix m);
rgb12 rgb12_white_balance(rgb12 c, white_balance_matrix m);

Expand Down
4 changes: 1 addition & 3 deletions tlc5947/tlc5947.c
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,7 @@ static bool pattern_do_tick(tlc5947_tlc5947_obj_t* self, pattern_base_t* pattern

pattern->brightness = clamp(pattern->brightness + p->brighness.brightness, 0.0F, 1.0F);

pattern->color.r = (uint16_t)((float)pattern->base_color.r * pattern->brightness);
pattern->color.g = (uint16_t)((float)pattern->base_color.g * pattern->brightness);
pattern->color.b = (uint16_t)((float)pattern->base_color.b * pattern->brightness);
pattern->color = rgb12_brightness(pattern->base_color, pattern->brightness);

pattern->current++;
if(pattern->current == pattern->len)
Expand Down

0 comments on commit a4fdc4d

Please sign in to comment.