我甚至不确定如何问这个问题。 LESS CSS Framework包含几个操作颜色的函数,我想知道如何自己调用这些函数来修改颜色。问题是这些函数位于另一个函数内并定义如下:
(function (tree) {
tree.functions = {
darken: function(color, amount){
...stuff...
}
}
}
我知道足够的假设变暗是 tree.functions 的一种方法,但是对于我的生活,我不知道如何调用它,因为它在里面匿名函数(函数(树)。
[编辑] 从@pradeek获得解决方案后,我创建了这个函数,任何人都需要它。可以很容易地适应LESS的所有其他功能:
var lessColor = {
/*
|--------------------------------------------------------------------------
| Darken
|--------------------------------------------------------------------------
*/
darken: function(col, val){
col = col.replace(/#/g, ''); //Remove the hash
var color = new less.tree.Color(col); //Create a new color object
var amount = new less.tree.Value(val); //Create a new amount object
var newRGB = less.tree.functions.darken(color, amount); //Get the new color
var hex = (newRGB.rgb[0] + 256 * newRGB.rgb[1] + 65536 * newRGB.rgb[2]).toString(16);
hex = hex.split('.', 1); //Remove everything after the decimal if it exists
//Add padding to the hex to make it 6 characters
while(hex.length < 6){
hex = hex+'0';
}
hex = '#'+hex; //Add the hash
return hex; //return the color
}
}
你可以这样称呼它:
$(this).css('background', lessColor.darken($(this).css('background'), .25);
答案 0 :(得分:3)
编辑: 变暗函数使用内置基元。
以下是如何使用变暗功能
var color = new less.tree.Color([255, 255, 255], 0.5),
amount = new less.tree.Value(5);
less.tree.functions.darken(color, amount); // returns a Color object
答案 1 :(得分:1)
查看LESS 1.7 right here的未缩小代码。
行141
就是这样:
less = {};
tree = less.tree = {};
并且在全球范围内。因此,less
对象在浏览器中定义。
接下来,查看第1254
行:
tree.functions = {
您的darken
函数已定义在那里。
您可以这样致电darken
:
less.tree.functions.darken(color, amount);
答案 2 :(得分:1)
虽然这个答案不会告诉你如何调用less.js函数来操纵颜色,但它确实提供了一种不同的操作颜色的方法,因为这似乎是主要目标。
只为此提供了一个方便的JavaScript库:TinyColor。
TinyColor是一个小型,快速的库,用于JavaScript中的颜色处理和转换。它允许多种形式的输入,同时提供颜色转换和其他颜色效用功能。它没有依赖关系。
简单地使颜色变暗
var darkenedColor = tinycolor("#f00").darken(20).toString(); // "#990000"