Changed up palette sorting to adapt to color and grayscale images