Less css compiler. Unable to use darken property

2019-03-05 13:52发布

问题:

I'm developing a project using LESS as compiler for my CSS. I have already a fully working loop that sets the background color properly.

My question is this: With my current code, when i try to use darken property, the compiling result is this:

SyntaxError:error evaluating function darken: Object # has no method 'toHSL'

and the code is this one:

@colors:
"008B8B",
"00CDCD",
"00EEEE";

/* Colors and background loop (based on colors.less arrays) */
.loop-colors(@index) when (@index > 0){ // loop to generate rules for each color
  .loop-colors(@index - 1);// call for the next iteration
  @color: e(extract(@colors, @index));
  @hexColor:  ~'#@{color}';
  @border: 1px solid darken(@hexColor, 5%);
  &.col-@{color}{
    background: @hexColor;
    border:@border;
  }
}

i have no idea why this is not good.

I mean, i think is because the list of color doesn't have the "#" before every color, but just because i'm using it on the css class as well, i can't add it to @colors, so i have to add it later.

I don't know if and why adding the "#" later will affect the darken property and how.

Thanks

回答1:

As mentioned by @seven-phases-max, ~'#@{color}' will not create a color but a string. To convert a string to a color, you can use color function.

@colors:
  "008B8B",
  "00CDCD",
  "00EEEE";

/* Colors and background loop (based on colors.less arrays) */
.loop-colors(@index) when (@index > 0) { // loop to generate rules for each color
  .loop-colors(@index - 1);// call for the next iteration
  @color: e(extract(@colors, @index));
  @hexColor:  ~'#@{color}';
  @border: 1px solid darken(color(@hexColor), 5%);
  &.col-@{color} {
    background: @hexColor;
    border: @border;
  }
}


标签: css less