Skip to content Skip to sidebar Skip to footer

Get Color Amount With Get-image-colors Package

I'm using get-image-colors package 'get-image-colors': '^2.0.0' to get color amount from png and gif. My code is: const getColors = require('get-image-colors') getColors('https:/

Solution 1:

I wasn't able to make it work with linked package but in its dependiecies I found "getPixels" package.

I used and it gave me an ndarray of pixels with its rgb. Then I converted this ndarray thing to regular array of arrays. Then in loop I checked if every nested arrays is equal by converting it to json string:

const getPixels = require("get-pixels");

//1px only black https://i.imgur.com/dStJ1Og.png//2px only black https://i.imgur.com/5z46WVp.png//88x62px only black https://i.imgur.com/ZtGMUh9.png//88x62px black and red https://i.imgur.com/67IZCaD.png//220x297px black and red https://i.imgur.com/KTNnJGF.jpggetPixels("https://i.imgur.com/KTNnJGF.jpg", function(err, ndarray) {
    if(err) {
        console.log("Bad image path!");
        return;
    }

    const uintArr = newUint8Array(ndarray.data);
    const regularArr = [];
    let oneColor = true;

    for(let i = 0, l = uintArr.length / 4; i < l; i++) {
        regularArr.push([uintArr[4*i], uintArr[4*i+1], uintArr[4*i+2], uintArr[4*i+3]])
    }
    for(let i = 0, l = regularArr.length; i < l; i++) {
        if(regularArr[i + 1]) {
            let currentValString = JSON.stringify(regularArr[i]);
            let nextValString = JSON.stringify(regularArr[i + 1]);

            if(currentValString !== nextValString) {
                oneColor = false;
                break;
            }
        }
    }

    console.log('Is image one color? ', oneColor);
});

I don't know if it's most efficient but it works.

Post a Comment for "Get Color Amount With Get-image-colors Package"