Other answers suggest multiplying the grayscale value with the target RGB color. This is normal, but there is a problem in that it will change the overall brightness of your image. For example, if you choose a darker shade of green, the whole image will become darker.
I think the RGB color model is not suitable for this. An alternative would be to select a color separately, without the corresponding brightness, and then colorize the image, preserving the original brightness of each pixel.
The algorithm will be something like this:
- select the target color in terms of two values, hue and saturation. The color tone determines the color tone (green, red, etc.), and the saturation determines how pure the color is (the cleaner, the more the color becomes gray).
- Now for each pixel in the grayscale image, calculate a new pixel in the HLS color model, where H and S are your target hue and saturation, and L is the gray value of your pixel.
- Convert this HLS color to RGB and save to a new image.
For the HLS to RGB conversion algorithm, see this page or this page .
source share