Ultraviolet - chroma.js-like color manipulation library

Ultraviolet is an Elixir color manipulation library designed to work like chroma-js. It does not have full parity with chroma-js, but it includes most of the common operations and features.

Getting started

Here are a few things it can do:

  • read colors from a wide range of inputs
  • analyze and manipulate colors
  • convert colors into a wide range of formats
  • linear, bezier, and custom interpolation in different color spaces

Here’s an example of a simple read / manipulate / output chain

{:ok, color} = Ultraviolet.new("pink")

color
|> Ultraviolet.Color.darken!()
|> Ultraviolet.Color.saturate!(2)
|> Ultraviolet.Color.hex()
#=> "#ff6d93"

Aside from that, ultraviolet can help you generate nice colors using various methods. These colors can be used, for example, as a color palette for maps or data visualization.

{:ok, scale} = Ultraviolet.scale(["#fafa6e", "#2a4858"], space: :lch)
colors = Ultraviolet.Scale.take(scale, 6)

This library has a lot more to offer, though; the documentation contains more examples of how to use it.


Background

I’m working on a project that requires creating nice-looking gradients and other cover images. I explored a number of related tools online, such as Cool Backgrounds and Trianglify. I noticed that pretty much all of them use chroma-js for color manipulation, so I took a look at the library. I was pleasantly surprised: the API is functional and well-documented with plenty of examples and test cases. I also noticed that there wasn’t a fully-featured color manipulation library in Elixir yet, so I felt the urge to try porting some of chroma.js over to Elixir to use with the project (a Phoenix website).

That was last Thursday. Since then, I’ve brought over most of the core chroma-js functionality, but made some API adjustments so it better fits the “Elixir style”. It’s not quite “finished” yet, and I probably need to do more testing and fine-tuning, but I feel like it’s robust and stable enough that I can release it as a library. If you need to manipulate colors for your app, please give ultraviolet a try and let me know what you think!

Acknowledgements

Huge thanks to the chroma.js creator for making such a wonderful and well-documented library. If you haven’t seen the chroma-js documentation, I’d recommend giving it a read.

15 Likes