Grayscale – Intro to Java Programming


In the olden days, cameras couldn’t record glorious color, they only had grey. And sometimes we want to take a photograph and turn it back into grey to give it that old fashioned effect. Now, you might thing that all you’d have to do is average the red, green, and blue values. But it’s not quite that simple. As it turns out, the receptors that we have in our eyes have different sensitivities to colors. And what you actually have to do is take the red, green and blue values weighted by these factors, and then turn them into grey. And then, when you’re done with that, you can set red to these value green and blue, because they’re all the same, that’s what makes the color grey. And then, you’re done. But we have the same problem that you just saw. Red, green, blue are integers. These weighing constants are fractional numbers. So, the whole expression is a fractional number. And I want to put it back into an integer. So, the solution is again, to inject a cast, put this expression in parentheses and put parenthesis int before that. And then, you get it truncated down to the nearest integer. Once again, I’d like you to try that, so you can see it with your own eyes. Go ahead. Again, in the color class, implement a new method. In this case, it’s the turnGray method. Take the red, green and blue values, combine them to a gray value using the equation that you just saw. Make sure that you get an integer. And then, take that integer and put it back into red, green and blue. Go ahead and do it now.

Leave a Reply

Your email address will not be published. Required fields are marked *