1
votes

I have a RGB color that represents light. I am looking for an algorithm to draw this over an arbitrary background (could be, for example, an image) in a way that simulates (or resembles) lighting.

Some examples:

  • RGB=#888888 represents white light at 50% intensity

    • Painting this over white (#ffffff) background pixels would do nothing
    • Painting this over black (#000000) background pixels would paint as #888888
    • Painting this over red (#ff0000) background pixels would result in a "lighter red"
  • RGB=#ff0000 represents red light at 100% intensity

    • Painting this over white (#ffffff) background pixels should result in a "light red" (mix of red and white)
    • Painting this over black (#000000) background pixels would paint as #880000
  • RGB=#000000 represents no light. Painting this over any background should have no effect.

I was hoping that I would be able to translate the original RGB color in (a set of) RGBA color(s) that I could paint over the background. I have been looking for an algorithm for this and playing around with HSL, HSB, alpha, etc. but cannot find a generic way to accomplish this.

Is there a generic algorithm to achieve what I want?

Update: I am aware of this question, but I don't think this is a duplicate (despite the similar names). The accepted answer to that question describes a behaviour (Red + Black = Dark red) that does not match this sceneario (lighting). I am specifically looking for an algorithm to simulate (colored) lighting.

1
Did you try just sum each color and divide by two? It would satisfy all these examples except #888888 over #ffffff, but I think the correct behaviour (based on other examples) should be #c3c3c3, which is also sum and divide.libik
@Panda I don't think it's a duplicate. I've edited the question to explain why.Grodriguez
If it's painting with light, then painting red over a white background should result in white -- unless you're subtracting light. If that's correct and you never want to subtract light, then all you need is additive blending: sum up colors and clamp to a valid range.user4842163
@Ike If I have a white wall at ambient light and put a red light on it, I will see it as "redish".Grodriguez

1 Answers

1
votes

If you view the values as decimal you have values that range 0-255. You could sum the two colours and then rescale them back to the range.

FF0000 + FFFFFF 
= 255,0,0 + 255,255,255 
= 510,255,255

Then scale this by 255/510 to

510 * 255/510, 255 * 255/510, 255 * 255/510
= 255, 127, 127

A light red as required.