package be.abyx.aurora;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.SparseIntArray;

/**
 * This factory is a concrete AuroraFactory implementation that uses a simple CPU-based algorithm
 * to determine the most occurring colour.
 *
 * @author Pieter Verschaffelt
 */
public class DefaultAuroraFactory implements AuroraFactory {
    private Context context;
    private ColourPalette palette;
    private int accuracy = 50;

    public DefaultAuroraFactory(Context context) {
        this.context = context;
        ColourPaletteFactory paletteFactory = new ColourPaletteFactory(context);
        palette = paletteFactory.getDefaultColourPalette();
    }

    @Override
    public Bitmap createAuroraBasedUponDrawable(Bitmap input) {
        return null;
    }

    @Override
    public Bitmap createAuroraBasedUponDrawable(Bitmap input, AuroraType type) {
        return null;
    }

    @Override
    public Bitmap createAuroraBasedUponColour(int colour) {
        AuroraType type = new SimpleGradientAurora();
        return createAuroraBasedUponColour(colour, type);
    }

    @Override
    public Bitmap createAuroraBasedUponColour(int colour, AuroraType type) {
        return null;
    }

    private int determineDominantColour(Bitmap input) {
        int totalPixels = input.getWidth() * input.getHeight();

        int[] pixels = new int[input.getWidth() * input.getHeight()];
        input.getPixels(pixels, 0, input.getWidth(), 0, 0, input.getWidth(), input.getHeight());

        SparseIntArray colourMap = new SparseIntArray();

        for (int i = 0; i < totalPixels; i += accuracy) {
            Integer closestColour = palette.matchToClosestColour(pixels[i]);
            colourMap.put(closestColour, colourMap.get(closestColour, 0) + 1);
        }

        int largestValue = 0;
        int largestKey = 0;

        for (int i = 0; i < colourMap.size(); i++) {
            int key = colourMap.keyAt(i);
            int value = colourMap.get(key);

            if (value > largestValue) {
                largestValue = value;
                largestKey = key;
            }
        }

        return largestKey;
    }
}

@@ -756,32 +756,55 @@
 ble(Bitmap input
+, int width, int height
 ) %7B%0A        retu
@@ -902,32 +902,55 @@
  AuroraType type
+, int width, int height
 ) %7B%0A        retu
@@ -1035,16 +1035,39 @@
 t colour
+, int width, int height
 ) %7B%0A    
@@ -1171,16 +1171,31 @@
 ur, type
+, width, height
 );%0A    %7D
@@ -1283,16 +1283,39 @@
 ype type
+, int width, int height
 ) %7B%0A    

Fix parameters