Browse - Programming Tips - How can I get the average of an ArrayList of colors?

Date: 2013sep27
Language: Java

Q.  How can I get the average of an ArrayList of colors?

A.

import java.awt.Color; import java.util.ArrayList; Color average(final ArrayList<Color> a) { int r = 0, g = 0, b = 0; for (final Color c : a) { r += c.getRed(); g += c.getGreen(); b += c.getBlue(); } final int n = a.size(); return new Color(r / n, g / n, b / n); }