给调色板元素边框或标题

时间:2015-09-22 12:25:35

标签: javascript jquery css colors color-picker

我有我的jquery频谱插件:Spectrum带有这样的颜色调色板:Palette

我需要为颜色选择器添加一个带颜色的单独调色板。 有5种原色和大约15种其他“次要”颜色,所以我想将它们标记/突出显示为主要颜色。例如,在盒子周围给它们一个黑色边框或者为5种原色设置标题。

以小提琴为例(完整示例): http://jsfiddle.net/bgrins/ctkY3/

<a href='http://bgrins.github.com/spectrum'>&lt;--Spectrum Homepage</a>

<h2>Basic Usage</h2>
<input type='text' class="basic"/>
<em id='basic-log'></em>

<h2>Full Example</h2>
<input type='text' id="full"/>

JS

$(".basic").spectrum({
    color: "#f00",
    change: function(color) {
        $("#basic-log").text("change called: " + color.toHexString());
    }
});

$("#full").spectrum({
    color: "#ECC",
    showInput: true,
    className: "full-spectrum",
    showInitial: true,
    showPalette: true,
    showSelectionPalette: true,
    maxSelectionSize: 10,
    preferredFormat: "hex",
    localStorageKey: "spectrum.demo",
    move: function (color) {

    },
    show: function () {

    },
    beforeShow: function () {

    },
    hide: function () {

    },
    change: function() {

    },
    palette: [
        ["rgb(0, 0, 0)", "rgb(67, 67, 67)", "rgb(102, 102, 102)",
        "rgb(204, 204, 204)", "rgb(217, 217, 217)","rgb(255, 255, 255)"],
        ["rgb(152, 0, 0)", "rgb(255, 0, 0)", "rgb(255, 153, 0)", "rgb(255, 255, 0)", "rgb(0, 255, 0)",
        "rgb(0, 255, 255)", "rgb(74, 134, 232)", "rgb(0, 0, 255)", "rgb(153, 0, 255)", "rgb(255, 0, 255)"], 
        ["rgb(230, 184, 175)", "rgb(244, 204, 204)", "rgb(252, 229, 205)", "rgb(255, 242, 204)", "rgb(217, 234, 211)", 
        "rgb(208, 224, 227)", "rgb(201, 218, 248)", "rgb(207, 226, 243)", "rgb(217, 210, 233)", "rgb(234, 209, 220)", 
        "rgb(221, 126, 107)", "rgb(234, 153, 153)", "rgb(249, 203, 156)", "rgb(255, 229, 153)", "rgb(182, 215, 168)", 
        "rgb(162, 196, 201)", "rgb(164, 194, 244)", "rgb(159, 197, 232)", "rgb(180, 167, 214)", "rgb(213, 166, 189)", 
        "rgb(204, 65, 37)", "rgb(224, 102, 102)", "rgb(246, 178, 107)", "rgb(255, 217, 102)", "rgb(147, 196, 125)", 
        "rgb(118, 165, 175)", "rgb(109, 158, 235)", "rgb(111, 168, 220)", "rgb(142, 124, 195)", "rgb(194, 123, 160)",
        "rgb(166, 28, 0)", "rgb(204, 0, 0)", "rgb(230, 145, 56)", "rgb(241, 194, 50)", "rgb(106, 168, 79)",
        "rgb(69, 129, 142)", "rgb(60, 120, 216)", "rgb(61, 133, 198)", "rgb(103, 78, 167)", "rgb(166, 77, 121)",
        "rgb(91, 15, 0)", "rgb(102, 0, 0)", "rgb(120, 63, 4)", "rgb(127, 96, 0)", "rgb(39, 78, 19)", 
        "rgb(12, 52, 61)", "rgb(28, 69, 135)", "rgb(7, 55, 99)", "rgb(32, 18, 77)", "rgb(76, 17, 48)"]
    ]
});

2 个答案:

答案 0 :(得分:2)

Demo

每个拇指都将颜色存储为数据,插件具有在显示调色板时运行的回调。我们可以使用这些东西。

创建原色列表

var primary = ["rgb(208, 224, 227)", "rgb(201, 218, 248)", "rgb(207, 226, 243)", "rgb(217, 210, 233)", "rgb(234, 209, 220)"];

添加相关的CSS

.primary-thumb{
    border: 1px solid #000;
}

在pallete选项的show函数中,包含将CSS类添加到具有这些颜色的Thumb的代码。

show: function () {
    $('.sp-thumb-el').each(function(){
        var this_color = $(this).data('color');
        if ($.inArray(this_color, primary) >= 0){
            $(this).find('.sp-thumb-inner').addClass('primary-thumb');
        }
    });
},

答案 1 :(得分:1)

这是一个快速的黑客攻击。

&#13;
&#13;
/*The Array of RGP colors you want to border*/
var arrayOfRGBColors = ['rgb(255, 128, 0)', 'rgb(255, 235, 205)', 'rgb(255, 255, 255)'];

/*Simple flat pallete*/
$("#flat").spectrum({
    flat: true,
    showPaletteOnly: true,
    showPalette: true,
    color: 'blanchedalmond',
    palette: [
        ['black', 'white', 'blanchedalmond',
            'rgb(255, 128, 0);', 'hsv 100 70 50'],
        ['red', 'yellow', 'green', 'blue', 'violet']
    ],
    move: function (color) {
        border();
    },
    show: function () {
        border();
    },
    beforeShow: function () {
        border();
    },
    hide: function () {
        border();
    },
    change: function () {
        border();
    },
});

function border() {
    /*Foreach pallete item, set the css*/
    $('.sp-thumb-inner').each(function () {
        /*Get the RGB color*/
        var color = $(this).css('background-color');
        /*If the color is found in the array*/
        if ($.inArray(color, arrayOfRGBColors) > 0) {
            /*Set the border*/
            $(this).addClass('bord');
        }
    });
}
&#13;
.bord{
    border:1px dashed black;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.7.1/spectrum.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/spectrum/1.7.1/spectrum.min.css" rel="stylesheet" />

<input type='text' id="flat" />
&#13;
&#13;
&#13;