class Chroma::Color

The main class to represent colors.

Public Class Methods

new(input, format = nil) click to toggle source

@param input [String, ColorModes::Rgb, ColorModes::Hsl, ColorModes::Hsv] @param format [Symbol] the color mode format

# File lib/chroma/color.rb, line 11
def initialize(input, format = nil)
  @input = input
  @rgb, gen_format = generate_rgb_and_format(input)
  @format = format || gen_format
end

Public Instance Methods

==(other) click to toggle source

Returns true if both are equal in value.

@example

red = 'red'.paint
blue = 'blue'.paint

red == red          #=> true
red == blue         #=> false
red == '#f00'.paint #=> true

@param other [Color] @return [true, false]

# File lib/chroma/color.rb, line 59
def ==(other)
  to_hex == other.to_hex
end
complement() click to toggle source

Returns the complementary color.

@example

'red'.paint.complement #=> cyan

@return [Color] the complementary color

# File lib/chroma/color.rb, line 69
def complement
  hsl = self.hsl
  hsl.h = (hsl.h + 180) % 360
  self.class.new(hsl, @format)
end
custom_palette(&block) click to toggle source

Defines a custom palette and immediately returns it. Uses a DSL inside `block` that mirrors the methods in {Color::Modifiers}.

@example

'red'.paint.custom_palette do
  spin 60
  spin 180
end
#=> [red, yellow, cyan]

@param block [Proc] the palette definition block @return [Array<Color>] palette array of colors

# File lib/chroma/color.rb, line 97
def custom_palette(&block)
  PaletteBuilder.build(&block).evaluate(self)
end
eql?(other) click to toggle source

Returns true if `self` is equal to `other` and they're both instances of {Color}.

@example

red = 'red'.paint
blue = 'blue'.paint

red.eql? red          #=> true
red.eql? blue         #=> false
red.eql? '#f00'.paint #=> true

@param other [Color] @return [true, false]

# File lib/chroma/color.rb, line 43
def eql?(other)
  self.class == other.class && self == other
end
paint() click to toggle source

Returns self. Useful for ducktyping situations with {String#paint}.

@example

red = 'red'.paint

red.paint            #=> red
red.paint.equal? red #=> true

@return [self]

# File lib/chroma/color.rb, line 26
def paint
  self
end
palette() click to toggle source

Returns an instance of {Harmonies} from which to call a palette method.

@example

'red'.paint.palette #=> #<Chroma::Harmonies:0x007faf6b9f9148 @color=red>

@return [Harmonies]

# File lib/chroma/color.rb, line 81
def palette
  Harmonies.new(self)
end

Private Instance Methods

generate_rgb_and_format(input) click to toggle source
# File lib/chroma/color.rb, line 107
def generate_rgb_and_format(input)
  RgbGenerator.generate_rgb_and_format(input)
end
to_2char_hex(n) click to toggle source
# File lib/chroma/color.rb, line 103
def to_2char_hex(n)
  n.round.to_s(16).rjust(2, '0')
end