Hydrogen Framework  1.3.1
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Hydrogen.Validate Class Reference

Additional static functions used to extend existing Validation support inside of Unity. More...

Static Public Member Functions

static float ColorPercentage (this Color sourceColor, Color targetColor)
 Calculate the percentage of similarity between two colors. More...
 
static bool IsColorApproximatelySame (this Color sourceColor, Color targetColor)
 Are the colors approximately equal? More...
 
static bool IsDictionaryEqual< TKey, TValue > (this IDictionary< TKey, TValue > first, IDictionary< TKey, TValue > second)
 Determines if dictionaries are equal. More...
 
static bool IsNumericType (this object value)
 Determines whether the specified value is of numeric type. More...
 
static bool IsPositive (this object value, bool zeroIsPositive=true)
 Determines whether the specified value is positive. More...
 
static bool ScrambledEquals< T > (IEnumerable< T > list1, IEnumerable< T > list2)
 Determines whether the two scrambled enumerable objects are equal. More...
 

Detailed Description

Additional static functions used to extend existing Validation support inside of Unity.

Definition at line 39 of file Validate.cs.

Member Function Documentation

static float Hydrogen.Validate.ColorPercentage ( this Color  sourceColor,
Color  targetColor 
)
static

Calculate the percentage of similarity between two colors.

Returns
The percentage of similarity (0-1)
Parameters
sourceColorLeft Side Color
targetColorRight Side Color

Definition at line 47 of file Validate.cs.

48  {
49  return ((targetColor.r / sourceColor.r) +
50  (targetColor.g / sourceColor.g) +
51  (targetColor.b / sourceColor.b) +
52  (targetColor.a / sourceColor.a) / 4f);
53  }
static bool Hydrogen.Validate.IsColorApproximatelySame ( this Color  sourceColor,
Color  targetColor 
)
static

Are the colors approximately equal?

Returns
true, if colors are approximately equal, false otherwise.
Parameters
sourceColorLeft Side Color
targetColorRight Side Color

Definition at line 61 of file Validate.cs.

62  {
63  return (Mathf.Approximately (sourceColor.r, targetColor.r) &&
64  Mathf.Approximately (sourceColor.g, targetColor.g) &&
65  Mathf.Approximately (sourceColor.b, targetColor.b) &&
66  Mathf.Approximately (sourceColor.a, targetColor.a));
67  }
static bool Hydrogen.Validate.IsDictionaryEqual< TKey, TValue > ( this IDictionary< TKey, TValue >  first,
IDictionary< TKey, TValue >  second 
)
static

Determines if dictionaries are equal.

Returns
true if the left side equals the right side; otherwise, false.
Parameters
firstLeft Side Dictionary.
secondRight Side Dictionary.
Template Parameters
TKeyThe 1st type parameter.
TValueThe 2nd type parameter.

Definition at line 77 of file Validate.cs.

79  {
80  if (first == second)
81  return true;
82  if ((first == null) || (second == null))
83  return false;
84  if (first.Count != second.Count)
85  return false;
86 
87  var comparer = EqualityComparer<TValue>.Default;
88 
89  foreach (KeyValuePair<TKey, TValue> kvp in first) {
90  TValue secondValue;
91  if (!second.TryGetValue (kvp.Key, out secondValue))
92  return false;
93  if (!comparer.Equals (kvp.Value, secondValue))
94  return false;
95  }
96  return true;
97  }
static bool Hydrogen.Validate.IsNumericType ( this object  value)
static

Determines whether the specified value is of numeric type.

Returns
true if value is a numeric type; otherwise, false.
Parameters
valueThe object to check.

Definition at line 107 of file Validate.cs.

108  {
109  return (value is byte ||
110  value is sbyte ||
111  value is short ||
112  value is ushort ||
113  value is int ||
114  value is uint ||
115  value is long ||
116  value is ulong ||
117  value is float ||
118  value is double ||
119  value is decimal);
120  }
static bool Hydrogen.Validate.IsPositive ( this object  value,
bool  zeroIsPositive = true 
)
static

Determines whether the specified value is positive.

Returns
true if the specified value is positive; otherwise, false.
Parameters
valueThe value.
zeroIsPositiveif set to true treats 0 as positive. Defaults to true.

Definition at line 130 of file Validate.cs.

131  {
132  switch (Type.GetTypeCode (value.GetType ())) {
133  case TypeCode.SByte:
134  return (zeroIsPositive ? (sbyte)value >= 0 : (sbyte)value > 0);
135  case TypeCode.Int16:
136  return (zeroIsPositive ? (short)value >= 0 : (short)value > 0);
137  case TypeCode.Int32:
138  return (zeroIsPositive ? (int)value >= 0 : (int)value > 0);
139  case TypeCode.Int64:
140  return (zeroIsPositive ? (long)value >= 0 : (long)value > 0);
141  case TypeCode.Single:
142  return (zeroIsPositive ? (float)value >= 0 : (float)value > 0);
143  case TypeCode.Double:
144  return (zeroIsPositive ? (double)value >= 0 : (double)value > 0);
145  case TypeCode.Decimal:
146  return (zeroIsPositive ? (decimal)value >= 0 : (decimal)value > 0);
147  case TypeCode.Byte:
148  return (zeroIsPositive || (byte)value > 0);
149  case TypeCode.UInt16:
150  return (zeroIsPositive || (ushort)value > 0);
151  case TypeCode.UInt32:
152  return (zeroIsPositive || (uint)value > 0);
153  case TypeCode.UInt64:
154  return (zeroIsPositive || (ulong)value > 0);
155  case TypeCode.Char:
156  return (zeroIsPositive || (char)value != '\0');
157  default:
158  return false;
159  }
160  }
static bool Hydrogen.Validate.ScrambledEquals< T > ( IEnumerable< T >  list1,
IEnumerable< T >  list2 
)
static

Determines whether the two scrambled enumerable objects are equal.

Returns
true, if they are equal, false otherwise.
Parameters
list1List1.
list2List2.
Template Parameters
TThe 1st type parameter.

Definition at line 169 of file Validate.cs.

170  {
171  var cnt = new Dictionary<T, int> ();
172 
173  foreach (T s in list1) {
174  if (cnt.ContainsKey (s)) {
175  cnt [s]++;
176  } else {
177  cnt.Add (s, 1);
178  }
179  }
180 
181  foreach (T s in list2) {
182  if (cnt.ContainsKey (s)) {
183  cnt [s]--;
184  } else {
185  return false;
186  }
187  }
188 
189  return cnt.Values.All (c => c == 0);
190  }