UNPKG

@thi.ng/color

Version:

Array-based color types, CSS parsing, conversions, transformations, declarative theme generation, gradients, presets

38 lines (37 loc) 1.02 kB
import { clamp01 } from "@thi.ng/math/interval"; import { setC4 } from "@thi.ng/vectors/setc"; import { srgb } from "../srgb/srgb.js"; const G1 = -0.6088425710866344; const G2 = -0.001748900018414868; const G3 = 0.4097731842899564; const G4 = 1.2762722061615583; const G5 = 3115080994769546e-19; const G6 = 0.11013841706194392; const R1 = 1.3803015908551253; const R2 = 4478684462124118e-19; const R3 = -0.15785750232675008; const B1 = -0.9990954974165059; const B2 = 0.0032447435545127036; const B3 = 0.453646839257496; const kelvinRgb = (out, kelvin, alpha = 1) => { kelvin *= 0.01; let t; return kelvin < 66 ? setC4( out || srgb(), 1, clamp01(G1 + G2 * (t = kelvin - 2) + G3 * Math.log(t)), kelvin < 20 ? 0 : clamp01( B1 + B2 * (t = kelvin - 10) + B3 * Math.log(t) ), alpha ) : setC4( out || srgb(), clamp01(R1 + R2 * (t = kelvin - 55) + R3 * Math.log(t)), clamp01(G4 + G5 * (t = kelvin - 50) - G6 * Math.log(t)), 1, alpha ); }; export { kelvinRgb };