Pinta.Core.ColorBgra.Blend C# (CSharp) Method

Blend() public static method

Constructs a new ColorBgra instance from the values in the given Color instance. Converts this ColorBgra instance to a Color instance. Smoothly blends between two colors.
public static Blend ( ColorBgra ca, ColorBgra cb, byte cbAlpha ) : ColorBgra
ca ColorBgra
cb ColorBgra
cbAlpha byte
return ColorBgra
        public static ColorBgra Blend(ColorBgra ca, ColorBgra cb, byte cbAlpha)
        {
            uint caA = (uint)Utility.FastScaleByteByByte((byte)(255 - cbAlpha), ca.A);
            uint cbA = (uint)Utility.FastScaleByteByByte(cbAlpha, cb.A);
            uint cbAT = caA + cbA;

            uint r;
            uint g;
            uint b;

            if (cbAT == 0)
            {
                r = 0;
                g = 0;
                b = 0;
            }
            else
            {
                r = ((ca.R * caA) + (cb.R * cbA)) / cbAT;
                g = ((ca.G * caA) + (cb.G * cbA)) / cbAT;
                b = ((ca.B * caA) + (cb.B * cbA)) / cbAT;
            }

            return ColorBgra.FromBgra((byte)b, (byte)g, (byte)r, (byte)cbAT);
        }

Same methods

ColorBgra::Blend ( ColorBgra colors, int count ) : ColorBgra

Usage Example

Example #1
0
        public virtual void BeforeRender()
        {
            if (!this.lerpCacheIsValid)
            {
                byte startAlpha;
                byte endAlpha;

                if (this.alphaOnly)
                {
                    ComputeAlphaOnlyValuesFromColors(this.startColor, this.endColor, out startAlpha, out endAlpha);
                }
                else
                {
                    startAlpha = this.startColor.A;
                    endAlpha   = this.endColor.A;
                }

                this.lerpAlphas = new byte[256];
                this.lerpColors = new ColorBgra[256];

                for (int i = 0; i < 256; ++i)
                {
                    byte a = (byte)i;
                    this.lerpColors[a] = ColorBgra.Blend(this.startColor, this.endColor, a);
                    this.lerpAlphas[a] = (byte)(startAlpha + ((endAlpha - startAlpha) * a) / 255);
                }

                this.lerpCacheIsValid = true;
            }
        }