如何使用光谱颜色选择器隐藏某些颜色

时间:2015-08-18 04:40:40

标签: javascript jquery css color-picker

我是使用Spectrum&#39的颜色选择器的新手。所以我查看了光谱颜色选择器提供的所有功能,但我找不到隐藏颜色选择器中某些颜色的方法,以便用户无法选择它们。

例如,所有浅色:#FFFFFFF,#F5F5F5,#FFFAFA,#F0FFF0,#F5FFFA,#F0FFFF,#F0F8FF,#F8F8FF,#FFF5E,#F5F5DC,#FDF5E6,#FFFAF0,#FFFFF0,#FAEBD7 ,#FAF0E6,#FFF0F5不应该在颜色选择器中可用。

我有办法做到这一点吗?

2 个答案:

答案 0 :(得分:1)

您无法删除颜色选择器中的颜色,但您可以在调色板中显示有限数量的颜色,用户可以使用这些颜色而不包含用户无法选择的颜色。

$("#togglePaletteOnly").spectrum({
    showPaletteOnly: true,
    togglePaletteOnly: true,
    togglePaletteMoreText: 'more',
    togglePaletteLessText: 'less',
    color: 'blanchedalmond',
    palette: [
        ["#000","#444","#666","#999","#ccc","#eee","#f3f3f3","#fff"],
        ["#f00","#f90","#ff0","#0f0","#0ff","#00f","#90f","#f0f"],
        ["#f4cccc","#fce5cd","#fff2cc","#d9ead3","#d0e0e3","#cfe2f3","#d9d2e9","#ead1dc"],
        ["#ea9999","#f9cb9c","#ffe599","#b6d7a8","#a2c4c9","#9fc5e8","#b4a7d6","#d5a6bd"],
        ["#e06666","#f6b26b","#ffd966","#93c47d","#76a5af","#6fa8dc","#8e7cc3","#c27ba0"],
        ["#c00","#e69138","#f1c232","#6aa84f","#45818e","#3d85c6","#674ea7","#a64d79"],
        ["#900","#b45f06","#bf9000","#38761d","#134f5c","#0b5394","#351c75","#741b47"],
        ["#600","#783f04","#7f6000","#274e13","#0c343d","#073763","#20124d","#4c1130"]
    ]
});

我希望它有所帮助。

答案 1 :(得分:1)

你必须制作一个限制颜色hax的数组列表。 然后你应该使用change方法来处理。 和jquery $.inArray()确定受限颜色的函数。

例如

var banColor = ['#ffffff','#000000','#F0FFF0', '#F5FFFA', '#F0FFFF', '#F0F8FF', '#F8F8FF', '#FFF5E', '#F5F5DC', '#FDF5E6', '#FFFAF0', '#FFFFF0', '#FAEBD7', '#FAF0E6', '#FFF0F5'];

$(".example").spectrum({
    color: "#f00",
    change: function(color) {
        if( $.inArray( color.toHexString(), banColor ) >= 0 ){
            $("#basic-log").text("change called: " + color.toHexString() +' Is Not Allowed');
            // Reset your palate
        }
    }
});