La conversione di ARBG RGB con alpha blending
Domanda
Diciamo che abbiamo un colore ARGB:
Color argb = Color.FromARGB(127, 69, 12, 255); //Light Urple.
Quando questo è dipinta sulla parte superiore di un colore, i colori si fondono.Così, quando si è mescolato con il bianco, il colore risultante è Color.FromARGB(255, 162, 133, 255);
La soluzione dovrebbe essere questa:
Color blend = Color.White;
Color argb = Color.FromARGB(127, 69, 12, 255); //Light Urple.
Color rgb = ToRGB(argb, blend); //Same as Color.FromARGB(255, 162, 133, 255);
Che cosa è ToRGB
implementazione?
Soluzione
Si chiama alpha blending.
In psuedocode, assumendo il colore di sfondo (blend) ha sempre 255 alfa.Presuppone, inoltre, l'alfa è 0-255.
alpha=argb.alpha()
r = (alpha/255)*argb.r() + (1 - alpha/255)*blend.r()
g = (alpha/255)*argb.g() + (1 - alpha/255)*blend.g()
b = (alpha/255)*argb.b() + (1 - alpha/255)*blend.b()
nota:probabilmente avete bisogno di essere un po ' più attenti a virgola mobile/int matematica e problemi di arrotondamento, a seconda della lingua.Cast intermedi di conseguenza
Edito per aggiungere:
Se non avete un colore di sfondo con un alfa di 255, l'algebra diventa molto più complicato.Ho fatto prima, ed è un esercizio divertente, lasciata al lettore (se si ha realmente bisogno di sapere, un'altra domanda :).
In altre parole, di che colore C si fonde con un po ' di sfondo uguale fusione di Una, quindi di fusione B.Questo è un po ' come il calcolo di A+B (che non è la stessa come B+A).
Altri suggerimenti
So che questo è un vecchio thread, ma voglio aggiungere questo:
Public Shared Function AlphaBlend(ByVal ForeGround As Color, ByVal BackGround As Color) As Color
If ForeGround.A = 0 Then Return BackGround
If BackGround.A = 0 Then Return ForeGround
If ForeGround.A = 255 Then Return ForeGround
Dim Alpha As Integer = CInt(ForeGround.A) + 1
Dim B As Integer = Alpha * ForeGround.B + (255 - Alpha) * BackGround.B >> 8
Dim G As Integer = Alpha * ForeGround.G + (255 - Alpha) * BackGround.G >> 8
Dim R As Integer = Alpha * ForeGround.R + (255 - Alpha) * BackGround.R >> 8
Dim A As Integer = ForeGround.A
If BackGround.A = 255 Then A = 255
If A > 255 Then A = 255
If R > 255 Then R = 255
If G > 255 Then G = 255
If B > 255 Then B = 255
Return Color.FromArgb(Math.Abs(A), Math.Abs(R), Math.Abs(G), Math.Abs(B))
End Function
public static Color AlphaBlend(Color ForeGround, Color BackGround)
{
if (ForeGround.A == 0)
return BackGround;
if (BackGround.A == 0)
return ForeGround;
if (ForeGround.A == 255)
return ForeGround;
int Alpha = Convert.ToInt32(ForeGround.A) + 1;
int B = Alpha * ForeGround.B + (255 - Alpha) * BackGround.B >> 8;
int G = Alpha * ForeGround.G + (255 - Alpha) * BackGround.G >> 8;
int R = Alpha * ForeGround.R + (255 - Alpha) * BackGround.R >> 8;
int A = ForeGround.A;
if (BackGround.A == 255)
A = 255;
if (A > 255)
A = 255;
if (R > 255)
R = 255;
if (G > 255)
G = 255;
if (B > 255)
B = 255;
return Color.FromArgb(Math.Abs(A), Math.Abs(R), Math.Abs(G), Math.Abs(B));
}
se non avete bisogno di sapere questo pre-rendering, si può sempre utilizzare il win32 metodo di getpixel, credo.
Nota:digitando su iPhone nel mezzo del Missouri, senza inet accesso.Cerca reale win32 esempio e vedere se c'è un .equivalente netto.
Nel caso In cui nessuno si preoccupa, e non desidera utilizzare la (ottima) risposta postato sopra, è possibile ottenere il valore del colore di un pixel .Net tramite questo link Esempio MSDN