-
Notifications
You must be signed in to change notification settings - Fork 999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes crash in STPColorUtils.perceivedBrightnessForColor
#954
Conversation
This is used for deciding whether to use light/dark scroll bars & status bar indicators, and choosing which color should be used for the switch in `STPSwitchTableViewCell`. This code assumed the color was always in the RGB color space, which is incorrect. It would reliably crash with Address Sanitization turned on, and give inconsistent answers with it turned off. Now using the `UIColor` `getRed:green:blue:alpha:` method to convert the color to RGB before calculating the luma value. Also added some links to documentation for why this calculation is interesting. Added tests for `STPColorUtils colorIsBright:` This also tests `perceivedBrightnessForColor:`, but does so by checking something that's more meaningful to me, rather than testing for specific luma numbers
3210389
to
f9eb0b9
Compare
CGFloat red, green, blue; | ||
if ([color getRed:&red green:&green blue:&blue alpha:nil]) { | ||
// We're using the luma value from YIQ | ||
// https://en.wikipedia.org/wiki/YIQ#From_RGB_to_YIQ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎓
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 nice work
-
I see
0.3
gets passed around quite often. Should we consolidate a little? Maybe just add a#define BRIGHT_COLOR_THRESHOLD 0.4
to the implementation and tests. I imagine figuring out a way to consolidate further can be tricky. -
Aside:
I can quickly understand testBuiltinColorsIsBright
but the other tests get pretty low level and I'm not sure if it'll be maintained (maybe we don't need to change them ever?).
I guess just testing that [UIColor whiteColor]
is "bright" covers the original ask and everything else is a bonus.
Thanks for the review, and for the chance to write a little more about this - mostly for posterity. There's a couple things going on. We have a pre-existing I think it's (slightly?) more likely that in the future we'll want to change our algorithm without changing the behavior with respect to grayscale, so that 30% gray is still the transition point. I think this is an updated algorithm (plus a example swift implementation). If I'm right (and I don't know how likely it that is), then having the value in the tests tied to the value in the implementation would be incorrect. There's also a situation where we cannot convert a color to RGB (probably because it's in an exotic color space?). The The |
* Conform to UITextInput * Split text storage * Cleanup * Cleanup * Fix test
Summary
This is used for deciding whether to use light/dark scroll bars & status bar indicators,
and choosing which color should be used for the switch in
STPSwitchTableViewCell
.This code assumed the color was always in the RGB color space, which is incorrect. It would
reliably crash with Address Sanitization turned on, and give inconsistent answers with
it turned off.
Now using the
UIColor
getRed:green:blue:alpha:
method to convert the color to RGBbefore calculating the luma value. Also added some links to documentation for why this
calculation is interesting.
Motivation
Fixes #951
Testing
Added tests for
STPColorUtils colorIsBright:
This also tests
perceivedBrightnessForColor:
, but does so by checking something that'smore meaningful to me, rather than testing for specific luma numbers