PremultiplyAlpha() public static method

public static PremultiplyAlpha ( VertexColor color ) : VertexColor
color VertexColor
return VertexColor
Beispiel #1
0
        /// <summary>
        /// Multiplies a range of alpha values with a certain factor.
        /// </summary>
        public void ScaleAlphaBy(float factor, int index, int numVertices)
        {
            if (index < 0 || index >= _numVertices)
            {
                throw new IndexOutOfRangeException("Invalid vertex index");
            }

            if (factor == 1.0f)
            {
                return;
            }

            int minAlpha = _premultipliedAlpha ? (int)(MIN_ALPHA * 255.0f) : 0;

            for (int i = index; i < index + numVertices; ++i)
            {
                VertexColor vertexColor = _vertexColors[i];
                byte        newAlpha    = Convert.ToByte(NumberUtil.Clamp(vertexColor.A * factor, minAlpha, 255));

                if (_premultipliedAlpha)
                {
                    vertexColor      = VertexColorHelper.UnmultiplyAlpha(vertexColor);
                    vertexColor.A    = newAlpha;
                    _vertexColors[i] = VertexColorHelper.PremultiplyAlpha(vertexColor);
                }
                else
                {
                    _vertexColors[i] = VertexColorHelper.CreateVertexColor(vertexColor.R, vertexColor.G, vertexColor.B, newAlpha);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Changes the way alpha and color values are stored.
        /// Optionally, all exisiting vertices are updated.
        /// </summary>
        public void SetPremultipliedAlpha(bool value, bool updateVertices)
        {
            if (value == _premultipliedAlpha)
            {
                return;
            }

            if (updateVertices)
            {
                if (value)
                {
                    for (int i = 0; i < _numVertices; ++i)
                    {
                        _vertexColors[i] = VertexColorHelper.PremultiplyAlpha(_vertexColors[i]);
                    }
                }
                else
                {
                    for (int i = 0; i < _numVertices; ++i)
                    {
                        _vertexColors[i] = VertexColorHelper.UnmultiplyAlpha(_vertexColors[i]);
                    }
                }
            }

            _premultipliedAlpha = value;
        }
Beispiel #3
0
        /// <summary>
        /// Updates the color of a vertex. The method always expects non-premultiplied alpha values.
        /// </summary>
        public void SetColor(uint color, float alpha, int atIndex)
        {
            if (atIndex < 0 || atIndex >= _numVertices)
            {
                throw new IndexOutOfRangeException("Invalid vertex index");
            }

            alpha = NumberUtil.Clamp(alpha, _premultipliedAlpha ? MIN_ALPHA : 0.0f, 1.0f);

            VertexColor vertexColor = VertexColorHelper.CreateVertexColor(color, alpha);

            _vertexColors[atIndex] = _premultipliedAlpha ? VertexColorHelper.PremultiplyAlpha(vertexColor) : vertexColor;
        }