2

For example, I know the color is #a0c5e8 (160,197,232) when over white, and it's #496e91 (73,110,145) when over black.

Is there a way to calculate the "real" color (at 100% opacity)?

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Sarke
  • 2,805
  • 2
  • 18
  • 28

1 Answers1

4

Yes you can, by solving a system of linear equations. Let's look at the red channel as an example:

Variables:
c - color (unknown)
f - opacity (unknown)

Equations:
c * f + (1 − f) * 255 = 160. (blending with white)
c * f + (1 − f) * 0 = 73. (blending with black)

Rearrange to get:
c * f − 255 f = −95.
c * f = 73.
255 f = 168.

Therefore:
f = 168/255 ≈ 65.9%.
c = 6205/56 ≈ 110.8.

Computing the other channels, your final color is (111,167,220) and opacity is 66%.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
  • 1
    Perfect! Math checks out, and so does the visual test! Final color is rgba(111, 167, 220, 0.659) – Sarke Aug 14 '15 at 18:21
  • I'm trying to use this equation for something I'm working on. But I cannot for the life of me work out how you got 6205 & 56? – milky_jay Nov 20 '20 at 04:09