Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conversion form color temperatures below 2000k results in 1000k #288

Open
Christian-Me opened this issue Jan 31, 2022 · 0 comments
Open

Comments

@Christian-Me
Copy link

The algorithm used for converting RGB to temperature fails for temperatures below 2000k (i.e. chroma.temperature(1500) results in chroma.temperature() = 1000k) This is due to the fact that blue is constant 0 and red constant 1 (255) below 2000k.
image

A fix could look like this taking green into account for values below 2000k

/*
 * Based on implementation by Neil Bartlett
 * https://github.com/neilbartlett/color-temperature
 * with modifications for temp < 2000k
 **/

const temperature2rgb = require('./temperature2rgb');
const {unpack} = require('../../utils');
const {round} = Math;

const rgb2temperature = (...args) => {
    const rgb = unpack(args, 'rgb');
    const r = rgb[0], g = rgb[1], b = rgb[2];
    let minTemp = 1000;
    let maxTemp = 40000;
    const eps = 0.4;
    let temp;
    while (maxTemp - minTemp > eps) {
        temp = (maxTemp + minTemp) * 0.5;
        const rgb = temperature2rgb(temp);
        if (b>0) { // temperatures above 2000k
            if ((rgb[2] / rgb[0]) >= (b / r)) {
                maxTemp = temp;
            } else {
                minTemp = temp;
            }
        } else { // below 2000k blue becomes constant 0 and red constant 255! Only green is relevant here
            if (rgb[1] >= g) {
                maxTemp = temp;
            } else {
                minTemp = temp;
            }
        }
    }
    return round(temp);
}

module.exports = rgb2temperature;

If this makes sense I can provide a PR. Thank you for this great library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant