Creating and then accessing sets of possible pixel sequences (Markov?)

Keep thinking different ways to go about this but I don’t quite have the experience to fully wrap my head around.

I want to analyze an img to create (and then access/manipulate) basically a Markov model of the set of colors that can occur after each given color present in the source image. Anyone have any ideas for how to start? Thanks!!

for the theory