lch.cr v0.1.1

A tiny Crystal shard for converting RGB colors to LCH (CIELCh) and back

LCH.cr

Implementation mostly copied from chroma.js

A shard to convert LCH colors to RGB and RGB colors to LCH.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      lch:
        github: homonoidian/lch.cr
    
  2. Run shards install

Usage

require "lch"

LCH.rgb2lch(r: 123, g: 40, b: 123)         # {32.17, 55.27, 327.61}
LCH.lch2rgb(l: 32.17, c: 55.27, h: 327.61) # {123, 40, 123}
# or LCH.rgb2lch([123, 40, 123])
# or LCH.lch2rgb([32.17, 55.27, 327.61])
# or any other Indexable

Several other, related conversion methods are public, but rgb2lch and lch2rgb are the main ones.

Rough benchmark

                        fast path 353.81M (  2.83ns) (± 4.35%)  0.0B/op         fastest
                       rgb to lch   5.69M (175.74ns) (± 0.90%)  0.0B/op   62.18× slower
              lch to rgb, in sRGB   9.63M (103.83ns) (± 1.00%)  0.0B/op   36.74× slower
lch to rgb with fitting into sRGB 486.06k (  2.06µs) (± 0.59%)  0.0B/op  727.92× slower

Your numbers may be different, but in general, fitting into sRGB will always be slower because doing this requires multiple conversions and a loop.

Useful links

P.S

I've just copied a lot of math I don't quite understand. If you do, however, feel free to make corrections & contribute!

It'd be cool to have a nice little mathematically sound LCH library for Crystal, because in practice, it makes dynamic color-choosing fun, and I believe that's exactly how programming graphics should feel!

Also, feel free to add and PR any methods/objects/things you think are related to LCH.

Contributing

  1. Fork it (https://github.com/homonoidian/lch.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Repository

lch.cr

Owner
Statistic
  • 0
  • 0
  • 0
  • 2
  • 0
  • over 1 year ago
  • March 4, 2023
License

MIT License

Links
Synced at

Wed, 06 Nov 2024 19:15:49 GMT

Languages