Chroma
Chroma is a color manipulation and palette generation library. It is heavily
inspired by and a very close Ruby port of the
tinycolor.js
library. Many thanks to Brian Grinstead
for his hard work on that library.
Please don't hesitate to examine the code and make issues, feature requests,
or pull requests. Please refer to the Contributing section
below.
Installation
Add this line to your application's Gemfile:
gem 'chroma'
And then execute:
$ bundle
Or install it yourself as:
$ gem install chroma
Creating Colors
Colors are created via the Chroma.paint
method. It expects any one of
many possible color formats as a string, including names, hexadecimal, rgb,
hsl, and hsv. As a convenience, a String#paint
is also available for more
succinct color creation.
Chroma.paint 'red'
Chroma.paint '#00ff00'
Chroma.paint '#00f'
Chroma.paint 'rgb(255, 255, 0)'
Chroma.paint 'rgba(255, 255, 0, 0.5)'
Chroma.paint 'hsl(60, 100%, 50%)'
Chroma.paint 'hsl(60, 1, 0.5)'
Chroma.paint 'hsla(60, 100%, 50%, 0.5)'
Chroma.paint 'hsv(60, 100%, 50%)'
Chroma.paint 'hsv(60, 1, 0.5)'
Chroma.paint 'hsva(60, 100%, 50%, 0.75)'
'red'.paint
'#00ff00'.paint
'#00f'.paint
'rgb(255, 255, 0)'.paint
'rgba(255, 255, 0, 0.5)'.paint
'hsl(60, 100%, 50%)'.paint
'hsla(60, 100%, 50%, 0.5)'.paint
'hsv(60, 100%, 50%)'.paint
'hsva(60, 100%, 50%. 0.5)'.paint
Motivation
Chroma's major strength is manipulating colors and generating color palettes,
which allows you to easily generate dynamic colors, dynamic themes for a web
application, and more.
Color Manipulation
Lighten
Lighten the color by a given amount. Defaults to 10.
'red'.paint.lighten
'red'.paint.lighten(20)
Brighten
Brighten the color by a given amount. Defaults to 10.
'red'.paint.brighten
'red'.paint.brighten(20)
Darken
Darken the color by a given amount. Defaults to 10.
'red'.paint.darken
'red'.paint.darken(20)
Desaturate
Desaturate the color by a given amount. Defaults to 10.
'red'.paint.desaturate
'red'.paint.desaturate(20)
Saturate
Saturate the color by a given amount. Defaults to 10.
'#123'.paint.saturate
'#123'.paint.saturate(20)
Grayscale
Convert the color to grayscale.
'green'.paint.grayscale
'red'.paint.greyscale
Opacity
Set the opacity of the color to a given amount.
'red'.paint.opacity(0.3)
'red'.paint.opacity(0.3).to_rgb
Spin
Spin a given amount in degrees around the hue wheel.
'red'.paint.spin(30)
'red'.paint.spin(60)
'red'.paint.spin(90)
Generating Palettes
Chroma's most powerful feature is palette generation. You can use the default
palettes available or even create your own custom palettes.
Palette methods are available via Color#palette
and by default output an
array of colors. If you want the underlying color strings, you can pass in
the desired format via the :as
option.
Available Formats
- name
- rgb
- hex
- hex6 (alias for hex)
- hex3
- hex8 (includes the alpha value in the highest order byte)
- hsl
- hsv
Complement
Generate a complement palette.
'red'.paint.palette.complement
'red'.paint.palette.complement(as: :name)
'red'.paint.palette.complement(as: :hex)
Triad
Generate a triad palette.
'red'.paint.palette.triad
'red'.paint.palette.triad(as: :name)
'red'.paint.palette.triad(as: :hex)
Tetrad
Generate a tetrad palette.
'red'.paint.palette.tetrad
'red'.paint.palette.tetrad(as: :name)
'red'.paint.palette.tetrad(as: :hex)
Split Complement
Generate a split complement palette.
'red'.paint.palette.split_complement
'red'.paint.palette.split_complement(as: :name)
'red'.paint.palette.split_complement(as: :hex)
Analogous
Generate an analogous palette. Pass in a :size
option to specify the size
of the palette (defaults to 6). Pass in a :slice_by
option to specify the
angle size to slice into the hue wheel (defaults to 30 degrees).
'red'.paint.palette.analogous
'red'.paint.palette.analogous(as: :hex)
'red'.paint.palette.analogous(size: 3)
'red'.paint.palette.analogous(size: 3, slice_by: 60)
Monochromatic
Generate a monochromatic palette. Pass in a :size
option to specify the size
of the palette (defaults to 6).
'red'.paint.palette.monochromatic
'red'.paint.palette.monochromatic(as: :hex)
'red'.paint.palette.monochromatic(size: 3)
Defining Custom Palettes
Chroma allows you to define your own custom palettes if the default ones aren't
all you're looking for. You can define a custom palette by calling
Chroma.define_palette
, passing in a palette name and definition block. The
definition block uses the color manipulation methods (i.e. lighten
, spin
,
etc.) as its DSL. Every DSL call defines a new color that will be included
in the palette. Your seed color (i.e. the color from which you call the
palette method) will be included as the first color in your palette too.
red = 'red'.paint
red.palette.respond_to? :my_palette
Chroma.define_palette :my_palette do
spin 60
spin 180
spin(60).brighten(20)
greyscale
end
red.palette.respond_to? :my_palette
red.palette.my_palette
Dynamic Custom Palettes
You can generate custom palettes on the fly too with
Chroma::Color#custom_palette
.
'red'.paint.custom_palette do
spin 60
spin 180
end
Serializing Colors
Colors offer several methods to output to different string color formats.
Method | Description |
---|
to_hsv | output to hsv string, outputs hsva if alpha < 1 |
to_hsl | output to hsl string, outputs hsla if alpha < 1 |
to_hex | output to hex string, optional argument allows 3-character hex output if possible |
to_hex8 | output to 8-character hex string with alpha value in the highest order byte |
to_rgb | output to rgb string, outputs rgba if alpha < 1 |
to_name | output to color name string if available, otherwise '<unknown>' or to_hex output based on optional arg value |
to_s | output to the appropriate string format based on how the color was created, optional arg forces the format |
'red'.paint.to_hsv
'rgba(255, 0, 0, 0.5)'.paint.to_hsv
'red'.paint.to_hsl
'rgba(255, 0, 0, 0.5)'.paint.to_hsl
'red'.paint.to_hex
'red'.paint.to_hex(true)
'rgba(255, 0, 0, 0.5)'.paint.to_hex
'red'.paint.to_hex
'rgba(255, 0, 0, 0.5)'.paint.to_hex
'red'.paint.to_rgb
'rgba(255, 0, 0, 0.5)'.paint.to_rgb
'red'.paint.to_name
'#00f'.paint.to_name
'rgba(255, 0, 0, 0.5)'.paint.to_name
'#123'.paint.to_name(true)
'red'.paint.to_s
'rgb(255, 0, 0)'.paint.to_s
'#f00'.paint.to_s
'#80ff0000'.paint.to_s(:rgb)
Other Methods
Colors also have a few other helper methods:
Method | Description |
---|
dark? | is the color dark? |
light? | is the color light? |
alpha | retrieve the alpha value |
brightness | calculate the brightness as a number between 0 and 255 |
complement | return the complementary color |
'red'.paint.dark?
'yellow'.paint.dark?
'red'.paint.light?
'yellow'.paint.light?
'red'.paint.alpha
'rgba(0, 0, 0, 0.5)'.paint.alpha
'red'.paint.brightness
'yellow'.paint.brightness
'white'.paint.brightness
'black'.paint.brightness
'red'.paint.complement
Contributing
Please branch from dev for all pull requests.
- Fork it (https://github.com/jfairbank/chroma/fork)
- Checkout dev (
git checkout dev
) - Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new pull request against dev