class SnesPal { public int maxColors; public List colors; // =================================================== public static int Distance(SnesPal a, SnesPal b) { int maxColors = a.maxColors; int numSame = 0; int numDiff = 0; // ------------------------------ if(a.maxColors != b.maxColors) { throw new Exception("Merging different sized palettes!"); } // add the number of colors in a that are not in b foreach(Color c in a.colors) { if( !b.colors.Contains(c) ) { numDiff += 1; } else { numSame += 1; } } // add the number of colors in b that are not in a foreach(Color c in b.colors) { if( !a.colors.Contains(c) ) { numDiff += 1; } } if( (numDiff + numSame) > maxColors ) { numDiff = maxColors; } return numDiff; } // =================================================== public static SnesPal Merge(SnesPal a, SnesPal b) { int maxColors = a.maxColors; SnesPal mergePal = new SnesPal(); // ---------------------------------- if( Distance(a,b) >= maxColors ) { throw new Exception("Palettes are too large to merge"); } // populate the merge palette with all the colors of a foreach(Color c in a.colors) { mergePal.colors.Add(c); } // now add all the colors that are in b but not in a. foreach(Color c in b.colors) { if( !mergePal.colors.Contains(c) ) { mergePal.colors.Add(c); } } return mergePal; } }