File size: 5,205 Bytes
3c13b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
 * AspectCalc.js - アスペクト比計算ライブラリ
 * 縦解像度と横解像度から3種類のアスペクト比を計算します
 *
 * MIT License
 * 
 * Copyright (c) 2025 SenY
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

class AspectCalc {
    /**
     * 最大公約数を計算する関数
     * @param {number} a - 数値1
     * @param {number} b - 数値2
     * @returns {number} 最大公約数
     */
    static gcd(a, b) {
        while (b !== 0) {
            let temp = b;
            b = a % b;
            a = temp;
        }
        return a;
    }

    /**
     * 2つの数値の最大公約数で割って、最も簡単な整数比を求める
     * @param {number} width - 幅
     * @param {number} height - 高さ
     * @returns {Object} {x: number, y: number} の形式
     */
    static getIntegerRatio(width, height) {
        const divisor = this.gcd(width, height);
        return {
            x: width / divisor,
            y: height / divisor
        };
    }

    /**
     * 縦横共に20以下の整数比で表せる最も近似のアスペクト比を求める
     * @param {number} width - 幅
     * @param {number} height - 高さ
     * @returns {Object} {x: number, y: number} の形式
     */
    static getApproximateRatio(width, height) {
        const targetRatio = width / height;
        let bestRatio = { x: 1, y: 1 };
        let minError = Math.abs(targetRatio - 1);

        // 1から20までの全ての組み合わせを試す
        for (let x = 1; x <= 20; x++) {
            for (let y = 1; y <= 20; y++) {
                const ratio = x / y;
                const error = Math.abs(targetRatio - ratio);
                if (error < minError) {
                    minError = error;
                    bestRatio = { x, y };
                }
            }
        }

        return bestRatio;
    }

    /**
     * 一般的なディスプレイアスペクト比の中で最も近似のものを求める
     * @param {number} width - 幅
     * @param {number} height - 高さ
     * @returns {Object} {x: number, y: number} の形式
     */
    static getStandardRatio(width, height) {
        const targetRatio = width / height;
        
        // 一般的なアスペクト比のリスト(正規化された値)
        const standardRatios = [
            { x: 1, y: 1 },      // 1:1
            { x: 16, y: 9 },     // 16:9
            { x: 4, y: 3 },      // 4:3
            { x: 8, y: 5 },      // 8:5
            { x: 3, y: 2 },      // 3:2
            { x: 5, y: 4 },      // 5:4
            { x: 9, y: 16 },     // 9:16 (16:9の逆)
            { x: 3, y: 4 },      // 3:4 (4:3の逆)
            { x: 5, y: 8 },      // 5:8 (8:5の逆)
            { x: 2, y: 3 },      // 2:3 (3:2の逆)
            { x: 4, y: 5 }       // 4:5 (5:4の逆)
        ];

        let bestRatio = standardRatios[0];
        let minError = Math.abs(targetRatio - (bestRatio.x / bestRatio.y));

        for (const ratio of standardRatios) {
            const error = Math.abs(targetRatio - (ratio.x / ratio.y));
            if (error < minError) {
                minError = error;
                bestRatio = ratio;
            }
        }

        return bestRatio;
    }

    /**
     * メイン関数:3種類のアスペクト比を計算して返す
     * @param {number} width - 幅
     * @param {number} height - 高さ
     * @returns {Object} 3種類のアスペクト比を含むオブジェクト
     */
    static calculate(width, height) {
        if (width <= 0 || height <= 0) {
            throw new Error('幅と高さは正の数である必要があります');
        }

        return {
            integer: this.getIntegerRatio(width, height),
            approximate: this.getApproximateRatio(width, height),
            standard: this.getStandardRatio(width, height)
        };
    }
}

// モジュールエクスポート対応
if (typeof module !== 'undefined' && module.exports) {
    module.exports = AspectCalc;
}

// グローバル変数としても利用可能にする
if (typeof window !== 'undefined') {
    window.AspectCalc = AspectCalc;
}