lch
Version, currently 0.1.12 versions
- 0.1.1latestMar 5, 2023
- 0.1.0not indexedMar 5, 2023
github.com/homonoidian/lch.cr
A tiny Crystal shard for converting RGB colors to LCH (CIELCh) and back
0 stars
1 dependent
License: MIT
Installation
# Add this to your shard.yml
dependencies:
lch:
github: homonoidian/lch.cr
version: ~> 0.1.1Then run:
shards installshard.yml
- Crystal
>= 1.7.0- License
- MIT
- Author
- Alexey Yurchenko <homonoidian@yandex.ru>
Dependencies
This version declares no dependencies.
README
# LCH.cr
> Implementation mostly copied from [chroma.js](https://github.com/gka/chroma.js)
A shard to convert LCH colors to RGB and RGB colors to LCH.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
lch:
github: homonoidian/lch.cr
```
2. Run `shards install`
## Usage
```crystal
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
```text
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
* An LCH [color picker](https://lch.oklch.com/)
* Another LCH [color picker](https://css.land/lch/)
* [LCH colors in CSS: what, why, and how?](https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/)
* [LCH is the best color space!](https://atmos.style/blog/lch-color-space)
## 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
- [Alexey Yurchenko](https://github.com/homonoidian) - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.1- Tagged
- Mar 5, 2023
- Commit
93eb38bc51b6- Crystal
>= 1.7.0- Indexed
- yes
Dependents
Repository
github.com/homonoidian/lch.cr
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 12, 2026
- Synced
- Aug 12, 2026
- Versions
- 2