@Override
	protected ArrayList<ColorPatch> doInBackground() throws Exception {

		ColorPatch sum = new ColorPatch();
		
		for (ColorPatch patch : patches) {
			sum.add(patch.getColor(), false);
		}
		
		sum.recalculate();
		
		int i=0, n=patches.size();
		for (ColorPatch patch : patches) {
			double score = 0;
			Color color = patch.getColor();
			score += (1d/color.getTemperature())*params.getTemperature();
			score += color.getBrightness()*params.getBrightness();
			score += color.getSaturation()*params.getSaturation();
			score += (1-1d/color.distance(sum.getColor(), Color.RGB_DISTANCE))*params.getDifference();
			score += (patch.getSize()/patch.getMaxSize())*params.getPatchSize();
			
			patch.setScore(score);
			i++;
			
			publish((double)i/n/2);
		}
		
		final int estimatedComparitions = (int) (patches.size()*Math.log(patches.size()));
		
		Collections.sort(patches, new Comparator<ColorPatch>() {
			int comparitions=0;
			
			@Override
			public int compare(ColorPatch o1, ColorPatch o2) {
				comparitions++;
				publish(0.5+(double)(comparitions/estimatedComparitions)/2);
				return -o1.compareTo(o2);
			}
		});
		
		return patches;
	}