Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you call a less.js function

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);
like image 413
Oz Ramos Avatar asked Apr 27 '26 00:04

Oz Ramos


1 Answers

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
like image 73
pradeek Avatar answered Apr 29 '26 12:04

pradeek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!