Skip to content

24 ways to impress your friends

Vote up?

Eric Wieser

Ok, it looks like I made a mistake reading that. #010000 does give the sensible choice of white.

However, I still feel that treating the entire color as one number is inherently wrong. Provided I keep my red component below 0×80, the text color will ALWAYS be white. That seems like a bad choice.

What I assume the author meant to do was to take the average of the components, and see if they were more than 50%. So:

return (r + g + b) / 3 > 0.5 ? ‘black’ : ‘white’;