代码之家  ›  专栏  ›  技术社区  ›  anekix

颜色从hsv到rgb转换后的数学

  •  0
  • anekix  · 技术社区  · 8 年前

    https://en.wikipedia.org/wiki/HSL_and_HSV . https://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c .

    /**
     * Converts an HSV color value to RGB. Conversion formula
     * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
     * Assumes h, s, and v are contained in the set [0, 1] and
     * returns r, g, and b in the set [0, 255].
     *
     * @param   Number  h       The hue
     * @param   Number  s       The saturation
     * @param   Number  v       The value
     * @return  Array           The RGB representation
     */
    function hsvToRgb(h, s, v){
        var r, g, b;
    
        var i = Math.floor(h * 6);
        var f = h * 6 - i;
        var p = v * (1 - s);
        var q = v * (1 - f * s);
        var 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 * 255, g * 255, b * 255];
    }.
    

    我想了解它背后的逻辑,以及在转换算法中使用这些特定值得出的数学。

    1 回复  |  直到 8 年前