Skip to content
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

Random colour generator / random item selector #732

Closed
wants to merge 3 commits into from
Closed

Random colour generator / random item selector #732

wants to merge 3 commits into from

Conversation

Synchro
Copy link
Member

@Synchro Synchro commented Mar 23, 2012

Adds two very simple functions, randomcolor and pickrandom.

randomcolor generates a completely random opaque colour.
pickrandom selects a random item (of any kind) from a list.

Usage is like this:

color: randomcolor();
background-color: pickrandom(pink, orange, blue, green, #176dea);
background-image: pickrandom(url(images/snow.png), url(images/water.png));

output from this might be:

color: #6b265c;
background-color: #0000ff;
background-image: url(images/snow.png);

There are no test cases for these functions because, by definition, the output is not predictable! I foresee randomcolor function being useful in generating random colour palettes, where an initial random colour can be used to derive others via LESS's existing colour manipulation functions and my contrast function in #730.
pickrandom has all kinds of uses since it can randomise anything you put into it.

@Synchro
Copy link
Member Author

Synchro commented Mar 23, 2012

I just realised that the 'pick from params' version of this function is completely general - since it just returns one of whatever you passed in, it doesn't care what they are, so you could use it for all sorts of things, not just colours. Polymorphism FTW!

Because of that I've amended my code to split it out as a separate function and amended the description above.

@sashasklar
Copy link
Contributor

It's a fun feature but unless I'm missing something it would rely on using less.js client-side.

@Synchro
Copy link
Member Author

Synchro commented Mar 29, 2012

Not necessarily; I'm intending to use it for generating per-user styles server-side (every user gets their own LESS stylesheet).

@sashasklar
Copy link
Contributor

Gotcha! Like I said I think it's a fun feature but I don't believe it belongs in LESS proper. LESS should work the same whether it's parsed on the client, dynamically on the server or compiled as part of a build process. A given LESS stylesheet as an input should have one CSS output.

@Synchro
Copy link
Member Author

Synchro commented Apr 2, 2012

It does work identically in all environments; the only thing that's different is what you do with the resulting CSS - on the server-side you usually keep it for a while; client side you tend to throw it away. In my case I'm not that interested in client-side, but I'm planning on regular server-side recompilation.
Just because it's non-deterministic doesn't make it inapplicable or invalid: a random function that always produces the same result isn't useful. For another example, a function that picks different values according to time of day or day of the week could be compiled hourly/daily on the server-side. Seems pretty reasonable to me, but it's not really any different.
Random selection is a control structure much like conditions, which less also lacks.
Without this I'd have to introduce a whole additional processing layer for style sheets in PHP or something which seems silly when I already have a CSS processing layer.

@lukeapage
Copy link
Member

@MatthewDL

I'm pro this one as well - but have no strong opinions at-all.

alternatively close and continue working towards being able to inject/plugin functions easily. what do you think?

@Synchro
Copy link
Member Author

Synchro commented Dec 13, 2012

I'd forgotten about this one; It is kind of harmless 😄
I think of this more as a control structure sort of thing rather than a function, not dissimilar to the sentiment behind #318 really. Is LESS aiming to have proper conditionals at some point?

@matthew-dean
Copy link
Member

It feels kinda ugly, especially a magic randomcolor() function. Features are never harmless. Each function we add risks damaging LESS's simplicity, and I don't see people clamoring for this one. I think this is a good proof-of-concept for plugins, though.

@lukeapage
Copy link
Member

Let's close it then and leave for future work with plugins

@lukeapage
Copy link
Member

Since we closed this I have seen bugs from 2 different people using javascript just to generate random numbers.

@ricardobeat
Copy link
Contributor

How's the plugin work going?

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

Successfully merging this pull request may close these issues.

None yet

5 participants