Skip to content

Use sRGB transfer function instead of plain gamma (better accuracy for lower brightness) #5707

Done

Description

@softhack007

It seems that sRGB has a better way for preserving low-brightness colors, compared to "plain" gamma correction.

As the gamma (and inverse gamma) LUT calculation is not in any hot path, we should try if the sRGB pedant holds its promise to provide smoother transitions at low brightness.

https://en.wikipedia.org/wiki/SRGB#Transfer_function_(%22gamma%22)

The math looks easy, by just replacing the "pow()" expression with the sRGB transfer function.
It's important to also adjust the inverse correction to use the inverse sRGB transfer function.

Edit: the sRGB transfer function uses a gamma exponent of 2.4, the resulting curve approximates a gamma=2.2 curve. I thinks it's possible to derive the correct scaling and offset coefficients for other gamma values - the wikipedia article shows the basic equations needed.

Activity

  1. DedeHai commented on Jun 28, 2026

    @DedeHai
    Collaborator

    that is a good suggestion, I ran extensive experiments on this already and found virtually no benefit - I did use various versions based on the CIE curve with varying "linear curve indices" and also tried different approximations. Depending up to which index the linear part goes (I even tried much steeper slopes) there is slightly better "dark end" color preservation but you also loose "gamma" in that section, sometimes distorting colors even more. It also brightens the dark end of the spectrum so "fading trails" look weird.
    There is no universal way to preserve dark colors except for just dropping them to black if they can not be displayed - you can check what that looks like using Blaz's dev branch.

  2. DedeHai commented on Jul 5, 2026

    @DedeHai
    Collaborator

    @softhack007 I even tried combining gamma & brightness for higher accuracy as a test and also added a "linear toe" for low brightness. This could be faster and more accurate per frame, it would be slower during brightness tansitions as every few frames a new table has to be created, could probably do some polynominal approximation to speed it up but the test show that for example the fading trails in "Matrix" disappear at very low brightness while the current approach (gamma + video dimming with hue preservation) keeps the trails although with some color errors. This combined approach would be more color accurate though as it does not accumulate the rounding errors.:

    void NeoGammaWLEDMethod::calcGammaTable(float gamma, uint8_t bri)
    {
      float gamma_inv = 1.0f / gamma;
      const float toe = 0.18f;
    
      for (size_t i = 1; i < 256; i++) {
        float x = (float)i / 255.0f;
    
        // CIE/sRGB-style piecewise blend:
        //   - toe * x      -> linear component, preserves low-end ratios
        //   - (1-toe) * x^gamma -> standard gamma component
        float y = toe * x + (1.0f - toe) * powf(x, gamma);
    
        // Scale back to 0–255
        float linear_val = y * 255.0f;
    
        // Bake brightness into the table in 16-bit precision, quantize to 8-bit once
        uint16_t gamma_full = (uint16_t)(linear_val * 257.0f + 0.5f); // 0-255 → 0-65535
        uint16_t combined   = (uint16_t)(((uint32_t)gamma_full * bri + 32767) >> 16);
    
        gammaT[i] = (combined > 255) ? 255 : (uint8_t)combined;
    
        // Inverse table uses pow() for simplicity and undoes gamma only, not brightness (good enough for testing)
        gammaT_inv[i] = (int)(powf(((float)i - 0.5f) / 255.0f, gamma_inv) * 255.0f + 0.5f);
      }
      gammaT[0] = 0;
      gammaT_inv[0] = 0;
    }

    Ultimately its a question of personal preference and "legacy" behaviour.

  3. softhack007 commented on Jul 6, 2026

    @softhack007
    MemberAuthor

    @DedeHai interesting experiments 😃

    fading trails in "Matrix" disappear at very low brightness while the current approach (gamma + video dimming with hue preservation) keeps the trails although with some color errors

    Yes, looks like an example of "you can't have both". In older release the Matrix trails would disappear depending on global brightness, so personally i'd prefer shorter trails instead of distorted colors at low brightness.

    Ultimately its a question of personal preference and "legacy" behaviour.

    We could allow both options, by either making the choice at compile-time (#ifdef WLED_USE_SRGB) or by adding a new drop-down where people can select "pure Gamma (legacy)" or "sRGB Gamma (better for low brightness)".

  4. softhack007 commented on Jul 6, 2026

    @softhack007
    MemberAuthor

    even tried combining gamma & brightness for higher accuracy as a test and also added a "linear toe" for low brightness. This could be faster and more accurate per frame, it would be slower during brightness tansitions as every few frames a new table has to be created

    @DedeHai just had an idea: would re-calculation also be necessary in case we store gammaT[] as uint16_t, i.e. gamma_full without multiplying in bri? It mean we'll need 512 bytes for the table, instead of 256 bytes. But that might be a price we can pay, at least on esp32.

  5. DedeHai commented on Jul 6, 2026

    @DedeHai
    Collaborator

    just doing some work on this. here are my current findings:

    • recalculating the gamma table using powf() on a C3 takes up to 18ms (!)
    • I had an AI write a quite accurate approximation which only takes 200us

    still highly experimental but promising. running the code with a very dumb "recalculate gamma table every frame" still runs at 124FPS on a C3. PS Fire dims down to black at bri<4 though, other FX might too so needs a lot of refinement.

  6. softhack007 commented on Jul 6, 2026

    @softhack007
    MemberAuthor
    uint16_t combined = (uint16_t)(((uint32_t)gamma_full * bri + 32767) >> 16);
    

    PS Fire dims down to black at bri<4 though

    I think the combined gamma value is using normal scaling, not "video scaling" ?

  7. DedeHai commented on Jul 7, 2026

    @DedeHai
    Collaborator

    yes, can't do video scaling with a lookup table and the video scaling is what "distorts" colors on fading trails, hence my elaborate video scaling with hue preservation.

  8. DedeHai commented on Jul 8, 2026

    @DedeHai
    Collaborator

    I played around some more with different gamma tables and brightness combinations. The only way to move back towards the pre 16.0 behaviour is to run brightness scaling before gamma. As per earlier findings that results in some issues with color loss but I might have a workaround for it: basically the issue is that if you scale a color to say 2% and then apply gamma, it goes to a value of 1 or 0 (for gamma=2) as we are scraping at "the tail end" of the curve. Adding CIE gamma (or sRGB as its also called) helps to mitigate this only mildly and results in even more color artefacts on "fading trails" for example in Matrix FX.
    What does work is limiting the brightness such that it does not go below gamma8inv(2), effectively meaning minimum brightness is where we still have at least two color steps.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions