mdgriffith / elm-ui

What if you never had to write CSS again?
https://package.elm-lang.org/packages/mdgriffith/elm-ui/latest/
BSD 3-Clause "New" or "Revised" License
1.35k stars 112 forks source link

Feature Request: Color from hex string #217

Open Anton-4 opened 4 years ago

Anton-4 commented 4 years ago

Hex triplets for colors are very common on the web, it would be useful to be able to construct colors from hex strings.

If allowed I would be happy to implement it myself.

mdgriffith commented 4 years ago

One trick you can do in elm: Element.rgb255 0xEE 0xFF 0xEE

So you need to format it slightly differently, but it should work!

I should also add it to the docs.

Anton-4 commented 4 years ago

Ah thank you, that is useful.

In my opinion it would still be beneficial to have a hex function that does not require you to split up the triplet. There are many online design tools that let you directly copy hex codes and it's nice to be able to paste them right in. It also makes it easier for people converting their frontend to elm-ui. It seems likely a large portion of new users would waste some time looking for a hex function.

mdgriffith commented 4 years ago

True! I should definitely add a note in the docs, but pointing to a way to parse hex strings is a good idea as well 👍

Anton-4 commented 4 years ago

I'll try to submit a PR this week.

miniBill commented 4 years ago

Another possibility is creating an Int -> Color function (either in elm-ui or your own codebase) that accepts the RGB triple directly. This is not parsing (so no Maybes) but it's much more comfortable to use when copypasting colors

Anton-4 commented 4 years ago

@miniBill do you mean without spaces like 25500?

Anton-4 commented 4 years ago

@miniBill I have not personally encountered colors being formatted like that, is this not quite rare?

miniBill commented 4 years ago

No, you don't usually find colors written that way, and it would make it impractical if the input is dynamic, but for hardcoded colors it's ok, just do #d000de -> 0xd000de and it's ready to be used

Anton-4 commented 4 years ago

Alright, it does make more sense with the hex formatting. I will add a function for this.