The invention relates to a process for anodic oxidation colorization of a magnesium alloy or an aluminum alloy surface and a formula of an electroplating solution thereof, belonging to the technical field of electroplating. The process is as follows: the pulse anodic oxidation method and the treatment solution composed of potassium hydroxide, aniline and potassium oxalate are used to colorize the zinc-coated layer on the surface of the magnesium alloy or aluminum alloy. First, the magnesium alloy or aluminum alloy substrate as the anode is pretreated. That is, degreasing, pickling, alkali washing, activation, zinc dipping, and then preparing a treatment solution, and then passing an electric current in the treatment solution to generate electric energy, so that a dense color film is formed on the surface of the magnesium alloy or aluminum alloy substrate of the pretreated anode. The components of the treatment solution and their weight percentages are: 20%-45% of potassium hydroxide, 30%-40% of aniline, and 20%-45% of potassium oxalate. The invention has simple process and basically no environmental pollution, and is suitable for the colorization of the galvanized layer of magnesium and aluminum alloy, and is also used for the colorization of the galvanized layer. The treatment solution has good stability, strong dispersing ability and deep plating ability, the obtained treatment film layer has good uniformity, and is excellently combined with the substrate, and the obtained dense oxide film in the form of colorful patterns is obtained.