Converting RGB images to a master palette

Hi, so I have a master palette of 256 colors. I want to convert a loaded RGB image to use this palette. Essentially, clamping the colors in the RGB image to the closest color it can find in the palette. I’ve tried for a few hours now to brute force it but it takes too long and produces vastly incorrect results. Does anyone have a solution?