Class: Chroma::Color

Inherits:
Object
  • Object
show all
Includes:
Attributes, Modifiers, Serializers, Helpers::Bounders
Defined in:
lib/chroma/color.rb,
lib/chroma/color/modifiers.rb,
lib/chroma/color/attributes.rb,
lib/chroma/color/serializers.rb

Overview

The main class to represent colors.

Defined Under Namespace

Modules: Attributes, Modifiers, Serializers

Instance Attribute Summary

Attributes included from Serializers

#rgb

Attributes included from Attributes

#format

Instance Method Summary collapse

Methods included from Helpers::Bounders

#bound01, #bound_alpha, #clamp01, #to_percentage

Methods included from Modifiers

#brighten, #darken, #desaturate, #grayscale, #lighten, #opacity, #saturate, #spin

Methods included from Serializers

#hsl, #hsv, #to_hex, #to_hex8, #to_hsl, #to_hsv, #to_name, #to_rgb, #to_s

Methods included from Attributes

#alpha, #brightness, #dark?, #light?

Constructor Details

#initialize(input, format = nil) ⇒ Color

Returns a new instance of Color.

Parameters:



11
12
13
14
15
# 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

Instance Method Details

#==(other) ⇒ true, false

Returns true if both are equal in value.

Examples:

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

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

Parameters:

Returns:

  • (true, false)


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

def ==(other)
  to_hex == other.to_hex
end

#complementColor

Returns the complementary color.

Examples:

'red'.paint.complement #=> cyan

Returns:

  • (Color)

    the complementary color



69
70
71
72
73
# 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) ⇒ Array<Color>

Defines a custom palette and immediately returns it. Uses a DSL inside block that mirrors the methods in Modifiers.

Examples:

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

Parameters:

  • block (Proc)

    the palette definition block

Returns:

  • (Array<Color>)

    palette array of colors



97
98
99
# File 'lib/chroma/color.rb', line 97

def custom_palette(&block)
  PaletteBuilder.build(&block).evaluate(self)
end

#eql?(other) ⇒ true, false

Returns true if self is equal to other and they're both instances of Chroma::Color.

Examples:

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

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

Parameters:

Returns:

  • (true, false)


43
44
45
# File 'lib/chroma/color.rb', line 43

def eql?(other)
  self.class == other.class && self == other
end

#paintself

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

Examples:

red = 'red'.paint

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

Returns:

  • (self)


26
27
28
# File 'lib/chroma/color.rb', line 26

def paint
  self
end

#paletteHarmonies

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

Examples:

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

Returns:



81
82
83
# File 'lib/chroma/color.rb', line 81

def palette
  Harmonies.new(self)
end