com.richtodd.android.quiltdesign.block.Theme.java Source code

Java tutorial

Introduction

Here is the source code for com.richtodd.android.quiltdesign.block.Theme.java

Source

/* Copyright (c) 2013 Richard G. Todd.
 * Licensed under the terms of the GNU General Public License (GPL) Version 3.0.
 */

package com.richtodd.android.quiltdesign.block;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Parcel;
import android.os.Parcelable;

public class Theme implements Parcelable {

    //
    // Fields
    //

    private ArrayList<Swatch> m_swatches = new ArrayList<Swatch>();

    private static float[] m_hsv = new float[3];

    //
    // Constructors
    //

    public Theme(int swatchCount) {
        int existingCount = m_swatches.size();
        for (int idx = existingCount; idx < swatchCount; ++idx) {
            Swatch swatch = new Swatch();
            swatch.setColor(0);

            m_swatches.add(swatch);
        }
    }

    public Theme(Parcel in) {
        int swatchCount = in.readInt();
        for (int idx = 0; idx < swatchCount; ++idx) {
            int color = in.readInt();
            m_swatches.get(idx).setColor(color);
        }
    }

    public static Theme createColors(int swatchCount, float saturation, float value) {
        m_hsv[1] = saturation;
        m_hsv[2] = value;

        Theme theme = new Theme(swatchCount);
        for (int idx = 0; idx < swatchCount; ++idx) {
            m_hsv[0] = ColorFunctions.AngleToHue(360f * idx / swatchCount);
            int color = Color.HSVToColor(m_hsv);

            theme.getSwatches().get(idx).setColor(color);
        }

        return theme;
    }

    public static Theme createShades(int swatchCount, float hue, float value) {
        m_hsv[0] = hue;
        m_hsv[2] = value;

        Theme theme = new Theme(swatchCount);

        for (int idx = 0; idx < swatchCount; ++idx) {
            m_hsv[1] = 1f - ((float) (idx) / (float) swatchCount);
            int color = Color.HSVToColor(m_hsv);

            theme.getSwatches().get(idx).setColor(color);
        }

        return theme;
    }

    public static Theme createTints(int swatchCount, float hue, float saturation) {
        m_hsv[0] = hue;
        m_hsv[1] = saturation;

        Theme theme = new Theme(swatchCount);

        for (int idx = 0; idx < swatchCount; ++idx) {
            m_hsv[2] = 1f - ((float) (idx) / (float) swatchCount);
            int color = Color.HSVToColor(m_hsv);

            theme.getSwatches().get(idx).setColor(color);
        }

        return theme;
    }

    //
    // Public
    //

    public List<Swatch> getSwatches() {
        return m_swatches;
    }

    public Swatch getSwatch(int color) {
        for (Swatch swatch : m_swatches) {
            if (swatch.getColor() == color) {
                return swatch;
            }
        }

        return null;
    }

    public Bitmap createBitmap(int width, int height) {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        draw(canvas, width, height);
        return bitmap;
    }

    public void sortSwatches() {
        Collections.sort(m_swatches);
    }

    //
    // JSON
    //

    JSONObject createJSONObject() throws JSONException {

        JSONArray jsonSwatches = new JSONArray();
        for (Swatch swatch : m_swatches) {
            jsonSwatches.put(swatch.createJSONObject());
        }

        JSONObject jsonTheme = new JSONObject();
        jsonTheme.put("objectType", "theme");
        jsonTheme.put("swatches", jsonSwatches);

        return jsonTheme;
    }

    static final Theme createFromJSONObject(JSONObject jsonObject) throws JSONException {

        if (jsonObject.has("swatches")) {
            JSONArray jsonSwatches = jsonObject.getJSONArray("swatches");

            int swatchCount = jsonSwatches.length();
            Theme theme = new Theme(swatchCount);

            for (int idx = 0; idx < swatchCount; ++idx) {
                JSONObject jsonSwatch = jsonSwatches.getJSONObject(idx);
                theme.m_swatches.set(idx, Swatch.createFromJSONObject(jsonSwatch));
            }

            return theme;
        }

        return null;
    }

    //
    // Parcelable
    //

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(m_swatches.size());
        for (Swatch swatch : m_swatches) {
            out.writeInt(swatch.getColor());
        }
    }

    public static final Parcelable.Creator<Theme> CREATOR = new Creator<Theme>() {

        @Override
        public Theme[] newArray(int size) {
            return new Theme[size];
        }

        @Override
        public Theme createFromParcel(Parcel in) {
            return new Theme(in);
        }
    };

    //
    // Private
    //

    private void draw(Canvas canvas, int width, int height) {
        canvas.drawColor(Color.WHITE);

        Paint swatchPaint = new Paint();
        swatchPaint.setStyle(Style.FILL);

        int idx = 0;
        int left = 0;
        int right = 0;
        for (Swatch swatch : m_swatches) {
            idx += 1;
            left = right;
            right = width * idx / m_swatches.size();

            swatchPaint.setColor(swatch.getColor());
            canvas.drawRect(left, 0, right, height, swatchPaint);
        }
    }
}