Colorist: Color Manipulation For WebHeads

by Michael Bleigh on August 18, 2008 • filed under gem open-source monday colorist color colorizationcomment

While writing color customization code for a recent project, I once again ran into the fact that the existing color gems for Ruby seem to be built for vastly different purposes. To that end, I decided to write a new library for dead-simple manipulation of colors with an emphasis on ease-of-use and being useful for web developers.

Installation

gem install colorist

The Basics

To instantiate a color in Colorist, you use a number of methods:

require 'colorist'
include Colorist

red = Color.new(0xff0000)
red = 0xff0000.to_color
red = "red".to_color
red = "#f00".to_color
red = Color.from_rgb(255,0,0)

Note: I include Colorist in these examples, but there’s no reason you can’t leave it namespaced i.e. Colorist::Color instead.

The idea is to give maximum flexibility without making it complicated. Once you’ve instantiated a color, you can figure out a few tidbits about it and perform some basic operations:

red.brightness # => 0.299
red.r # => 255
red.invert # => #<Color #00ffff>
red.text_color # => #<Color #ffffff>
red.to_s # => "#ff0000"
red.to_s(:rgb) # => "1.000, 0.000, 0.000"

Operations

The real value of Colorist comes in comparison and addition. You can use normal operators with the colors to add them together, subtract them, and compare them based on brightness. You can also do this with the string or numeric representations of colors:

red = 0xff0000.to_color
green = 0x00ff00.to_color
yellow = red + green
yellow = "red".to_color + "green"
yellow.to_s # => "#ffff00"
red - 0.2 # => #<Color #cc0000>

Comparisons work off of the brightness of a given color. You can also calculate the contrast between two colors using the W3C’s formula:

red = "red".to_color
red > "black".to_color # => true
red > "white".to_color # => false
red.contrast_with("green".to_color) # => 0.500653594771242

Get Coloring!

That’s most of the basic functionality, for all of the details you can view the RDoc on RubyForge. The source is available on GitHub and there is a Lighthouse project for any bugs or feature requests. Enjoy!

Comment | 
blog comments powered by Disqus

Words we've written view all blog posts »

Featured Article

Intridea Insider: Ping Yu

by Renae Bair on July 30, 2010

Ping came on board with Intridea during our infancy. He was employee #4, and has been part of the vision and culture of our company from the beginning. He has helped to grow Intridea from a group of four guys working at their dining room tables, to a company of 40+ Ruby and Rails developers and designers that span continents and multiple time zones. His work as Director of Asian Operations aims to bridge our Asian team of Rubyists with our centralized team in the states. "I believe in helping to extend Intridea into China. There is so much talent in China, it feels like a natural thing to do. Intridea has a culture of quality, and I want to bring that to these developers. They love Ruby, and they have so much energy."

Continue reading »

Recent Blog Posts

Intridea Insider: Ping Yu

by Renae Bair on July 30, 2010

Talkin' About Upgrading to Rails 3

by Michael Bleigh on July 26, 2010

A To-Go Plate of Ruby Midwest

by Michael Bleigh on July 19, 2010