Photo editing software seems magic sometimes in the way that it can switch a photo from color to black and white with a button click. Learning the code for this has pulled back the curtain on this magical process.
When the Red, Green, and Blue values for a given pixel (between 0-255) are the same, you get a gray tone. This also might explain why scanners or other imaging equipment list color settings as 24 bit and grayscale is only one third the value at 8 bit. If only one number value is needed to determine the pixel color instead of three separate ones for each color channel in Red, Green, and Blue then you have one third of the data.
With the handy for-loop command, Nick Parlante taught us how to calculate an average value for each pixel in a color image and modify each channel to be the same value to change it to gray. Here is the average snippet of code that generated the value to set for each pixel in the loop:
avg = (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3;