cdepillabout / termonad

Terminal emulator configurable in Haskell.
https://hackage.haskell.org/package/termonad
BSD 3-Clause "New" or "Revised" License
393 stars 47 forks source link

Support create colors from hex #240

Closed albertodvp closed 10 months ago

albertodvp commented 10 months ago

Hello! :wave: Thank you for this repo!

I'm configuring termonad using haskell, I see there is this function to create colours from Word8

createColour
  :: Word8 -- ^ red channel
  -> Word8 -- ^ green channel
  -> Word8 -- ^ blue channel
  -> AlphaColour Double

I don't find any way to create colors from the hex. Am I missing something?

Assuming there are no ways to do that, would you find it valuable to add something like this?

createColour' :: String -> AlphaColour Double

Obviously with:

createColour' . sRGB32show == id

Probably that String (the hex of the colour) could be wrapped in a better type.

I could open a PR for this if you find this valuable/useful 😊

cdepillabout commented 10 months ago

I don't find any way to create colors from the hex. Am I missing something?

Would you be able to give a little more information about what you're trying to do?

You can create colors from hex by using hex literals:

> import Termonad.Config.Colour
> createColour 0xff 0x10 0xaa
Data.Colour.SRGB.Linear.rgb 1.0 5.181516702338386e-3 0.4019777798321958 `withOpacity` 1.0

Did you want to be able to create colors with a string like #ff10aaff? If so, I wonder if this is something that would be accepted into the colour library? It sounds like something that may help all users of the colour package, and not something Termonad-specific.

albertodvp commented 10 months ago

Did you want to be able to create colors with a string like #ff10aaff?

This is what I had in mind, sorry for my lack of precision. I think it would be convenient to use the string directly in the configuration file.

If so, I wonder if this is something that would be accepted into the colour library? It sounds like something that may help all users of the colour package, and not something Termonad-specific.

That's definitely a good point, I'll try to see if that could be useful there, I think we can close this in the meantime.

Thanks!

roconnor commented 10 months ago

It would have to be a literal string for input right? So maybe extending Data.Colour.Names.readColourName to parse strings beginning with # as hex colours?

albertodvp commented 10 months ago

I wouldn't pollute the readColourName because the hex strings are not "names" and I would keep those 2 different classes of strings (color names and hexadecimal strings) separated. But yes, the signature is the one I'd expect:

(Fail.MonadFail m, Ord a, Floating a) => String -> m (Colour a)
roconnor commented 10 months ago

Names don't begin with "#", so the pollution isn't necessarily bad.

If there were a separate function, would it require that strings begin with #? I'm thinking yes because then you can compose readColourName and readColourHex with <|>.

albertodvp commented 10 months ago

Names don't begin with "#", so the pollution isn't necessarily bad.

That's right!

If there were a separate function, would it require that strings begin with #? I'm thinking yes because then you can compose readColourName and readColourHex with <|>.

Good point! Yes, I'd require that input of readColourHex starts with #.

Anyway @roconnor I think you know the library better so you'd know better what's the best solution :smiley_cat:

roconnor commented 10 months ago

Oh. It looks like https://hackage.haskell.org/package/colour-2.3.6/docs/Data-Colour-SRGB.html#v:sRGB24read already does this.

albertodvp commented 10 months ago

Oh. It looks like https://hackage.haskell.org/package/colour-2.3.6/docs/Data-Colour-SRGB.html#v:sRGB24read already does this.

Oh, my bad I've lost it :disappointed: Thank you!