Javascript 如何从Vibrant js中获得柔和的颜色?

Javascript 如何从Vibrant js中获得柔和的颜色?,javascript,Javascript,我使用从图像中获取颜色,在他们的网站上,他们展示了一些示例,其中他们还显示了最白的颜色(LightMuted),但是我可以获取任何其他值,但是LightMuted对我来说不返回任何值,我如何获取该颜色?我试着像这样使用它: albumart.addEventListener('load', function () { var vibrant = new Vibrant(albumart); var swatc

我使用从图像中获取颜色,在他们的网站上,他们展示了一些示例,其中他们还显示了最白的颜色(LightMuted),但是我可以获取任何其他值,但是LightMuted对我来说不返回任何值,我如何获取该颜色?我试着像这样使用它:

            albumart.addEventListener('load', function () {
                var vibrant = new Vibrant(albumart);
                var swatches = vibrant.swatches()
                for (var swatch in swatches)
                    if (swatches.hasOwnProperty(swatch) && swatches[swatch]) {
                    var name = (swatches.LightMuted.getHex()); // Nothing
                    var details = (swatches.DarkVibrant.getHex()); // Returns color

请注意,当Vibrant无法为配置文件找到匹配颜色时,某些样例可能会设置为“未定义”

您可以在开始之前检查该值

var name = swatches.LightMuted ? swatches.LightMuted.getHex() 
                               : '#333' // default value
有一个名为“可以用来获取图像”的库,但请记住,它每次都会提供随机调色板

// Create the Kleur Object
Kleur = new Kleur();

// Set the image link to get the palette from
imageObj = Kleur.init(imgLink);

// Wait for the image to load
imageObj.onload = function(e) {

    // get the color array from the image
    let colorArr = Kleur.getPixelArray(imageObj);

    // pass the array to generate the color array

    let array_of_pixels = Kleur.generateColorArray(colorArr);

    // get light colors
    const light = Kleur.getLightColors(array_of_pixels);

    // you can get the dominant color from the image
    const dominant = Kleur.getDominant(array_of_pixels);

    // log the light colors and the dominant color
    console.log(light, dominant)
}

如果要查看使用此代码的示例,请访问

,无需每次都获取LightMuted的值。根据您选择的图像,它可能为空。即使在vibrant.js的登录页上,您也可以看到第二幅图像没有LightMuted属性。是否有任何方法可以检查是否没有LightMuted颜色并默认为另一种颜色?@Daniell您可以使用条件运算符=)进行检查