internal static TelColorPair.ColorPair GetColorFromPairNumber(int pairNumber) { // The function supports only 1 based index. Pair numbers valid are from 1 to 25 int minorSize = TelColorPair.colorMapMinor.Length; int majorSize = TelColorPair.colorMapMajor.Length; if (pairNumber < 1 || pairNumber > minorSize * majorSize) { throw new ArgumentOutOfRangeException( string.Format("Argument PairNumber:{0} is outside the allowed range", pairNumber)); } // Find index of major and minor color from pair number int zeroBasedPairNumber = pairNumber - 1; int majorIndex = zeroBasedPairNumber / minorSize; int minorIndex = zeroBasedPairNumber % minorSize; // Construct the return val from the arrays TelColorPair.ColorPair pair = new TelColorPair.ColorPair() { majorColor = TelColorPair.colorMapMajor[majorIndex], minorColor = TelColorPair.colorMapMinor[minorIndex] }; // return the value return(pair); }
public static void Main(string[] args) { int minorSize = TelColorPair.colorMapMajor.Length; int majorSize = TelColorPair.colorMapMajor.Length; int pairNumber = 4; TelColorPair.ColorPair testPair1 = GetNumber.GetColorFromPairNumber(pairNumber); Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1); Debug.Assert(testPair1.majorColor == Color.White); Debug.Assert(testPair1.minorColor == Color.Brown); pairNumber = 5; testPair1 = GetNumber.GetColorFromPairNumber(pairNumber); Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1); Debug.Assert(testPair1.majorColor == Color.White); Debug.Assert(testPair1.minorColor == Color.SlateGray); pairNumber = 23; testPair1 = GetNumber.GetColorFromPairNumber(pairNumber); Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1); Debug.Assert(testPair1.majorColor == Color.Violet); Debug.Assert(testPair1.minorColor == Color.Green); TelColorPair.ColorPair testPair2 = new TelColorPair.ColorPair() { majorColor = Color.Yellow, minorColor = Color.Green }; pairNumber = GetPair.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}\n", testPair2, pairNumber); Debug.Assert(pairNumber == 18); testPair2 = new TelColorPair.ColorPair() { majorColor = Color.Red, minorColor = Color.Blue }; pairNumber = GetPair.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}", testPair2, pairNumber); Debug.Assert(pairNumber == 6); }
internal static int GetPairNumberFromColor(TelColorPair.ColorPair pair) { // Find the major color in the array and get the index int majorIndex = -1; for (int i = 0; i < TelColorPair.colorMapMajor.Length; i++) { if (TelColorPair.colorMapMajor[i] == pair.majorColor) { majorIndex = i; break; } } // Find the minor color in the array and get the index int minorIndex = -1; for (int i = 0; i < TelColorPair.colorMapMinor.Length; i++) { if (TelColorPair.colorMapMinor[i] == pair.minorColor) { minorIndex = i; break; } } // If colors can not be found throw an exception if (majorIndex == -1 || minorIndex == -1) { throw new ArgumentException( string.Format("Unknown Colors: {0}", pair.ToString())); } // Compute pair number and Return // (Note: +1 in compute is because pair number is 1 based, not zero) return((majorIndex * TelColorPair.colorMapMinor.Length) + (minorIndex + 1)); }