I FINALLY FIGURED IT OUT.
By changing the R G and B value of each pixel to the corresponding index in the palette, the color gets indexed to the proper one. I don’t know if this is the right way to do palettes but it works so I’m not complaining.