Comment by johnisgood

Comment by johnisgood 2 months ago

4 replies

Well, I used 0.5 as a convenient and intuitive midpoint of the 0-1 luminance range, but this of course is a simplification and doesn't align with human perception (edit: it is aligned), it was more of an example if anything.

You are right, 0.18 is indeed perceptually closer to "middle gray" because the eye responds more sensitively to darker tones, so yeah, using a threshold closer to 0.18 makes more sense if we want to identify whether a color "feels" light or dark.

That said, 0.5 is a mathematical midpoint, but as I said, not aligned with how humans perceive brightness (edit: it is aligned).

Ultimately one could use 0.18-0.3 as threshold.

anoncareer0212 2 months ago

> midpoint of the 0-1 luminance range

There are two physical quantities for luminance, relative, and perceptual, so that passed along a nugget for those not as wise as you who might not know that :) As you know and have mentioned, using 0.5 with the luminance calculation you mentioned, for relative luminance, would be in error (I hate being pedantic, but it's important for some parties, a11y is a de facto legal requirement for a lot of work, and 0.5 would be spot on for ensuring WCAG 2 text contrast as long as used with perceptual luminance, L*)

> doesn't align with human perception

It is 100% aligned with how humans perceive brightness, in fact, it's a stable work product dating back to the early 1900s.

> Ultimately one could use 0.18-0.3 as threshold

Perceptual luminance and relative luminance have precise mathematical definitions, one can be calculated in terms of the other.

If you need to hit contrast K with background color C, you won't be able to treat this as variable. What you pass along about it being variable is valuable, of course, in that, given K and C, output has a range, i.e. if contrast algo says you need +40 L* for your text to hit APCA/WCAG whatever, and your C has 50 L*, your palette is everything from 90 L* to 100 L* and 0 L* to 10 L*.

  • johnisgood 2 months ago

    So 0.5 is correct after all?! I thought I was completely off with 0.5 and I thought it does not align with human perception because I thought I was wrong. Ouch. In my defense, it has been a while. :D

    BTW, would this relatively simple way to determine if the color is dark work?

      $luminance = 0.299 * $r + 0.587 * $g + 0.114 * $b;
      return $luminance < $threshold;
    
    Where $threshold is 128, I think? IIRC 128 is a common threshold from what I remember, in this case.
    • Pinus 2 months ago

      I once did it with 0.3R + 0.6G + 0.1B < 128, mostly because I could not be bothered to think deeper than that. It was certainly not perfect — there were obvious cases where the opposite choice of black or white for contrast would have been better — but it worked well enough for my purpose (making a label at least not unreadable regardless of background colour).

      • johnisgood 2 months ago

        I implemented something where contrast matters a lot, so I am using more sophisticated algorithms, but yeah, there are cases where you could get away with less sophisticated ones. :D