This is from one of Kant’s pre-Critical works, published in 1764, Observations on the Feeling of the Beautiful and Sublime:
A profound feeling for the beauty and dignity of human nature and a firmness and determination of the mind to refer all one’s actions to this as to a universal ground is earnest, and does not at all join with a changeable gaiety nor with the inconstancy of a frivolous person. It even approaches melancholy, a gentle and noble feeling so far as it is grounded upon the awe that a hard-pressed soul feels when, full of some great purpose, he sees the danger he will have to overcome, and has before his eyes the difficult but great victory of self-conquest. Thus genuine virtue based upon principles has something about it which seems to harmonize most with the melancholy frame of mind in the moderated understanding.
— Beobachtungen über das Gefühl des Schönen und Erhabenen (trans. Goldthwait) p. 62-3.