import { clamp01 } from "../../math/clamp01.js";

/**
 *
 * @param {number} h from 0 to 1
 * @param {number} s from 0 to 1
 * @param {number} v from 0 to 1
 * @returns {{r: number, g: number, b: number}} output in range 0..1
 */
export function hsv2rgb_float(h, s, v) {
    let _h = h;

    if (_h < 0) {
        _h = _h + Math.ceil(Math.abs(_h));
    }

    _h = _h % 1;

    const _s = clamp01(s);
    const _v = clamp01(v);

    let r, g, b, i, f, p, q, t;

    i = Math.floor(_h * 6);

    f = _h * 6 - i;

    p = _v * (1 - _s);
    q = _v * (1 - f * _s);
    t = _v * (1 - (1 - f) * _s);

    switch (i % 6) {
        case 0:
            r = _v, g = t, b = p;
            break;
        case 1:
            r = q, g = _v, b = p;
            break;
        case 2:
            r = p, g = _v, b = t;
            break;
        case 3:
            r = p, g = q, b = _v;
            break;
        case 4:
            r = t, g = p, b = _v;
            break;
        case 5:
            r = _v, g = p, b = q;
            break;
    }

    return {
        r, g, b
    }
}