Skip to content

Compilation of named colors results in hex values being used incorrectly in interpolations #1595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TedDriggs opened this issue Oct 14, 2013 · 9 comments

Comments

@TedDriggs
Copy link

The variable declaration @myVariable: red; recognizes that it contains a color, which is useful. However, on compilation div { color: @myVariable; } is compiled to:

div { 
    color: #ff0000;
}

instead of

div {
    color: red;
}

By itself, this isn't a huge problem, but it becomes one when attempting to use selector interpolation: .@{myVariable} { color: @myVariable; } becomes

.#ff0000 {
    color: #ff0000;
}

instead of

.red {
    color: red;
}

While it makes sense to parse the named color as a color rather than just a string, I'm not sure why the output wouldn't preserve the named color.

@seven-phases-max
Copy link
Member

See #692.
It's more like a "legacy feature / indented behaviour" not really a bug.
The workaround for your particular case is to use: ~'red'or e('red') instead.

@seven-phases-max
Copy link
Member

Regardless of above I actually also wonder myself what was the original purpose of this "immediate and unconditional" string to hex color conversion only for variables (I'm searching through old issues but didn't find the answer yet). Indeed, this all seems to be strange and confusing:

you-have-to-remember-when-its-converted-and-when-its-not {
    @tomato: tomato;
    color:   tomato;         // -> tomato
    color:  @tomato;         // -> #ff6347
    color: (@tomato + #111); // OK
    color:  (tomato + #111); // Error
    // etc.
}

It does not look like something that fits "Today's LESS" naturally. So, perhaps it is a good time to reconsider this behaviour.


Update: I found the corresponding issue (#289) and commits around it.
Well, yes, it's still looks questionable (like "introduced several minor issues here and there in favour of syntaxic sugar"):

  1. The answer for Can't use color function on variable with named color #289 could be just: @alt_color: lighten(color(@main_color), 10%);
  2. And if we really still need this automatic conversion for such function calls it would make sense to move this conversion from variable definition to somewhere near the function call/evaluation itself or so. Though I don't know how complicated it may be to implement.

@lukeapage
Copy link
Member

its really difficult because some people also rely on the fact it is turned into hex.

in your example, it is like that because less is lazy - prop: red is essentially detected as not needing processing and is untouched - it never becomes a colour in the ast.

I'd support changing this to remember what it was encoded as and use that or default to hex (this is what transparent does) as long as we have a function to get to hex or a colour code.

@seven-phases-max
Copy link
Member

I'd support changing this to remember what it was encoded as and use that or default to hex (this is what transparent does) as long as we have a function to get to hex or a colour code.

I see, yes that makes sense (especially if we already have more consistent transparent as reference). So to summarize:

new behaviour {
    @wheat:  wheat;
    color:   wheat;           // -> wheat (1)
    color:  @wheat;           // -> wheat (2)
    color: ( wheat + #111);   // Error    (3)
    color: (@wheat + #111);   // OK       (4)
    color:   spin( wheat, 5); // OK       (5)
    color:   spin(@wheat, 5); // OK       (6)
}

@seven-phases-max
Copy link
Member

Updated: summary of #1604 changes moved to #1604 itself.

@TedDriggs
Copy link
Author

Why is the named color not used with the --compress option? Introducing semantic changes between the normal and minified output seems like it will cause bugs that are hard to diagnose.

@seven-phases-max
Copy link
Member

Why is the named color not used with the --compress option?

Only because I tried to keep the changes as minimal as possible (and I just did not see how bad it could be before I wrote the summary :). Of course I'll change this as soon as we're agree it's not acceptable.

@seven-phases-max
Copy link
Member

--compress option

Fixed with later commits to #1604

@lukeapage
Copy link
Member

Fixed in 2.0 branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants