all files / src/ config.js

99.02% Statements 101/102
100% Branches 32/32
100% Functions 20/20
97.37% Lines 37/38
26 statements, 1 function, 19 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                  80667×           80667×         80667×           80667×                                           160630× 321260× 160630×     160630× 160630× 160630× 321259× 321259×       160630× 321260× 321260×     321260× 321260× 321260×       321260× 321260×     321259×             160629× 160629×       160629×         321258× 321258×     160629× 321258× 321258× 160629× 481887× 481887×   160629×        
/**
 * 日の満ち欠けの反映処理の設定を提供します。
 */
class Config {
    /**
     * インスタンスを初期化します。
     */
    constructor() {
        /**
         * 日の満ち欠けを表現する日時。
         */
        this.now = new Date();
 
        /**
         * 日の満ち欠けが要素に与える影響度。0 は朝でも夜でも影響がないことを意味します。
         * 1 は日の満ち欠けのみが色を決め、要素本来の色が調整後に影響しないことを意味します。
         */
        this.impact = 0.1;
 
        /**
         * 要素への最大反射回数。1 を指定した場合は最初の実行のみ色への反映が適用されます。
         */
        this.numberOfLimitReflection = 1;
 
        /**
         * 1日の明るさの定義。それぞれの時間帯の明るさを表します。プロパティは時刻を表す 
         * hh:mm:ss 形式の文字列で、値は明るさを示すヘックスコードです。
         */
        this.brightness = {
            "00:00:00": "#000000",
            "04:00:00": "#000000",
            "05:00:00": "#474747",
            "06:00:00": "#8c8c8c",
            "07:00:00": "#b8b8b8",
            "08:00:00": "#f2f2f2",
            "09:00:00": "#f2f2f2",
            "18:00:00": "#b8b8b8",
            "19:00:00": "#8c8c8c",
            "20:00:00": "#474747",
            "21:00:00": "#000000",
            "23:59:59": "#000000",
        }
    }
 
    /**
     * 現在時刻のテーマ色を取得します。
     * @returns {Array<Number>} rgbを返します。
     */
    getThemeColor() {
        // 基準となる日付を生成
        const times = Object.keys(this.brightness);
        const format = n => ("00" + n).slice(-2);
        const date = `${this.now.getFullYear()}/${format(this.now.getMonth() + 1)}/${format(this.now.getDate())}`;
 
        // 色情報を抽出する処理
        const core = require("hcv-core");
        const extractor = core.extractor;
        const getColor = expression => {
            const color = extractor.extract(expression);
            return [ color.r, color.g, color.b ];
        }
        
        // 設定された季節
        const distances = [];
        for(const time of times) {
            const themeTime = new Date(`${date} ${time}`);
 
            // 秒数差の計算
            const getSecondDifference = (date1, date2) => {
                const secondMilliseconds = 1000;
                return Math.abs(Math.ceil((date1 - date2) / secondMilliseconds));
            }
 
            // 対象時間の設定色があれば、そちらを利用
            const distance = getSecondDifference(themeTime, this.now);
            if (distance === 0) return getColor(this.brightness[time]);
 
            // 時間毎の距離を記録
            distances.push({
                time: time,
                distance: distance
            });
        }
 
        // 秒数差で昇順ソート
        distances.sort((a, b) => {
            return a.distance - b.distance;
        });
 
        // 最も近い時間帯2つ以外を排除
        while (distances.length > 2) {
            distances.pop();
        };
 
        // 全体を100として比率を計算
        const amount = [ 0, 1 ].map(x => distances[x].distance).reduce((a, b) => a + b, 0);
        [ 0, 1 ].forEach(x => distances[x].distance = Math.round((distances[x].distance / amount * 100)));
 
        // 2つの時間帯の中間色を算出
        const [ color1, color2 ] = [ 0, 1 ]
            .map(x => this.brightness[distances[x].time])
            .map(x => getColor(x));
        const midColor = [ 0, 1, 2 ].map(x => {
            const gap = color1[x] - color2[x];
            return color2[x] + Math.round(gap * distances[0].distance / 100);
        });
        return midColor;
    }
}
 
export { Config }