Dave's Brain

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); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.