Add range checks in XMLReader
This commit is contained in:
1 parent
388bd6310f
commit
9f8dfaa50c
2 files changed
+22
-11
No files matched your search
@@ -64,6 +64,12 @@ namespace VocaluxeLib
|
|||||||
return (int)result;
|
return (int)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool InRange<T>(this T val, T min, T max) where T : IComparable<T>
|
||||||
|
{
|
||||||
|
Debug.Assert(min.CompareTo(max) <= 0);
|
||||||
|
return val.CompareTo(min) >= 0 && val.CompareTo(max) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Makes sure val is between min and max
|
/// Makes sure val is between min and max
|
||||||
/// Asserts that min<=max
|
/// Asserts that min<=max
|
||||||
|
|||||||
+16
-11
@@ -88,20 +88,25 @@ namespace VocaluxeLib
|
|||||||
return GetValue(xPath, out val, value.ToString()) && CHelper.TryParse(val, out value);
|
return GetValue(xPath, out val, value.ToString()) && CHelper.TryParse(val, out value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a normalized (0<=x<=1) float value
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="xPath"></param>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns>True if exists and is normalized</returns>
|
||||||
|
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)
|
public bool TryGetColorFromRGBA(string xPath, ref SColorF value)
|
||||||
{
|
{
|
||||||
bool result = true;
|
bool result = true;
|
||||||
result &= TryGetFloatValue(xPath + "/R", ref value.R);
|
result &= TryGetNormalizedFloatValue(xPath + "/R", ref value.R);
|
||||||
result &= TryGetFloatValue(xPath + "/G", ref value.G);
|
result &= TryGetNormalizedFloatValue(xPath + "/G", ref value.G);
|
||||||
result &= TryGetFloatValue(xPath + "/B", ref value.B);
|
result &= TryGetNormalizedFloatValue(xPath + "/B", ref value.B);
|
||||||
result &= TryGetFloatValue(xPath + "/A", ref value.A);
|
result &= TryGetNormalizedFloatValue(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);
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user