I have a grayscale image and a some color, represented in RGB triplet. And i need to colorize grayscale image using this triplet.
The other answers suggest multiplying the grayscale value by the target RGB color. This is okay, but has the problem that it will alter the overall brightness of your picture. For example, if you pick a dark shade of green the whole image will go darker.
I think the RGB color model is not best suited for this. An alternative would be to pick the color alone, without an associated brightness, then colorize the image while preserving the original brightness of each pixel.
The algorithm would be something like this:
For the algorithm to convert HLS to RGB see this page or this page.
Conceptually, you'd take the greyscale value of each pixel in the original image and use that as a percentage of the green value. so if a pixel has greyscale value 87, then the equivalent pixel in the colorized image would be:
colorized_red = (87 / 255) * red_component(green_shade);
colorized_green = (87 / 255) * green_component(green_shade);
colorized_blue = (87 / 255) * blue_component(green_shade);