/**
 *
 * @param {number} r 0 to 1
 * @param {number} g 0 to 1
 * @param {number} b 0 to 1
 * @returns {{h: number, s: number, v: number}}
 */
export function rgb2hsv(r, g, b) {

    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);

    let h, s, v = max;

    const d = max - min;

    s = max === 0 ? 0 : d / max;

    if (max === min) {
        h = 0; // achromatic
    } else {
        switch (max) {
            case r:
                h = (g - b) / d + (g < b ? 6 : 0);
                break;
            case g:
                h = (b - r) / d + 2;
                break;
            case b:
                h = (r - g) / d + 4;
                break;
        }

        h /= 6;
    }

    return {
        h,
        s,
        v
    };
}
