Can anyone explain to me how color swatches work when changing images? And where it is done? I want to know where in code it change images.
Magento uses attributes as swatches. That means that "color" is just the label you have for your swatch, but it could really be anything.
The basic idea is to enable this option under
System > Configuration > Catalog > Configurable Swatches.
The next step is to create the configurable product using the attribute set you want. Then, under Images tab, you add new images with the same label used in the attributes page.
Here is a good tutorial with images and more details: https://sherodesigns.com/tutorial-configurable-swatches-in-magento/
About the code, you can find it under
app\code\core\Mage\ConfigurableSwatches\Block\Catalog\Layer\State\Swatch.php but it is not recommended to change anything there.