I have this code there find the dominated color of an image (using RGBaster.js) then convert it to a flat color, and give the information box beside the image the color. At the same time, it makes the text color white or black after the YIQ scala.
function getContrastYIQ(hexcolor){
    var r = parseInt(hexcolor.substr(1,2),16);
    var g = parseInt(hexcolor.substr(3,2),16);
    var b = parseInt(hexcolor.substr(4,2),16);
    var yiq = ((r*299)+(g*587)+(b*114))/1000;
    return (yiq >= 128) ? 'black' : 'white';
}
var img = $('.category').attr('data-background');
var colors = RGBaster.colors(img, {
    paletteSize: 5,
    success: function(colors){
        console.log("-----------------------------------------------");
        console.log("Dominant color:", colors.dominant);
        console.log("-----------------------------------------------");
        var rgb = colors.dominant;
        rgb = rgb.substring(4, rgb.length-1).replace(/ /g, '').split(',');
        var flatColors = FlatColors(rgb);
        var flatColor = flatColors[3];
        var textColor = getContrastYIQ(flatColor);
        console.log(textColor);
        $(".category").css('background-image', 'url(' + img + ')');
        $(".category .info").css('background-color', flatColor);
        $(".text").css('color', textColor);
        $(".text").text(flatColors[4]);
    }
});
Here comes the problem
I have multiple divs named like follow:
<div class="category" data-background="images/7.jpg">
    <div class="info">
        <p class="text">Hello World</p>
    </div>
</div>
And the code are need to find the dominated color for each category div and paint the info div with it.
I have tried to do this:
function colorPick(img) {
    RGBaster.colors(img, {
    paletteSize: 5,
        success: function(colors){
            var rgb = colors.secondary;
            rgb = rgb.substring(4, rgb.length-1).replace(/ /g, '').split(',');
            var flatColors = FlatColors(rgb);
            console.log("Return:", flatColors[3]);
            return flatColors[3];                       
        }
    });
}
$('.category').each(function(){
    var img = $(this).attr('data-background');
    $(this).css('background-image', 'url(' + img + ')');
    var color = colorPick(img);
    console.log(color);
});
But that didn't work. so now I need some help.
 
    