about summary refs log tree commit diff stats
path: root/js/leibovitz/balance.js
blob: 6566176ad906ff422d99003ff92490e90dedcb81 (plain) (blame)
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
// BalanceManager handles white balance adjustments
class BalanceManager {
    static init() {
        this.balanceSlider = document.getElementById('balance-slider');
        this.balanceValue = document.getElementById('balance-value');
        this._setupEventListeners();
    }

    static _setupEventListeners() {
        this.balanceSlider.addEventListener('input', () => {
            const value = this.balanceSlider.value;
            this.balanceValue.textContent = `${value}K`;
        });
    }

    static getCurrentBalance() {
        return parseInt(this.balanceSlider.value);
    }

    static applyBalance(imageData) {
        const balance = this.getCurrentBalance();
        if (!balance || balance === 6500) return imageData; // 6500K is neutral

        const data = imageData.data;
        const temperature = balance / 6500; // Convert to temperature ratio

        for (let i = 0; i < data.length; i += 4) {
            // Adjust red and blue channels based on temperature
            // Warmer (lower K) increases red, decreases blue
            // Cooler (higher K) increases blue, decreases red
            data[i] = Math.min(255, data[i] * (1 + (temperature - 1) * 0.2));     // Red
            data[i + 2] = Math.min(255, data[i + 2] * (1 + (1 - temperature) * 0.2)); // Blue
        }

        return imageData;
    }
}