首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery从JSON数组中找到相似的颜色

问jQuery从JSON数组中找到相似的颜色
EN

Stack Overflow用户
提问于 2013-02-08 00:53:59
回答 2查看 1.2K关注 0票数 0

我是Javascript和jQuery的新手,在调整下面的脚本时遇到了麻烦:

http://jsfiddle.net/kJdX3/

如前所述,它从颜色值数组中查找最接近的颜色。

我如何编辑代码,而不是计算此数组中的颜色值:

代码语言:javascript
复制
var base_colors=["660000","0066cc","ff9900"];

它将计算JSON数组中的十六进制值属性,如下所示:

代码语言:javascript
复制
var base_colors = [
    { "name" : "burgundy" , "number" : "17A" , "hexvalue" : "660000"}, 
    { "name" : "blue" , "number" : "18B ", "hexvalue" : "0066cc"}, 
    { "name" : "orange" , "number" : "19C" , "hexvalue" : "ff9900"}];

最后,如何使用jQuery引用包含base_colors数组的外部JSON文件?

EN

回答 2

Stack Overflow用户

发布于 2013-02-08 01:01:41

我已经对您的jsFiddle进行了更改,以实现这一点。

http://jsfiddle.net/kJdX3/9/

代码语言:javascript
复制
function getSimilarColors (color) {
    var base_colors = [
{ "name" : "burgundy" , "number" : "17A" , "hexvalue" : "660000"}, 
{ "name" : "blue" , "number" : "18B ", "hexvalue" : "0066cc"}, 
{ "name" : "orange" , "number" : "19C" , "hexvalue" : "ff9900"}];
    //Convert to RGB, then R, G, B
    var color_rgb = hex2rgb(color);
    var color_r = color_rgb.split(',')[0];
    var color_g = color_rgb.split(',')[1];
    var color_b = color_rgb.split(',')[2];

    //Create an emtyp array for the difference betwwen the colors
    var differenceArray=[];

    //Function to find the smallest value in an array
    Array.min = function( array ){
           return Math.min.apply( Math, array );
    };


    //Convert the HEX color in the array to RGB colors, split them up to R-G-B, then find out the difference between the "color" and the colors in the array
    $.each(base_colors, function(index, value) { 
        var base_color_rgb = hex2rgb(value.hexvalue);
        var base_colors_r = base_color_rgb.split(',')[0];
        var base_colors_g = base_color_rgb.split(',')[1];
        var base_colors_b = base_color_rgb.split(',')[2];

        //Add the difference to the differenceArray
        differenceArray.push(Math.sqrt((color_r-base_colors_r)*(color_r-base_colors_r)+(color_g-base_colors_g)*(color_g-base_colors_g)+(color_b-base_colors_b)*(color_b-base_colors_b)));
    });

    //Get the lowest number from the differenceArray
    var lowest = Array.min(differenceArray);

    //Get the index for that lowest number
    var index = differenceArray.indexOf(lowest);

    //Function to convert HEX to RGB
    function hex2rgb( colour ) {
        var r,g,b;
        if ( colour.charAt(0) == '#' ) {
            colour = colour.substr(1);
        }

        r = colour.charAt(0) + colour.charAt(1);
        g = colour.charAt(2) + colour.charAt(3);
        b = colour.charAt(4) + colour.charAt(5);

        r = parseInt( r,16 );
        g = parseInt( g,16 );
        b = parseInt( b ,16);
        return r+','+g+','+b;
    }

    //Return the HEX code
    return base_colors[index].hexvalue;
}

以下是我所做的更改:

var base_color_rgb = hex2rgb(value.hexvalue);

return base_colors[index].hexvalue;

票数 1
EN

Stack Overflow用户

发布于 2013-02-08 00:57:40

使用http://api.jquery.com/jQuery.getJSON/

代码语言:javascript
复制
$.getJSON(url, params, function(data) {
 // your json with colors
});

非常重要的一点:的json应该是有效的!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14756542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档