/// <summary> /// Initialize the colors and return the color pair /// </summary> internal static ColorPair[] InitializeColors() { // Set of major and minor colors in order Color[] colorMapMajor = new Color[] { Color.White, Color.Red, Color.Black, Color.Yellow, Color.Violet }; Color[] colorMapMinor = new Color[] { Color.Blue, Color.Orange, Color.Green, Color.Brown, Color.SlateGray }; int majorLength = colorMapMajor.Length; int minorLength = colorMapMinor.Length; // Construct the color pair ColorPair[] colorPairs = new ColorPair[majorLength * minorLength]; int pairIndex = 0; for (int i = 0; i < majorLength; i++) { for (int j = 0; j < minorLength; j++) { ColorPair pair = new ColorPair() { MajorColor = colorMapMajor[i], MinorColor = colorMapMinor[j] }; colorPairs[pairIndex] = pair; pairIndex++; } } // return the color pairs return(colorPairs); }
/// <summary> /// Find the index of the pair in /// </summary> /// <param name=""></param> /// <returns></returns> private static int FindPairNumber(ColorPair pair) { // Find the 0 based index in color pair array int pairIndex = Array.FindIndex(colorPairs, item => ((item.MajorColor == pair.MajorColor) && (item.MinorColor == pair.MinorColor))); // Compute pair number and Return // (Note: +1 is because pair number is 1 based, not zero based index) return(pairIndex == -1 ? -1 : pairIndex + 1); }
/// <summary> /// Given the two colors the function returns the pair number corresponding to them /// </summary> /// <param name="pair">Color pair with major and minor color</param> /// <returns></returns> internal static int GetPairNumberFromColor(ColorPair pair) { // Find the 0 based index in color pair array int pairIndex = FindPairNumber(pair); // If colors can not be found throw an exception if (pairIndex == -1) { throw new ArgumentException( string.Format("Unknown Colors: {0}", pair.ToString())); } return(pairIndex); }
/// <summary> /// Given a pair number function returns the major and minor colors in that order /// </summary> /// <param name="pairNumber">Pair number of the color to be fetched</param> /// <returns></returns> internal static ColorPair GetColorFromPairNumber(int pairNumber) { // The function supports only 1 based index. Pair numbers valid are from 1 to 25 if (!IsValidPairNumber(pairNumber)) { throw new ArgumentOutOfRangeException( string.Format("Argument PairNumber:{0} is outside the allowed range", pairNumber)); } // Construct the return val from the values, note pair number is 1 based ColorPair pair = new ColorPair(colorPairs[pairNumber - 1]); // return the value return(pair); }
/// <summary> /// Test for correct data /// </summary> internal void PositiveTestGetColor() { // Positive case 1 int pairNumber = 5; ColorPair testPair1 = ColorCodeRegistry.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); // Positive case 2 pairNumber = 24; testPair1 = ColorCodeRegistry.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.Brown); }
/// <summary> /// Test for incorrect data /// </summary> private void NegativeTestGetColor() { // Negatice test case with wrong pair number as input to simulate error condition int pairNumber = 0; bool errorConditionHandled = false; try { ColorPair testPair1 = ColorCodeRegistry.GetColorFromPairNumber(pairNumber); Console.WriteLine("[In]Pair Number: {0},[Out] Colors: {1}\n", pairNumber, testPair1); } catch (Exception) { errorConditionHandled = true; Console.WriteLine("Invalid Pair Number : {0}, Error Condition handled\n", pairNumber); } Debug.Assert(errorConditionHandled); }
/// <summary> /// Test for incorrect data /// </summary> private void NegativeTestGetPair() { // negative test case with wrong color as input to simulate error condition bool errorConditionHandled = false; ColorPair testPair2 = new ColorPair() { MajorColor = Color.Red, MinorColor = Color.Maroon }; try { int pairNumber = ColorCodeRegistry.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}\n", testPair2, pairNumber); } catch (Exception) { errorConditionHandled = true; Console.WriteLine("Invalid Color : {0}, Error Condition handled\n", testPair2); } Debug.Assert(errorConditionHandled); }
/// <summary> /// Test for correct data /// </summary> private void PositiveTestGetPair() { // Positive case 1 ColorPair testPair2 = new ColorPair() { MajorColor = Color.Yellow, MinorColor = Color.Blue }; int pairNumber = ColorCodeRegistry.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}\n", testPair2, pairNumber); Debug.Assert(pairNumber == 16); // Positive case 2 testPair2 = new ColorPair() { MajorColor = Color.Red, MinorColor = Color.Green }; pairNumber = ColorCodeRegistry.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}\n", testPair2, pairNumber); Debug.Assert(pairNumber == 8); }
/// <summary> /// Copy ctor /// </summary> /// <param name="pair"></param> public ColorPair(ColorPair pair) { MajorColor = pair.MajorColor; MinorColor = pair.MinorColor; }