Color over grayscale image

你离开我真会死。 提交于 2019-12-13 21:19:30

问题


I want to color gray-scale image with only one color. So I have for example pixel: RGB(34,34,34) and I want to color it with color: RGB(200,100,50) to get new RGB pixel. So I new to do this for every pixel in image.

The white pixels get color: RGB(200,100,50), darker pixels get darker color than RGB(200,100,50).

So the result is gray-scale with black and selected color instead of black and white.

I will program this hard core without any built in function.

Similar to this: Image or this:Image


回答1:


All you need to do is use the ratio of gray to white as a multiplier to your color. I think you'll find that this gives better results than a blend.

new_red = gray * target_red / 255
new_green = gray * target_green / 255
new_blue = gray * target_blue / 255



回答2:


From what you describe I figure you look for a blending algorithm.

What you need is a blendingPercentage (bP).

new red = red1 * bP + red2 * (1 - bP)
new green = green1 * bP + green2 * (1 - bP)
new blue = blue1 * bP + blue2 * (1 - bP)

Your base color is RGB 34 34 34; Color to blend is RGB 200 100 50
BlendingPercentage for example = 50% -> 0.5

Therefore:
New red = 34 * 0.5 + 200 * (1 - 0.5) = 117
New green = 34 * 0.5 + 100 * (1 - 0.5) = 67
New blue = 34 * 0.5 + 50 * (1 - 0.5) = 42



来源:https://stackoverflow.com/questions/16546862/color-over-grayscale-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!