brightness() claims to return a float when it's actually an integer?

I was hoping to use brightness() to find very small differences in brightness between pixels in source images, but when I look closely I see it always outputs ints cast as floats, like 32.0 or 43.0.

Any way to make it actually return floats?

Answers

  • edited April 2016

    It doesn't claim but actually delivers a float result.
    That is so b/c colorMode() can impose fractional ranges.

  • According to the reference page, it returns a float. That's what I meant when I said it 'claims' to do so.

    I read the colorMode() page but am still not clear how I can get brightness() to return a fractional value when evaluating .jpg's. Can you explain?

    Thanks!

  • We can convert any range to 0 to 1 using norm():
    https://Processing.org/reference/norm_.html

    Remember that by default, colorMode() is 0 to 255 range for all of its attributes.

  • edited April 2016

    Sorry, through norm() is new to me it's not what I'm looking for here.

    I want to run brightness(col) and get a result like 32.3 instead of 32.0.

    Generally the math to find brightness is a formula that weights green's brightness heavily, plus some contribution from red and blue. (If you could point me to where brightness() is defined in the language, I'd be happy to write my own myBrightness() for use here mimicking its qualities without rounding. I looked in the repo but wasn't able to find it, but I'm pretty elementary at that level of source code so that doesn't mean it's not there somewhere.)

    Even if the r, g, and b values are integers, I believe the brightness formula should only return values ending in .0 one tenth of the time, absent strange artifacts. I want to get these values with precision intact.

  • Answer ✓

    Sorry, that didn't help.

    I ended up writing a simple function that does the job for me:

    float luminance(color col){
        float lum = 0.2126*red(col) + 0.7152*green(col) + 0.0722*blue(col);
        return lum;
    }
    

    Note that these values come from the relevant Wikipedia article and full white will sum to 100 (which is fine by me, so long as I've got precision along the way).

Sign In or Register to comment.