Color harmonization is an artistic technique to adjust a set of colors in order to enhance their visual harmony so that they are aesthetically pleasing in terms of human visual perception. We present a new color harmonization method that treats the harmonization as a function optimization. For a given image, we derive a cost function based on the observation that pixels in a small window that have similar unharmonic hues should be harmonized with similar harmonic hues. By minimizing the cost function, we get a harmonized image in which the spatial coherence is preserved. A new matching function is proposed to select the best matching harmonic schemes, and a new component-based preharmonization strategy is proposed to preserve the hue distribution of the harmonized images. Our approach overcomes several shortcomings of the existing color harmonization methods. We test our algorithm with a variety of images to demonstrate the effectiveness of our approach.