From the Super Mario Wiki
“Luigi, I found this cap in front of the castle a little while ago. You must have dropped it. If you want to become a full-fledged hero like your brother, you can't be so careless!”
Luigi's hat color was taken from the Shellcreeper's palette to distinguish Mario from Luigi. The hat was made green with a green "L" emblem, but it was white on Super Mario Bros. and Super Mario Bros: The Lost Levels.
In Super Mario Bros. 2, the tip of the cap had been changed to black instead of his usual green. This happened for Mario as well. However, after this game, Luigi's cap went back to having a green tip.
In Super Mario 64 DS, Luigi's hat was used to shape-shift into Luigi if the player was either Mario, Wario, or Yoshi. In the same game, Luigi is affected the same way as Mario and Wario if their hats are lost. Luigi's defense and attack sharply decline, while his hat is absent from his head. If the player gets the hat back, Luigi's attack and defense revert to normal. If Luigi is locked, it only appears in Big Boo's Haunt for any star except #1.
In the Mario & Luigi series, the "L" on Luigi's cap is black. Mario's cap was changed in the same way, Starting with Dream Team, they are the usual green and red.