1

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.

Community
  • 1
  • 1
Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • 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 Nov 27 '15 at 14:10
  • Possible duplicate of [Algorithm for Additive Color Mixing for RGB Values](http://stackoverflow.com/questions/726549/algorithm-for-additive-color-mixing-for-rgb-values) – Panda Nov 27 '15 at 14:32
  • @Panda I don't think it's a duplicate. I've edited the question to explain why. – Grodriguez Nov 27 '15 at 14:48
  • 1
    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. –  Nov 28 '15 at 05:03
  • @Ike If I have a white wall at ambient light and put a red light on it, I will see it as "redish". – Grodriguez Nov 30 '15 at 08:33
  • @Grodriguez Oh I see, light absorption and reflection under some kind of medium ambient lighting condition. –  Nov 30 '15 at 14:35
  • @Grodriguez That's a bit tricky if you want to avoid per-pixel control -- none of the Photoshop blending modes do that, e.g., and most libraries that provides some kind of blending API offer a subset of those modes. –  Nov 30 '15 at 14:38

1 Answers1

1

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.

Panda
  • 1,231
  • 18
  • 27
  • Yes, this is correct, but I am looking for a way to paint the color "over an arbitrary background" (could be for example an image), without iterating pixel by pixel. I will clarify this. I was hoping there would be a way to achieve this, perhaps playing with alpha. – Grodriguez Nov 27 '15 at 14:54