tesk9 / palette

Define, blend, and generate Colors and Palettes in Elm.
https://package.elm-lang.org/packages/tesk9/palette/latest
BSD 3-Clause "New" or "Revised" License
28 stars 2 forks source link

Add color systems #24

Open tesk9 opened 4 years ago

tesk9 commented 4 years ago

Which color systems should this package support?

Orasund commented 4 years ago

Short answer: CIE-LAB and CIE-LCH.

Long answer: I noticed that your maths functions are not very nice. While looking into algorithms for adding and blending colors, I found out that CIE-LAB is a good system for that(That's the system Photoshop and GIMP are using). There was just one small downside: Calculating the middle of two colors sometimes returns a grayish color. To fix that, CIE-LCH was invented.

For the actual implementation, you can use noahzgordon/elm-color-extra to convert to LAB and then define the following functions:

toCIELCH =
    Convert.colorToLab
        >> (\{ l, a, b } ->
            { l = l
            , c = sqrt (a * a + b * b)
            , h = atan2 b a
            }
        )

fromCIELCH =
    Convert.colorFromLab
        (\{ l, c, h } ->
            { l = l
            , a = c * cos h
            , b = c * sin h
            }
        )
            >> Convert.labToColor