The way Grayscale works in RGB is that when all the values of red green and blue are equal, it is a shade of gray. (0,0,0) is black, and (255,255,255) is white. Something like (127,127,127) would be an in-between shade of gray. The higher the numbers, the lighter the gray, so (55,55,55) is darker than (190,190,190)
If you wanted to tint a grayscale image red, for example, you could create a function which increases the red value of each pixel by an amount which is randomly generated. I would go about this by using nested for
loops to read each pixel of the image and add some red to it. you could also subtract the other values slightly so the image does not get too light.
Here is a pseudo-code method that you could use to achieve this:
int randRed = rand.nextInt(50)
Color[][] colorArray = new Color[image width][image height];
//nested for loops to assign each pixel into Color[][] array
for (int i=0; i < [image width]; i++) {
for (int j=0; j < [image height]; j++) {
colorArray[i][j] = image.getRGB();
}
}
//nested for loops to add 'randRed' to each pixel into Color[][] array
for (int i=0; i < [image width]; i++) {
for (int j=0; j < [image height]; j++) {
colorArray[i][j] = new Color(image.getR()+randRed, image.getG(), image.getB());
}
}
You would need to add checks to ensure that your rib values do not exceed 255 or go below 0, which could be achieved with some if
statements. Also create functions like getR()
to get the red value, etc. which shouldn't be too difficult. And figure out how to print the image back, but this should give you a good start!