For clarity, Nearly same colors of pixels belong to a category. Another nearly same colors of pixels belong to another category. The distinction depends on nearly same RGB values of pixels.
If the image is a GIF (or similar), you have large areas a single colour.
To find where these areas end, an edge detector is the logical choice.
In DSP terms, look at high pass filters to detect changes when you move between areas.
Note: jpeg and other picture formats can have areas where colour slowly changes. In this case, the edge between colours is not an easy choice.