less.js icon indicating copy to clipboard operation
less.js copied to clipboard

Can I use LESS functions in JavaScript evaluation?

Open Pestov opened this issue 13 years ago • 3 comments

Hi. I want rewrite this SCSS on LESS. It's look like this:

.shade(@color: #3498db, @depth: 20, @angle: 135deg, @long: false, @fade: false) {
    @ang: (@angle - 90);
    @x: 1.5 * cos(@ang);
    @y: 1.5 * sin(@ang);

    @defineDarken: (lightness(@color)/@depth)/2;
    @darken: ~`(function (long, defineDarken) {var d = (long == "true") ? 0 : defineDarken; return d;})("@{long}", "@{defineDarken}")`;  

    @defineOpacify: (opacity(@color)/@depth);
    @opacify: ~`(function (fade, defineOpacify) {var o = (fade == "true") ? 0 : defineOpacify; return o;})("@{fade}", "@{defineOpacify}")`;

    @thishue: hue(@color);
    @thissaturation: saturation(@color);
    @thislightness: lightness(@color);

    @shadow: ~`(function (depth,x,y,darken,opacify,thishue,thissaturation,thislightness) {
        var shadow = "";
        for(var i=1; i<depth; i++) {
            if (i != depth) {
                shadow += i*x+"px "+i*y+"px 0 hsla("+thishue+", "+thissaturation+", "+thislightness - (i*darken)+", "+1 - (i * opacify)+"), ";
            } else {
                shadow += i*x+"px "+i*y+"px 0 hsla("+thishue+", "+thissaturation+", "+thislightness - (i*darken)+", "+1 - (i * opacify)+");"; 
            }
        }
        return shadow;
    })("@{depth}", "@{x}", "@{y}", "@{darken}", "@{opacify}", "@{thishue}", "@{thissaturation}", "@{thislightness}")`;

    text-shadow: @shadow;   

I don't use CSS function and as a result this big bad code. This LESS not work - "Operation on an invalid type". Next issue how i can see result my javascript before compile error?

A lot of thanks, in advance.

Pestov avatar Sep 07 '13 11:09 Pestov

I am just re-writing this without javascript, but the reason for the error seems to be because you are using opacity() rather than alpha().. would like to work out why thats causing a syntax error as it should pass through..

lukeapage avatar Sep 08 '13 15:09 lukeapage

because opacity(white) gets compiled to opacity(white) (unchanged) and then gets an error when you try to do maths on it.

lukeapage avatar Sep 08 '13 15:09 lukeapage

ok, here you go, written for 1.5.0 beta, no JavaScript

.shade(@color: #3498db, @depth: 20, @angle: 135deg, @long: false, @fade: false) {
    @ang: (@angle - 90deg);
    @x: 1.5px * cos(@ang);
    @y: 1.5px * sin(@ang);

    .shade(@color, @depth, @x, @y, @long, @fade, (lightness(@color)/@depth)/2, (alpha(@color)/@depth));
}

.shade(@color, @depth, @x, @y, @long, @fade, @darken, @opacify) when (@long = true) {
      .shade(@color, @depth, @x, @y, @fade, 0, @opacify);
}

.shade(@color, @depth, @x, @y, @long, @fade, @darken, @opacify) when (@long = false) {
  .shade(@color, @depth, @x, @y, @fade, @darken, @opacify);
}


.shade(@color, @depth, @x, @y, @long, @darken, @opacify) when (@fade = true) {
  .shade(@color, @depth, @x, @y, @darken, 0);
}


.shade(@color, @depth, @x, @y, @long, @darken, @opacify) when (@fade = false) {
  .shade(@color, @depth, @x, @y, @darken, @opacify);
}

.shade(@color, @depth, @x, @y, @darken, @opacify) {
    @hue: hue(@color);
    @sat: saturation(@color);
    @lightness: lightness(@color);
    .shade-recursive(1, @depth, @x, @y, @darken, @opacify, @hue, @sat, @lightness);  
}

.shade-recursive(@i, @depth, @x, @y, @darken, @opacify, @hue, @sat, @lightness) when (@i < @depth) {
  text-shadow+: (@i * @x) (@i * @y) 0 hsla(@hue, @sat, (@lightness - (@i * @darken)), (1 - (@i * @opacify)));
  .shade-recursive(@i + 1, @depth, @x, @y, @darken, @opacify, @hue, @sat, @lightness);
}

.a {
  .shade();
}

lukeapage avatar Sep 08 '13 15:09 lukeapage