In replaceColors()
, the special handling of just one tile is not justified.
There are two places that open-code tiling.
Process data before replication!
Depending on the relative size of image and texture, this may include multiplication/combination/convolution.