// https://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering public static void FloydSteinberg( Color32[] pixels, System.Func <Color32, Color32> encode, int width, int height) { ColorInt e, e7, e5, e3, e1; e = e7 = e5 = e3 = e1 = new ColorInt(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { var c0 = pixels[(y * width) + x]; var c1 = encode(c0); pixels[(y * width) + x] = c1; e.AddDiff(ref c0, ref c1); e7.SetMulDiv(ref e, 7, 16); e5.SetMulDiv(ref e, 5, 16); e3.SetMulDiv(ref e, 3, 16); e1.SetMulDiv(ref e, 1, 16); if ((x + 1) < width) { AddColor(ref pixels[(y * width) + (x + 1)], ref e7); if ((y + 1) < height) { AddColor(ref pixels[((y + 1) * width) + (x + 1)], ref e1); } } if ((y + 1) < height) { if ((x - 1) >= 0) { AddColor(ref pixels[((y + 1) * width) + (x - 1)], ref e3); } AddColor(ref pixels[((y + 1) * width) + x], ref e5); } e.Sub(ref e7); e.Sub(ref e5); e.Sub(ref e3); e.Sub(ref e1); } } }