画面上のどこからでもピクセル RGB 値を取得するカラー ピッカーを作成しています。選択した色に既にアルファ値があることを指定するオプションも必要です。結果の色をどのように計算できるのか疑問に思っています。
例えば:
結果のピクセルの色は 240,247,249 ですが、元の色の不透明度は 10% で、白 (255,255,255) の背景の上にあったことがわかります。元の RGB 値を計算する計算は何ですか?
画面上のどこからでもピクセル RGB 値を取得するカラー ピッカーを作成しています。選択した色に既にアルファ値があることを指定するオプションも必要です。結果の色をどのように計算できるのか疑問に思っています。
例えば:
結果のピクセルの色は 240,247,249 ですが、元の色の不透明度は 10% で、白 (255,255,255) の背景の上にあったことがわかります。元の RGB 値を計算する計算は何ですか?
The standard blending equation is:
out = alpha * new + (1 - alpha) * old
Where out
, new
and old
are RGB colors, and alpha
is a floating point number in the range [0,1].
So, you have (for red):
240 = 0.1 * newR + 0.9 * 255
Solving for newR
, we get:
newR = (240 - 0.9 * 255) / 0.1
which evaluates to 105. Repeat for the other components, and you're done.
I'm just wondering how come you know its alpha and not its r,g,b... Unless the blending mode used is in every case Normal, you won't be able to calculate the original value. And even if all is ideal, you will only be able to approximate, as some rounding has probably taken place already when going from original to result. Also, you will most likely have to round when going back from result to original.
If the blending mode is Normal, then it's not too difficult :-)
opacity*original + (1-opacity)*background = resulting pixel
original R = (resulting R - ((1-opacity)*background R)) / opacity.
original G = (resulting G - ((1-opacity)*background G)) / opacity.
original B = (resulting B - ((1-opacity)*background B)) / opacity.
in which opacity is (alpha/100)
.