Reputation: 21995
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
RGB=#ff0000 represents red light at 100% intensity
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.
Upvotes: 1
Views: 924
Reputation: 1239
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.
Upvotes: 1