I came up with a method that avoids photoshop because it seems every time I touch that everything breaks. My method is to take the graphic created with photoshop, and use it to punch a hole through a sample of the color that I want.
I opened that png, the one that was meant to be my icon, in Preview.app (not photoshop because photoshop was messing everything up) and then maximise contrast, made saturation as low as possible, and mess around with other settings, like maximising/minimising exposure, to make it completely black on a transparent background.
Then I cmd-A copied it all, drew a white rectangle, and pasted it down again. So now my original image is a completely black-on-white icon.
The color settings in Preview let you invert colours by moving the leftmost levels arrow to the right, and the rightmost to the left.
Now it should be white-on-black. Save that. This will be a mask.

Then I screenshotted the color I actually wanted. I used Preview's resize to resize that color to a 1000x1000px square (the size of my final image). Just a solid color. Screenshotting a solid block of color from the original png that you want to preserve the color of (first image of question) would have worked too.

Then I ran this command with imagemagick:
convert color.png mask.png -alpha off -compose copy_opacity -composite warningicon.png
It spat out exactly what I wanted, which was a png of the correct color that doesn't weirdly change colours when uploaded to discord.
