But the real reason for the colorful uniforms was the fact of the smoke on the battlefield.
The gunpowder back then was not smokeless, so the bright colors were needed so soldiers wouldn't shoot their own side by mistake.
In the American Civil War the uniforms were not that clearly defined and as a result friendly fire deaths were common.
Firing a black powder musket produces a massive great billow of white smoke. Furthermore, soldiers with muzzle loaders had to fight standing up, because the only way to load a weapon quickly from the front end is to use gravity to carry the powder and bullet down the barrel. So there was no way for pre-modern soldiers to both hide and fight. They were going to be standing up in full view making smoke, anyway. A red coat was the least of their worries. At least the enemy weapons weren't very accurate. If you were going to get shot, being noticed was not going to be the bottleneck stage in the process.
On the other hand friendly fire was and still is a terribly serious concern. Your friends are usually closer to you than the enemy, and the enemy isn't always even there but you are almost always in contact with your friends. Several famous Napoleonic battles had nasty episodes of battalions firing volleys at their own allies, and I suspect it was an issue in most battles of that era. In the modern empty battlefield of smokeless powder and breech-loading repeaters accurate to long range, it's harder to tell just who has shot whom; but I've heard that when the US Department of Veterans' Affairs collected data on bullets that had been lodged in veteran's bodies from WW2 and eventually removed, they found that
most of them were American bullets.
Clearly distinctive uniforms were a much better idea than camouflage up until the later 19th century, when technology changes tipped the tactical balance in favor of concealment. One of the tragedies of the US Civil War was that it was mostly fought with muzzle-loading rifles that were accurate to long range but still forced their users to stand up to load. The longer range evidently made duller uniforms worthwhile for both sides, but casualty rates were still terribly high even as wars go, and this shows how little dull uniforms help when your weaponry prevents you from really hiding. The next major war (the Franco-Prussian) was fought with breech loaders, and that changed a lot.
The French army actually went into WWI with red trousers (under dull blue coats that weren't too bad for visibility), but all the other major armies had long since switched to drabber colors at that point. Even modern disruptive pattern isn't necessarily any better, across a range of terrain, than solid khaki or feldgrau or olive drab.