1. An image forming apparatus comprising: an image former that forms a pattern for positional deviation correction on an image carrier; a detector that detects the pattern for positional deviation correction; a controller that controls a positional deviation correction process in accordance with a result of detection of the detector; a receiver that receives a color mode selected from among a plurality of color modes, each having a different number of colors used for forming the pattern; and circuitry configured to calculate a total pattern length so that a total length of the pattern for positional deviation correction in a sub-scanning direction is equal in any of the color modes, wherein the image former forms the pattern for positional deviation correction including the number of colors corresponding to the selected color mode so that the total length of the pattern for positional deviation correction is equal to the calculated total pattern length.