<commit_before>package com.thebluealliance.spectrum;

import com.thebluealliance.spectrum.internal.ColorItem;
import com.thebluealliance.spectrum.internal.SelectedColorChangedEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * General-purpose class that displays colors in a grid.
 */
public class ColorPaletteView extends LinearLayout {

    private static final int DEFAULT_COLUMN_COUNT = 4;

    private int mColorItemDimension;
    private int mColorItemMargin;
    private @ColorInt int[] mColors;
    private @ColorInt int mSelectedColor;
    private OnColorSelectedListener mListener;

    private int mNumColumns = 2;
    private int mOldNumColumns = -1;
    private boolean mViewInitialized = false;

    private EventBus mEventBus;

    public ColorPaletteView(Context context) {
        super(context);
        init();
    }

    public ColorPaletteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mEventBus = new EventBus();
        mEventBus.register(this);

        mColorItemDimension = getResources().getDimensionPixelSize(R.dimen.color_item_small);
        mColorItemMargin = getResources().getDimensionPixelSize(R.dimen.color_item_margins_small);

        setOrientation(LinearLayout.VERTICAL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width, height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
            mNumColumns = computeColumnCount(widthSize - (getPaddingLeft() + getPaddingRight()));
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = widthSize;
            mNumColumns = computeColumnCount(widthSize - (getPaddingLeft() + getPaddingRight()));
        } else {
            width = computeWidthForNumColumns(DEFAULT_COLUMN_COUNT);
            mNumColumns = DEFAULT_COLUMN_COUNT;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(computeHeight(mNumColumns), heightSize);
        } else {
            height = computeHeight(mNumColumns);
        }

        createPaletteView();

        Log.d("spectrum", "width: " + width + "; height: " + height + "; column count: " + mNumColumns);

        super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    private int computeColumnCount(int maxWidth) {
        int numColumns = 0;
        while (((numColumns + 1) * mColorItemDimension) + ((numColumns + 1) * 2 * mColorItemMargin) <= maxWidth) {
            numColumns++;
        }
        return numColumns;
    }

    private int computeWidthForNumColumns(int columnCount) {
        return columnCount * (mColorItemDimension + 2 * mColorItemMargin);
    }

    private int computeHeight(int columnCount) {
        int rowCount = mColors.length / columnCount;
        if (mColors.length % columnCount != 0) {
            rowCount++;
        }
        return rowCount * (mColorItemDimension + 2 * mColorItemMargin);<commit_msg>Fixed bug with palette caused by not accounting for padding when measuring the view<commit_after>package com.thebluealliance.spectrum;

import com.thebluealliance.spectrum.internal.ColorItem;
import com.thebluealliance.spectrum.internal.SelectedColorChangedEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * General-purpose class that displays colors in a grid.
 */
public class ColorPaletteView extends LinearLayout {

    private static final int DEFAULT_COLUMN_COUNT = 4;

    private int mColorItemDimension;
    private int mColorItemMargin;
    private @ColorInt int[] mColors;
    private @ColorInt int mSelectedColor;
    private OnColorSelectedListener mListener;

    private int mNumColumns = 2;
    private int mOldNumColumns = -1;
    private boolean mViewInitialized = false;

    private EventBus mEventBus;

    public ColorPaletteView(Context context) {
        super(context);
        init();
    }

    public ColorPaletteView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mEventBus = new EventBus();
        mEventBus.register(this);

        mColorItemDimension = getResources().getDimensionPixelSize(R.dimen.color_item_small);
        mColorItemMargin = getResources().getDimensionPixelSize(R.dimen.color_item_margins_small);

        setOrientation(LinearLayout.VERTICAL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width, height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
            mNumColumns = computeColumnCount(widthSize - (getPaddingLeft() + getPaddingRight()));
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = widthSize;
            mNumColumns = computeColumnCount(widthSize - (getPaddingLeft() + getPaddingRight()));
        } else {
            width = computeWidthForNumColumns(DEFAULT_COLUMN_COUNT) + getPaddingLeft() + getPaddingRight();
            mNumColumns = DEFAULT_COLUMN_COUNT;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            height = Math.min(computeHeight(mNumColumns) + getPaddingBottom() + getPaddingTop(), heightSize);
        } else {
            height = computeHeight(mNumColumns) + getPaddingTop() + getPaddingBottom();
        }

        createPaletteView();

        Log.d("spectrum", "width: " + width + "; height: " + height + "; column count: " + mNumColumns);

        super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }

    private int computeColumnCount(int maxWidth) {
        int numColumns = 0;
        while (((numColumns + 1) * mColorItemDimension) + ((numColumns + 1) * 2 * mColorItemMargin) <= maxWidth) {
            numColumns++;
        }
        return numColumns;
    }

    private int computeWidthForNumColumns(int columnCount) {
        return columnCount * (mColorItemDimension + 2 * mColorItemMargin);
    }

    private int computeHeight(int columnCount) {
        int rowCount = mColors.length / columnCount;
        if (mColors.length % columnCount != 0) {
            rowCount++;
        }
        return rowCount * (mColorItemDimension + 2 * mColorItemMargin);
{'fraction_non_alphanumeric': 0.09273410180028493, 'fraction_numerical': 0.0025903380391141045, 'mean_word_length': 3.2013057671381935, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '377557', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2095, 'n_words': 466}