<commit_before>/*
 * Copyright (c) 2018 大前良介(OHMAE Ryosuke)
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/MIT
 */

package net.mm2d.android.vmb.theme

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v4.math.MathUtils
import android.support.v4.view.ViewCompat
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.TextView
import net.mm2d.android.vmb.R
import net.mm2d.android.vmb.dialog.SelectThemeDialog
import net.mm2d.android.vmb.drawable.GridDrawable
import net.mm2d.android.vmb.settings.Settings
import java.util.*

/**
 * @author <a href="mailto:<EMAIL>">大前良介 (OHMAE Ryosuke)</a>
 */

class ThemeHelper(
        private val activity: AppCompatActivity,
        private val root: View,
        private val textView: TextView,
        private val icon: Drawable?) {
    private val themes = ArrayList<Theme>(listOf(
            Theme(activity.getString(R.string.theme_white_black), Color.WHITE, Color.BLACK),
            Theme(activity.getString(R.string.theme_black_white), Color.BLACK, Color.WHITE),
            Theme(activity.getString(R.string.theme_black_yellow), Color.BLACK, Color.YELLOW),
            Theme(activity.getString(R.string.theme_black_green), Color.BLACK, Color.GREEN)
    ))
    private val settings = Settings(activity)
    private val gridDrawable = GridDrawable(activity)

    /**
     * Preferenceを読みだして、テーマを設定する。
     */
    fun apply() {
        apply(settings.backgroundColor, settings.foregroundColor)
    }

    /**
     * テーマ設定。
     *
     * 背景色と文字色を設定するのみ。
     *
     * @param background 背景色
     * @param foreground 文字色
     */
    private fun apply(background: Int, foreground: Int) {
        gridDrawable.setColor(background)
        ViewCompat.setBackground(root, gridDrawable)
        root.invalidate()
        textView.setTextColor(foreground)
        icon?.let {
            DrawableCompat.setTint(DrawableCompat.wrap(it), getIconColor(background))
        }
    }

    fun select(theme: Theme) {
        settings.backgroundColor = theme.backgroundColor
        settings.foregroundColor = theme.foregroundColor
        apply()
    }

    /**
     * テーマ設定のダイアログを起動。
     */
    fun showDialog() {
        SelectThemeDialog.newInstance(themes)
                .show(activity.supportFragmentManager, "")
    }

    companion object {
        @ColorInt
        private fun getIconColor(@ColorInt background: Int): Int =
                if (getBrightness(background) < 128) Color.WHITE else Color.BLACK

        private fun getBrightness(@ColorInt color: Int): Int =
                getBrightness(Color.red(color), Color.green(color), Color.blue(color))

        private fun getBrightness(r: Int, g: Int, b: Int): Int =
                MathUtils.clamp((r * 0.299 + g * 0.587 + b * 0.114 + 0.5).toInt(), 0, 255)
    }
}
<commit_msg>refactor:<commit_after>/*
 * Copyright (c) 2018 大前良介(<NAME>)
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/MIT
 */

package net.mm2d.android.vmb.theme

import android.graphics.Color
import android.graphics.drawable.Drawable
import android.support.annotation.ColorInt
import android.support.v4.app.FragmentActivity
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v4.math.MathUtils
import android.support.v4.view.ViewCompat
import android.view.View
import android.widget.TextView
import net.mm2d.android.vmb.R
import net.mm2d.android.vmb.dialog.SelectThemeDialog
import net.mm2d.android.vmb.drawable.GridDrawable
import net.mm2d.android.vmb.settings.Settings
import java.util.*

/**
 * @author <a href="mailto:<EMAIL>">大前良介 (OH<NAME>)</a>
 */

class ThemeHelper(
        private val activity: FragmentActivity,
        private val root: View,
        private val textView: TextView,
        private val icon: Drawable?) {
    private val themes = ArrayList<Theme>(listOf(
            Theme(activity.getString(R.string.theme_white_black), Color.WHITE, Color.BLACK),
            Theme(activity.getString(R.string.theme_black_white), Color.BLACK, Color.WHITE),
            Theme(activity.getString(R.string.theme_black_yellow), Color.BLACK, Color.YELLOW),
            Theme(activity.getString(R.string.theme_black_green), Color.BLACK, Color.GREEN)
    ))
    private val settings = Settings(activity)
    private val gridDrawable = GridDrawable(activity)

    /**
     * Preferenceを読みだして、テーマを設定する。
     */
    fun apply() {
        apply(settings.backgroundColor, settings.foregroundColor)
    }

    /**
     * テーマ設定。
     *
     * 背景色と文字色を設定するのみ。
     *
     * @param background 背景色
     * @param foreground 文字色
     */
    private fun apply(background: Int, foreground: Int) {
        gridDrawable.setColor(background)
        ViewCompat.setBackground(root, gridDrawable)
        root.invalidate()
        textView.setTextColor(foreground)
        icon?.let {
            DrawableCompat.setTint(DrawableCompat.wrap(it), getIconColor(background))
        }
    }

    fun select(theme: Theme) {
        settings.backgroundColor = theme.backgroundColor
        settings.foregroundColor = theme.foregroundColor
        apply()
    }

    /**
     * テーマ設定のダイアログを起動。
     */
    fun showDialog() {
        SelectThemeDialog.newInstance(themes)
                .show(activity.supportFragmentManager, "")
    }

    companion object {
        @ColorInt
        private fun getIconColor(@ColorInt background: Int): Int =
                if (getBrightness(background) < 128) Color.WHITE else Color.BLACK

        private fun getBrightness(@ColorInt color: Int): Int =
                getBrightness(Color.red(color), Color.green(color), Color.blue(color))

        private fun getBrightness(r: Int, g: Int, b: Int): Int =
                MathUtils.clamp((r * 0.299 + g * 0.587 + b * 0.114 + 0.5).toInt(), 0, 255)
    }
}

{'fraction_non_alphanumeric': 0.11168614357262104, 'fraction_numerical': 0.011352253756260434, 'mean_word_length': 2.9336835193696653, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '1690942', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1885, 'n_words': 346}