/// <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 ColorMap.ColorPair GetColorFromPairNumber(int pairNumber) { ColorMap obj = new ColorMap(); // The function supports only 1 based index. Pair numbers valid are from 1 to 25 int minorSize = obj.ColorMapMinor.Length; int majorSize = obj.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 ColorMap.ColorPair pair = new ColorMap.ColorPair() { majorColor = obj.ColorMapMajor[majorIndex], minorColor = obj.ColorMapMinor[minorIndex] }; // return the value return(pair); }
public override string ToString() { string manual = ""; for (int i = 1; i < 26; i++) { ColorMap.ColorPair pair = NumToColor.GetColorFromPairNumber(i); manual += String.Format("Pair Number - {0}: Colors - {1} \n", i, pair.ToString()); } return(manual); }
private static void Main(string[] args) { int pairNumber = 4; print manual = new print(); //Console.WriteLine(manual.ToString()); ColorMap.ColorPair testPair1 = NumToColor.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 = NumToColor.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 = NumToColor.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); ColorMap.ColorPair testPair2 = new ColorMap.ColorPair() { majorColor = Color.Yellow, minorColor = Color.Green }; pairNumber = ColorToNum.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}\n", testPair2, pairNumber); Debug.Assert(pairNumber == 18); testPair2 = new ColorMap.ColorPair() { majorColor = Color.Red, minorColor = Color.Blue }; pairNumber = ColorToNum.GetPairNumberFromColor(testPair2); Console.WriteLine("[In]Colors: {0}, [Out] PairNumber: {1}", testPair2, pairNumber); Debug.Assert(pairNumber == 6); }
/// <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(ColorMap.ColorPair pair) { ColorMap obj = new ColorMap(); // Find the major color in the array and get the index int majorIndex = -1; for (int i = 0; i < obj.ColorMapMajor.Length; i++) { if (obj.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 < obj.ColorMapMinor.Length; i++) { if (obj.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 * obj.ColorMapMinor.Length) + (minorIndex + 1)); }