private static void FromString(string stringValue, CultureInfo cultureInfo, out double value, out HotSpotUnit unit) { var goodString = stringValue.Trim().ToLowerInvariant(); unit = HotSpotUnit.Pixel; var strLen = goodString.Length; var strLenUnit = 0; var unitFactor = 1.0; var matchedType = false; // this is where we would handle trailing whitespace on the input string. // peel [unit] off the end of the string foreach (var tuple in UnitTuples) { // Note: this is NOT a culture specific comparison. // this is by design: we want the same unit string table to work across all cultures. if (!goodString.EndsWith(tuple.Item1, StringComparison.Ordinal)) { continue; } strLenUnit = tuple.Item1.Length; unit = tuple.Item2; matchedType = true; break; } // we couldn't match a real unit from HotSpotUnit. // try again with a converter-only unit (a pixel equivalent). if (matchedType) { foreach (var tuple in PixelUniTuples) { // Note: this is NOT a culture specific comparison. // this is by design: we want the same unit string table to work across all cultures. if (!goodString.EndsWith(tuple.Item1, StringComparison.Ordinal)) { continue; } strLenUnit = tuple.Item1.Length; unitFactor = tuple.Item2; break; } } // this is where we would handle leading whitespace on the input string. // this is also where we would handle whitespace between [value] and [unit]. // check if we don't have a [value]. This is acceptable for certain UnitTypes. var valueString = goodString.Substring(0, strLen - strLenUnit); value = Convert.ToDouble(valueString, cultureInfo) * unitFactor; }
/// <summary>Returns the hash code for this instance.</summary> /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns> public override int GetHashCode() { unchecked { var hash = 17; hash = hash * 23 + Value.GetHashCode(); hash = hash * 23 + HotSpotUnit.GetHashCode(); return(hash); } }
/// <summary> /// Create a HotSpot with the provided value and HotSpotUnit. The value /// is either representing /// </summary> /// <param name="value">the value</param> /// <param name="type">the unit type (defaults to HotSpotUnit.Pixel)</param> public HotSpot(double value, HotSpotUnit type = HotSpotUnit.Pixel) { Value = value; HotSpotUnit = type; if (IsProportional && !Value.IsInRange(0, 1, 0.01)) { throw new ArgumentOutOfRangeException(nameof(value), value, "Value must be between 0 and 1"); } if (IsAbsolute && Value < 0) { throw new ArgumentOutOfRangeException(nameof(value), value, "Value must be positive"); } }