如何调用 less.js 函数

发布于 2024-12-17 10:11:19 字数 1460 浏览 5 评论 0原文

我什至不太确定如何问这个问题。 LESS CSS框架包含几个操作颜色的函数,我想知道如何自己调用这些函数来修改颜色。问题是这些函数位于另一个函数内部并定义如下:

(function (tree) {
tree.functions = {
    darken: function(color, amount){
        ...stuff...
    }
}
}

我知道足够多的信息来假设 darkentree.functions 的一种方法,但是对于我不知道如何调用它,因为它位于匿名函数 (function (tree) 内部。

[编辑] 从 @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);

I'm not even really sure how to ask this. The LESS CSS Framework contains several functions to manipulate color, I'd like to know how to call these functions myself to modify a color. The problem is that these functions are located inside another function and defined as such:

(function (tree) {
tree.functions = {
    darken: function(color, amount){
        ...stuff...
    }
}
}

I know enough to assume that darken is a method of tree.functions, but for the life of me don't know how to call it because it is inside of the anonymous function (function (tree).

[edit]
After getting a solution from @pradeek I created this function incase anyone needs it. Can easily be adapted to all the other functions LESS has:

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
}
}

And you can call it like so:

$(this).css('background', lessColor.darken($(this).css('background'), .25);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吖咩 2024-12-24 10:11:19

编辑:
darken 函数使用内置原语。

下面介绍如何使用变暗功能

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

EDIT:
The darken function uses built-in primitives.

Here's how to use the darken function

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
还不是爱你 2024-12-24 10:11:19

看LESS 1.7未缩小的代码 对在这里

第 141 行是这样的:

less = {};
tree = less.tree = {};

并且位于全局范围内。所以less对象是在浏览器中定义的。

接下来,查看 1254 行:

tree.functions = {

您的 darken 函数是在其中的某处定义的。


您可以像这样调用 darken

less.tree.functions.darken(color, amount);

Look at the un-minified code of LESS 1.7 right here.

Line 141 is this:

less = {};
tree = less.tree = {};

And is in the global scope. So the less object is defined in the browser.

Next, look at line 1254:

tree.functions = {

Your darken function is defined somewhere in there.


You can call darken like so:

less.tree.functions.darken(color, amount);
半边脸i 2024-12-24 10:11:19

虽然这个答案不会告诉您如何调用 less.js 函数来操纵颜色,但它确实提供了一种不同的方法来操纵颜色,因为这似乎是主要目标。

有一个方便的 JavaScript 库专门用于此目的:TinyColor

TinyColor 是一个小型、快速的库,用于在 JavaScript 中进行颜色操作和转换。它允许多种形式的输入,同时提供颜色转换和其他颜色实用功能。它没有依赖关系。

要使颜色变暗,只需

var darkenedColor = tinycolor("#f00").darken(20).toString(); // "#990000"

While this answer won’t tell you how to call less.js functions to manipulate color, it does provide a different approach to manipulate color, as this seems to be the main goal.

There exists a handy JavaScript library just for that: TinyColor.

TinyColor is a small, fast library for color manipulation and conversion in JavaScript. It allows many forms of input, while providing color conversions and other color utility functions. It has no dependencies.

To darken a color, simply

var darkenedColor = tinycolor("#f00").darken(20).toString(); // "#990000"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文