Module: Chroma

Defined in:
lib/chroma.rb,
lib/chroma/color.rb,
lib/chroma/errors.rb,
lib/chroma/version.rb,
lib/chroma/harmonies.rb,
lib/chroma/color_modes.rb,
lib/chroma/rgb_generator.rb,
lib/chroma/palette_builder.rb,
lib/chroma/converters/base.rb,
lib/chroma/color/modifiers.rb,
lib/chroma/helpers/bounders.rb,
lib/chroma/color/attributes.rb,
lib/chroma/color/serializers.rb,
lib/chroma/rgb_generator/base.rb,
lib/chroma/rgb_generator/from_hsl.rb,
lib/chroma/rgb_generator/from_hsv.rb,
lib/chroma/rgb_generator/from_rgb.rb,
lib/chroma/converters/hsl_converter.rb,
lib/chroma/converters/rgb_converter.rb,
lib/chroma/converters/hsv_converter.rb,
lib/chroma/rgb_generator/from_string.rb,
lib/chroma/rgb_generator/from_hsl_values.rb,
lib/chroma/rgb_generator/from_rgb_values.rb,
lib/chroma/rgb_generator/from_hsv_values.rb,
lib/chroma/rgb_generator/from_hex_string_values.rb

Overview

The main module.

Defined Under Namespace

Modules: ColorModes, Converters, Errors, Helpers, RgbGenerator Classes: Color, Harmonies, PaletteBuilder

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.define_palette(name, &block) ⇒ Symbol, String

Defines a custom palette for use by Chroma::Color#palette. Uses a DSL inside block that mirrors the methods in Chroma::Color::Modifiers.

Examples:

'red'.paint.palette.respond_to? :my_palette #=> false

Chroma.define_palette :my_palette do
  spin 60
  spin 120
  spin 240
end

'red'.paint.palette.respond_to? :my_palette #=> true

Raises:


110
111
112
113
114
115
116
# File 'lib/chroma.rb', line 110

def define_palette(name, &block)
  if Harmonies.method_defined? name
    raise Errors::PaletteDefinedError, "Palette `#{name}' already exists"
  end

  PaletteBuilder.build(name, &block)
end

.hex_from_name(name) ⇒ String?

Returns the hexadecimal string representation of a named color and nil if no match is found. Favors 3-character hexadecimal if possible.

Examples:

Chroma.hex_from_name('red')       #=> 'f00'
Chroma.hex_from_name('aliceblue') #=> 'f0f8ff'
Chroma.hex_from_name('foo')       #=> nil

73
74
75
# File 'lib/chroma.rb', line 73

def hex_from_name(name)
  named_colors_map[name]
end

.name_from_hex(hex) ⇒ String?

Returns the color name of a hexadecimal color if available and nil if no match is found. Requires 3-character hexadecimal input for applicable colors.

Examples:

Chroma.name_from_hex('f00')    #=> 'red'
Chroma.name_from_hex('f0f8ff') #=> 'aliceblue'
Chroma.name_from_hex('123123') #=> nil

88
89
90
# File 'lib/chroma.rb', line 88

def name_from_hex(hex)
  hex_named_colors_map[hex]
end

.paint(input) ⇒ Color

Returns a new instance of color. Supports hexadecimal, rgb, rgba, hsl, hsla, hsv, hsva, and named color formats.

Examples:

Chroma.paint('red')
Chroma.paint('#f00')
Chroma.paint('#ff0000')
Chroma.paint('rgb(255, 0, 0)')
Chroma.paint('hsl(0, 100%, 50%)')
Chroma.paint('hsv(0, 100%, 100%)')

59
60
61
# File 'lib/chroma.rb', line 59

def paint(input)
  Color.new(input)
end