This is an incarnation of a random walk.
The start position the upper-left for the image and a random color random for the cube.
Both the path in the color cube and the path in the image are random walks, with a twist (actually a straight). All diagonally connected pixels are a valid direction. That's 8 directions for the image walk and 26 directions for the color walk. Once a direction is chosen, it keeps going straight. Only when it can't go straight anymore it chooses an available random direction at random. When it hits a dead end and can't turn anywhere, the path it traced is traced in reverse until a previously visited point is found for which a valid direction exists.