The project is in a healthy, maintained state
Convert colors to hex/rgb/hsv/cmyk/hsl/xyz/cielab/oklch
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

Color Converters

Give me a color and I'll convert it.

Color Converters is an ruby gem package for use in ruby or other projects that provides conversions for colors to other color spaces. Given a color in Hexadecimal, RGA(A), HSL(A), HSV, HSB, CMYK, XYZ, CIELAB, or OKLCH format, it can convert the color to those other spaces.

Lab and LCH color spaces are special in that the perceived difference between two colors is proportional to their Euclidean distance in color space. This special property, called perceptual uniformity, makes them ideal for accurate visual encoding of data. In contrast, the more familiar RGB and HSL color spaces distort data when used for visualization.

Converters

Colors can be converted between the following spaces:

  • hex
  • rgb(a)
  • hsl(a)
  • hsv/hsb
  • cmyk
  • xyz
  • cielab
  • oklch
  • name

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add color_converters

If bundler is not being used to manage dependencies, install the gem by executing:

gem install color_converters

Usage

Initialize a color:

# from hex
color = ColorConverters::Color.new("#3366cc")
color = ColorConverters::Color.new("#36c")

# from rgb(a)
color = ColorConverters::Color.new(r: 51, g: 102, b: 204)
color = ColorConverters::Color.new(r: 51, g: 102, b: 204, a: 0.5)

# from hsl(a)
color = ColorConverters::Color.new(h: 225, s: 73, l: 57)
color = ColorConverters::Color.new(h: 225, s: 73, l: 57, a: 0.5)

# from hsv/hsb
color = ColorConverters::Color.new(h: 220, s: 75, v: 80)
color = ColorConverters::Color.new(h: 220, s: 75, b: 80)

# from cmyk
color = ColorConverters::Color.new(c: 74, m: 58, y: 22, k: 3)

# from xyz
color = ColorConverters::Color.new(x: 16, y: 44, z: 32)

# from cielab
color = ColorConverters::Color.new(l: 16, a: 44, b: 32, space: :cie)

# from cielch
color = ColorConverters::Color.new(l: 16, c: 44, h: 32, space: :cie)

# from textual color
color = ColorConverters::Color.new("blue")

# from a css rgb(a) string
color = ColorConverters::Color.new("rgb(51, 102, 204)")
color = ColorConverters::Color.new("rgba(51, 102, 204, 0.5)")

# from a css hsl(a) string
color = ColorConverters::Color.new("hsl(225, 73%, 57%)")
color = ColorConverters::Color.new("hsl(225, 73%, 57%, 0.5)")

Converters

color = ColorConverters::Color.new(r: 70, g: 130, b: 180, a: 0.5)

color.alpha
=> 0.5

color.rgb
=> {:r=>70, :g=>130, :b=>180}

color.hsl
=> {:h=>207, :s=>44, :l=>49}

color.hsv
=> {:h=>207, :s=>61, :v=>71}

color.hsb
=> {:h=>207, :s=>61, :b=>71}

color.cmyk
=> {:c=>61, :m=>28, :y=>0, :k=>29}

color.xyz
=> {:x=>33, :y=>21, :z=>54}

color.cielab
=> {:l=>52.47, :a=>-4.08, :b=>-32.19}

color.cielch
=> {:l=>52.47, :c=>32.45, :h=>262.78}

color.hex
=> "#4682b4"

color.name
=> "steelblue"

Options

space

As there are certain color spaces that use the same letter keys, there needed to be a way to different between those space. The space parameter allows that, with examples in the usage code above

ColorConverters::Color.new(l: 64, a: 28, b: -15, space: :cie)

limit_override

By default all values are checked to be within the expected number ranges, i.e.; rgb between 0-255 each. This parameter allows you to ignore those ranges and submit any values you want.

ColorConverters::Color.new(r: 270, g: 1300, b: 380, a: 0.5, limit_override: true)

Development

Converting Colors can be usef to help verify results. Different calculators use different exponents and standards so there can be discrepency across them (like this calculator for LCH).

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/louiswdavis/color_converters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

Forked from original gem by Derek DeVries. The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the ColorConverters project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.