I have a pixel with some color. I know for sure that white #FFFFFF was superimposed on its main color and when applied it turned out (for example) №808080. Is it possible to calculate the main color that was before the imposition of white? How can you subtract color from color? I tried to subtract aRGB color - it does not work.

Please explain or prove that this can not be.

  • And what does "superimposed" mean? - VladD
  • @VladD, for example, I have a white shape. With an opacity of 0.1. I take a screenshot and it turns out almost natural colors, since there is a white color from the form. - Emil Sabitov

2 answers 2

Let's count. Let the background be decomposed into components r , g , b , and your superimposed color - r1 , g1 , b1 , opacity = alpha . Then the resulting color has the components R = r * (1 - alpha) + r1 * alpha , G = g * (1 - alpha) + g1 * alpha , B = b * (1 - alpha) + b1 * alpha .

If you have r , g , b and alpha values, you can easily calculate:

 r1 = (R - r * (1 - alpha)) / alpha g1 = (G - g * (1 - alpha)) / alpha b1 = (B - b * (1 - alpha)) / alpha 

Counting is possible, of course, if alpha != 0 , that is, there is at least something left of the new color :)

  • one
    @ Emil Sabitov: source background r, g, b; superimposed color r1, g1, b1; the resulting color is R, G, B. If you have given r1, g1, b1 and R, G, B, you can calculate r, g, b in the same way: r = (R - r1 * alpha) / (1 - alpha) and t d. - VladD
  • @VladD, do you use color values ​​in 0..255? Just if in this interval, then after the decision, I get almost the same color (RGB), which turned out when applying. r1g1b1 is always white with transparency of 0.1. Can this be a snag?) - Emil Sabitov
  • @ Emil Sabitov: yes, 0..255. And what are your r1g1b1, RGB and alpha equal? For example, if you apply the color #FFFFFF with opacity = 0.1 and get #808080 : R = 0x80 r1 = 0xff alpha = 0.1 r = (R - r1 * alpha) / (1 - alpha) = (0x80 - 0xff * 0.1) / 0.9 = (128 - 25.5) / 0.9 = 113.88 ~ = 114 - VladD
  • In general, the color of the film is #FFFFFF with opacity 0.1. Apparently I 808080 clearly made a mistake, I'm sorry. I got the color # 191919. In general, the desired color should be # 000000 in this case (but it can be different. You should look for it) @VladD, Thanks for the answers. - Emil Sabitov
  • one
    @ Emil Sabitov: Well, everything is correct: r = (0x19 - 0xff * 0.1) / 0.9 = (25 - 25.5) / 0.9 It is clear that 0x19 is a rounded value, so that -0.5/0.9 gives our 0. - VladD

Something like that, probably:

 [DllImport("gdi32.dll")] static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos); //получаем цвет IntPtr hdc; int x,y; Color clr = Color.FromArgb((Int32)GetPixel(hdc, x, y)); 

Plus two links may still be useful:

http://habrahabr.ru/post/60085/

http://www.realcoding.net/articles/glava-6-metody.html

  • I almost understood how to work with it. I connected the library, but how to call correctly? @markgenuine - Emil Sabitov
  • one
    o_O And is it somehow connected with the question? - Qwertiy