Question

Disons que nous avons une ARGB de la couleur:

Color argb = Color.FromARGB(127, 69, 12, 255); //Light Urple.

Quand c'est peint sur le dessus d'une couleur, les couleurs vont se mélanger.Alors, quand il est mélangé avec du blanc, la couleur résultante est Color.FromARGB(255, 162, 133, 255);

La solution devrait fonctionner comme ceci:

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);

Qu'est-ce que ToRGB's la mise en œuvre?

Était-ce utile?

La solution

Il est appelé alpha blending.

Dans psuedocode, en supposant que la couleur d'arrière-plan (mélange) a toujours 255 alpha.Suppose aussi l'alpha est de 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()

note:vous avez probablement besoin d'être un peu (plus) attention au sujet de la virgule flottante/int mathématiques et de l'arrondi, en fonction de la langue.Fonte des intermédiaires en conséquence

Edité pour ajouter:

Si vous n'avez pas de couleur de fond avec un alpha de 255, l'algèbre devient beaucoup plus compliqué.Je l'ai fait avant et c'est un exercice amusant à gauche pour le lecteur (si vous en avez vraiment besoin de savoir, poser une autre question :).

En d'autres termes, quelle est la couleur C se fond dans quelques fond la même que la fusion d'Un, puis le mélange B.C'est un peu comme le calcul de A+B (qui n'est pas le même que B+A).

Autres conseils

Je sais que c'est un vieux thread, mais je tiens à ajouter ceci:

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));
}

si vous n'avez pas besoin de savoir ce pré-rendu, vous pouvez toujours utiliser le win32 méthode getpixel, je crois.

Note:en tapant sur l'iPhone dans le milieu du Missouri sans inet accès.Recherche réel win32 exemple et voir si il y a un .net équivalent.

Dans le cas où quelqu'un se soucie, et ne veut pas utiliser le (excellent) réponse publiée ci-dessus, vous pouvez obtenir la valeur de la couleur d'un pixel .Net via ce lien MSDN exemple

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top