Abachrome is a Ruby gem for parsing, manipulating, and managing colors. It provides a robust set of tools for working with various color formats including hex, RGB, HSL, and named colors.
Installation
Add this line to your application’s Gemfile:
gem 'abachrome'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install abachrome
Usage
Basic Color Creation
# Create colors in different ways
color = Abachrome.from_rgb(1.0, 0.0, 0.0) # Red using RGB values
color = Abachrome.from_hex('#FF0000') # Red using hex
color = Abachrome.from_name('red') # Red using CSS color name
# Create color with alpha
color = Abachrome.from_rgb(1.0, 0.0, 0.0, 0.5) # Semi-transparent red
Color Space Conversion
# Convert between color spaces
rgb_color = Abachrome.from_rgb(1.0, 0.0, 0.0)
oklab_color = rgb_color.to_oklab # Convert to Oklab
rgb_again = oklab_color.to_rgb # Convert back to RGB
Color Output Formats
color = Abachrome.from_rgb(1.0, 0.0, 0.0)
# Different output formats
color.rgb_hex # => "#ff0000"
Abachrome::Outputs::CSS.format(color) # => "#ff0000"
Abachrome::Outputs::CSS.format_rgb(color) # => "rgb(255, 0, 0)"
Parsing Colors
Abachrome supports parsing colors from various string formats:
# Hex colors
color = Abachrome::Parsers::CSS.parse('#ff0000')
color = Abachrome::Parsers::CSS.parse('#f00') # Short hex
# RGB and HSL strings
color = Abachrome::Parsers::CSS.parse('rgb(255, 0, 0)')
color = Abachrome::Parsers::CSS.parse('rgba(255, 0, 0, 0.5)')
color = Abachrome::Parsers::CSS.parse('hsl(0, 100%, 50%)')
# Named colors
color = Abachrome::Parsers::CSS.parse('red')
# Advanced formats
color = Abachrome::Parsers::CSS.parse('oklab(0.5 0.2 -0.1)')
color = Abachrome::Parsers::CSS.parse('oklch(0.5 0.2 120)')
Color Manipulation
color1 = Abachrome.from_rgb(1.0, 0.0, 0.0)
color2 = Abachrome.from_rgb(0.0, 1.0, 0.0)
# Blend two colors
blended = color1.blend(color2, 0.5) # 50% blend
# Adjust lightness
lighter = color1.lighten(0.2) # Lighten by 20%
Features
- Support for multiple color spaces (RGB, HSL, Lab, Oklab, OKLCH, XYZ, LMS)
- Color space conversion between all supported spaces
- Advanced gamut mapping for sRGB, P3, and Rec.2020
- Comprehensive CSS color parsing and formatting
- Support for CSS named colors
- Color blending and lightness adjustments
- Palette creation and interpolation
- High-precision color calculations using BigDecimal
- Alpha channel support throughout all operations
Requirements
- Ruby >= 3.0.0
License
The gem is available as open source under the terms of the MIT License.
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests.
Testing
Tests are run automatically in CI/CD pipelines for all pull requests and pushes to the main branch.
To run tests locally:
rake test
Contributing
We welcome contributions from the community! Here’s how you can help:
Reporting Issues
Please use GitHub issues to report bugs or request features.
Contributing Code
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature
) - Make your changes
- Ensure tests pass (
rake test
) - Follow the code style (run
rubocop
if available) - Commit your changes
- Push to your fork
- Open a pull request
Security
For security-related issues, please contact us directly at [email protected] instead of using public issues.
GitHub
⭐ 3 stars | 📅 Created April 2025 | 🔄 Updated October 2025