You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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 **/consttemperature2rgb=require('./temperature2rgb');const{unpack}=require('../../utils');const{round}=Math;constrgb2temperature=(...args)=>{constrgb=unpack(args,'rgb');constr=rgb[0],g=rgb[1],b=rgb[2];letminTemp=1000;letmaxTemp=40000;consteps=0.4;lettemp;while(maxTemp-minTemp>eps){temp=(maxTemp+minTemp)*0.5;constrgb=temperature2rgb(temp);if(b>0){// temperatures above 2000kif((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 hereif(rgb[1]>=g){maxTemp=temp;}else{minTemp=temp;}}}returnround(temp);}module.exports=rgb2temperature;
If this makes sense I can provide a PR. Thank you for this great library.
The text was updated successfully, but these errors were encountered:
The algorithm used for converting RGB to temperature fails for temperatures below 2000k (i.e.
chroma.temperature(1500)
results inchroma.temperature() = 1000k
) This is due to the fact that blue is constant 0 and red constant 1 (255) below 2000k.A fix could look like this taking green into account for values below 2000k
If this makes sense I can provide a PR. Thank you for this great library.
The text was updated successfully, but these errors were encountered: