Unblend & Replace Colour

^ Earth, but instead of water being blue, it's purple/"orange"/red/green. Made with Replace Colour, source: NASA

^ Some colour light thing, but the black is replaced with transparency. Made with unblend, source: N/A. (top is input)

Unblend is a P5 thing that replaces a given colour with transparency, but not like a magic wand tool or a chroma key because it keeps on blending until there is no trace of the colour left.
Replace colour does the same thing, but it replaces the colour with a different colour instead of transparency. (brown is just dark orange ok, don't judge me)

Maths: Unblend

To unblend a pixel, it first calculates how far away the colour of the pixel is from the selected colour. To do this, it takes the maximum (on default settings, it is also the correct setting) of the absolute difference of the values for each of the colours.
dist = max(|pixel_red-selected_red|,|pixel_green-selected_blue|,|pixel_green-selected_blue|)
Now, to get the "original colour," it does some more maths to every channel:
output_red = (255*pixel_red+(-255+dist)*selected_red)/dist
output_green = (255*pixel_green+(-255+dist)*selected_green)/dist
output_blue = (255*pixel_blue+(-255+dist)*selected_blue)/dist
It then draws a pixel to the canvas with the colour and as alpha value the dist value and goes to the next pixel (until it's done).

Maths: Replace Colour

Replace Colour works very similarly to unblend, so the distance is calculated in the same way, but here the value is renamed because there are two colour pickers.
dist = max(|pixel_red-from_red|,|pixel_green-from_green|,|pixel_blue-from_blue|)
If this distance is 0, it sets the output pixel to the second colour picker
Else, to get the new colour it does some more maths to every channel:
output_red = (255*pixel_red+(-255+dist)*from_red+to_red*(255-dist))/255
output_green = (255*pixel_green+(-255+dist)*from_green+to_green*(255-dist))/255
output_blue = (255*pixel_blue+(-255+dist)*from_blue+to_blue*(255-dist))/255
It then draws a pixel to the canvas with the colour, just copies the alpha from the original image and goes to the next pixel (until it's done).

dark mode