diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index 411d39ec..aaadf7bc 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -64,6 +64,12 @@ namespace VocaluxeLib return (int)result; } + public static bool InRange(this T val, T min, T max) where T : IComparable + { + Debug.Assert(min.CompareTo(max) <= 0); + return val.CompareTo(min) >= 0 && val.CompareTo(max) <= 0; + } + /// /// Makes sure val is between min and max /// Asserts that min<=max diff --git a/VocaluxeLib/CXMLReader.cs b/VocaluxeLib/CXMLReader.cs index 4b06b922..8ebf0d19 100644 --- a/VocaluxeLib/CXMLReader.cs +++ b/VocaluxeLib/CXMLReader.cs @@ -88,20 +88,25 @@ namespace VocaluxeLib return GetValue(xPath, out val, value.ToString()) && CHelper.TryParse(val, out value); } + /// + /// Gets a normalized (0<=x<=1) float value + /// + /// + /// + /// True if exists and is normalized + public bool TryGetNormalizedFloatValue(string xPath, ref float value) + { + string val; + return GetValue(xPath, out val, value.ToString()) && CHelper.TryParse(val, out value) && value.InRange(0f, 1f); + } + public bool TryGetColorFromRGBA(string xPath, ref SColorF value) { bool result = true; - result &= TryGetFloatValue(xPath + "/R", ref value.R); - result &= TryGetFloatValue(xPath + "/G", ref value.G); - result &= TryGetFloatValue(xPath + "/B", ref value.B); - result &= TryGetFloatValue(xPath + "/A", ref value.A); - if (result) - { - value.R = value.R.Clamp(0f, 1f); - value.G = value.G.Clamp(0f, 1f); - value.B = value.B.Clamp(0f, 1f); - value.A = value.A.Clamp(0f, 1f); - } + result &= TryGetNormalizedFloatValue(xPath + "/R", ref value.R); + result &= TryGetNormalizedFloatValue(xPath + "/G", ref value.G); + result &= TryGetNormalizedFloatValue(xPath + "/B", ref value.B); + result &= TryGetNormalizedFloatValue(xPath + "/A", ref value.A); return result; }