在javascript中选择最丰富多彩的颜色

Mac*_*Elf 7 javascript colors

我有一组颜色,我想找到他们相对的色彩.任何人都可以在javascript中向我展示一个例子吗?非常感谢!

ken*_*bec 4

// 您可以通过饱和度和亮度来比较颜色 -

function rgbtoHsv(rgb){
    var c= rgb.match(/\d+/g),
    r= c[0]/255, g= c[1]/255, b= c[2]/255,
    max= Math.max(r, g, b), min= Math.min(r, g, b),
    h= 0, s= 0, v= max;
    if(max!= min){
        var d= max-min;
        s= d/max;
        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;
        }
    }
    return [Math.round(h*60), Math.round(s*100), Math.round(v*100)];
}
function sortColors(a, b){
    var a1= rgbtoHsv(a), b1= rgbtoHsv(b);
    return  (b1[1]+b1[2])-  (a1[1]+a1[2]);
}
Run Code Online (Sandbox Code Playgroud)

var color=['rgb(255,0,0)','rgb(150,150,150)','rgb(0,200,100)','rgb(0,255,255)']; // 颜色.sort(sortColors).join('\n')

/*  returned value: (most to least 'colorful')
rgb(255,0,0)
rgb(0,255,255)
rgb(0,200,100)
rgb(150,150,150)
*/
Run Code Online (Sandbox Code Playgroud)