public bool SetElementValue(int index, Vector4 value) { if (index > elements.Count - 1 || index < 0) { return(false); } GradientElement el = elements[index]; el.value = value; elements[index] = el; return(true); }
public Vector4 Sample(float position) { if (elements.Count == 0) { return(Vector4.Zero); } if (elements[0].position > position) { return(elements[0].value); } if (elements[elements.Count - 1].position < position) { return(elements[elements.Count - 1].value); } GradientElement firstElement = elements[0]; for (int i = 1; i < elements.Count; i++) { GradientElement secondElement = elements[i]; if (position == secondElement.position) // small check { return(secondElement.value); } if (firstElement.position < position && secondElement.position > position) { float sample = (position - firstElement.position) / (secondElement.position - firstElement.position); return(Vector4.Lerp(firstElement.value, secondElement.value, sample)); } firstElement = secondElement; } throw new Exception("unexpected end of sampling method!"); }
private void LoadGradientNode(string key, XmlNode node) { LinearGradientMode mode; XmlAttribute attrMode = node.Attributes ["mode"]; switch (attrMode.Value) { case "vertical": mode = LinearGradientMode.Vertical; break; case "horizontal": mode = LinearGradientMode.Horizontal; break; case "forward-diagonal": mode = LinearGradientMode.ForwardDiagonal; break; case "backward-diagonal": mode = LinearGradientMode.BackwardDiagonal; break; default: throw new Exception("Invalid or unspecified <gradient> mode"); } XmlNode gradStartNode = node.SelectSingleNode("startcolor"); if (gradStartNode == null) { throw new Exception("Gradient <startcolor> not specified"); } Color startColor = LoadColorFromNode(gradStartNode); XmlNode gradEndNode = node.SelectSingleNode("endcolor"); if (gradEndNode == null) { throw new Exception("Gradient <endcolor> not specified"); } Color endColor = LoadColorFromNode(gradEndNode); float[] blendPositions = null; float[] blendFactors = null; XmlNodeList blendNodes = node.SelectNodes("blend"); if (blendNodes.Count > 0) { blendPositions = new float [blendNodes.Count + 2]; blendFactors = new float [blendNodes.Count + 2]; blendPositions [0] = 0.0f; blendFactors [0] = 0.0f; blendPositions [blendNodes.Count + 1] = 1.0f; blendFactors [blendNodes.Count + 1] = 1.0f; for (int i = 0; i < blendNodes.Count; i++) { XmlNode blendNode = blendNodes [i]; XmlAttribute attrPosition = blendNode.Attributes ["position"]; if (attrPosition == null) { throw new Exception("<blend> position not specified"); } XmlAttribute attrFactor = blendNode.Attributes ["factor"]; if (attrFactor == null) { throw new Exception("<blend> factor not specified"); } blendPositions [i + 1] = (float)Double.Parse(attrPosition.Value, CultureInfo.InvariantCulture); blendFactors [i + 1] = (float)Double.Parse(attrFactor.Value, CultureInfo.InvariantCulture); } } _schemeElements [key] = new GradientElement(startColor, endColor, mode, blendPositions, blendFactors); }