This is an explanation of the video content.
 用技术延续对ACG的热爱
59

 |   | 

javascript库:Vibrant.js获取图片主题颜色

使用Vibrant是非常直接的,但因为代码比解释更有效,这里有一个例子

var img = document.createElement('img');
img.setAttribute('src', 'examples/octocat.png')

img.addEventListener('load', function() {
    var vibrant = new Vibrant(img);
    var swatches = vibrant.swatches()
    for (var swatch in swatches)
        if (swatches.hasOwnProperty(swatch) && swatches[swatch])
            console.log(swatch, swatches[swatch].getHex())

    /*
     * Results into:
     * Vibrant #7a4426
     * Muted #7b9eae
     * DarkVibrant #348945
     * DarkMuted #141414
     * LightVibrant #f3ccb4
     */
});

正如你所看到的,Vibrant的第一个参数是一个图像。在将其传递给Vibrant之前,请确保它已加载。Vibrant有3个构造函数参数

new Vibrant(
    img,
    64, /* amount of colors in initial palette from which the swatches will be generated, defaults to 64 */
    5 /* quality. 0 is highest, but takes way more processing. defaults to 5. */
)
        
Function nameDescription
new Swatch()Initialize a new swatch. First argument needs to be an RGB array, second argument must be the swatch' population.
.getRgb()Get swatch color in a RGB array
.getHex()Get swatch color in hex format (#EE22DD)
.getHsl()Get swatch color in a HSL array
.getPopulation()Get population (amount of times this color was used in the original image)
.getTitleTextColor()Get a color (in hex) that works best with any 'title' text that is used over this swatch's color
.getBodyTextColor()Get a color (in hex) that works best with any 'body' text that is used over this swatch's color

59 🖺前端 ↦ JavaScript开发技巧 __ 198 字
 JavaScript开发技巧 #13