I’ve noticed that many paintings such as those of Diego Velasquez depict 16th and 17th century Spanish men wearing black caps, doublets, hose, and sometimes coats.
This doesn’t seem practical in the Spanish sun. Were black clothes just for evening wear or was it some kind of flex to show that you stayed inside all day?
Velázquez may not be the best source for generalisations. Bear in mind that he was the Court painter, a man whose commissions mostly came from the royal house or its entourage, which is to say the richest people in the land.
Why does this matter? Because black was a sign of luxury and wealth, and it had been for decades by the time Velázquez became court painter. The natural dye from which pure black was obtained was called "palo de Campeche" (stick from Campeche) and was outrageously expensive. It produced a tremendously beautiful colour, an iridiscent shade of black called "negro ala de cuervo" (crow's wing black). The colour is rather self-explanatory for anyone familiar with crows, which have a rather bright sort of black plumage, something stunning.
Of course, not everyone could afford the palo de Campeche, so they would try using other tinctures that produced very dark shades of blue or grey that could kinda pass off as black without being obscenely pricey.
Other colours were used indeed, and one can see them even Velázquez's paintings, like the beautiful portait of Philip IV in Fraga, in which he wears an absolutely fabulous red doublet. "The siege of Breda", "The water-seller of Seville", or "Old woman frying eggs" also show people not necessarily wearing black clothes. If you check out other painters like Zurbarán, Murillo, Martínez del Mazo, or Juan Bautista Maíno you may see people wearing many different colours.
So, it was one heck of a flex, but not the one you are thinking of. The biggest flex in terms of wealth, though, was the infanta Catherine Michelle (daughter of Philip II) and her astonishing inventory of jewels, some of which you can see in her portraits
This is however, not the whole picture (pun absolutely intended). Palo de Campeche was criminally expensive, but so were other dyes like cochineal, which produced a natural pure red. Other elements such as fashion, aesthetics, philosophy, and virility have to be taken into account.
Black colours had started to become fashionable in Burgundy in the second hald of the 15th century, and with Charles V of the Holy Roman Empire, I of Castile, Aragon, etc those styles came into the Spanish territories, where they became the fashionable attire for a long time, more or less until the arrival of king Philip V in the early 18th century, who brought new French fashions with him and his entourage.
Black was a colour associated with dignity, gravitas, and authority, as clearly attested by Baldassarre Castiglione in "Il cortegiano", and later commented as well by Lucas Gracián Dantisco in his Galateo Español. This is what the Italian nobleman had to say: "Black robes have more grace and authority than any other colour, for it brings a certain beauty and gallantry, which is very à propos for the service of arms". In that phrase we see black not only associated to grace, but also to gallantry and manliness, a highly sought after quality.
The use of black dyes and dark colours came to illustrate a perfect joinder of aesthetics that combined martial looks, economic power, and religiosity as well. During the second half of the 16th century, the white robes of the order of Santiago start to fade away from portraits, being replaced for the cross being stitched or embroidered into the more fashionable clothes that military people wore, or even painted over the cuirasses. There are instances of the white habit of Santiago, like the portrait of Julián Romero by El Greco, but the opposite is far more common.
With the continuous use of black clothes, Philip IV tries to emulate his grandfather Philip II, bringing forward an image of wealth, militarism, and religiousness. This type of fashion, along with the simplification of the "lechuguillas" (see Cervantes' portrait) being substituted by the "walloon collars" try to transmit the values of the time, and it eventually percolated to the society at large as long as they could afford it.