/// <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); } } }
/// <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; }
/// <summary> /// Returns the RGB color of a vertex (without premultiplied alpha). /// </summary> public uint ColorAt(int index) { if (index < 0 || index >= _numVertices) { throw new IndexOutOfRangeException("Invalid vertex index"); } VertexColor vertexColor = _vertexColors[index]; if (_premultipliedAlpha) { vertexColor = VertexColorHelper.UnmultiplyAlpha(vertexColor); } return(ColorUtil.GetRGB(vertexColor.R, vertexColor.G, vertexColor.B)); }