Class: Yeah::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/yeah/color.rb

Overview

A Color represents a color.

C is an alias for Color.

Examples:

Comparing two colors

C[100, 100, 100] == C['#646464']
# => true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(red, green, blue) ⇒ Color #initialize(hex_string) ⇒ Color

Returns a new instance of Color.

Overloads:

  • #initialize(red, green, blue) ⇒ Color

    Parameters:

    • red (Numeric)

      value, from 0 to 255

    • green (Numeric)

      value, from 0 to 255

    • blue (Numeric)

      value, from 0 to 255

  • #initialize(hex_string) ⇒ Color

    Parameters:

    • hex (String)

      string in ‘#dadada’ or ‘#fff’ format



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yeah/color.rb', line 28

def initialize(*args)
  if args[0].respond_to?(:[]) && args[0][0] == '#' # hex string
    if args[0].length == 4 # short hex
      @value = args[0][1..3].chars.map { |h| "#{h}#{h}".to_i(16) }
    else # normal hex
      @value = args[0][1..6].scan(/../).map { |h| h.to_i(16) }
    end
  else
    @value = args
  end
end

Instance Attribute Details

#valueArray (readonly)

Returns color value in RGB format.

Returns:

  • (Array)

    color value in RGB format



20
21
22
# File 'lib/yeah/color.rb', line 20

def value
  @value
end

Class Method Details

.[](*args) ⇒ Vector

Alias for ::new.

Parameters:

  • arguments

    catch-all

Returns:



14
15
16
# File 'lib/yeah/color.rb', line 14

def [](*args)
  new(*args)
end

Instance Method Details

#==(other) ⇒ Boolean

Returns whether self matches other color.

Returns:

  • (Boolean)

    whether self matches other color



46
47
48
# File 'lib/yeah/color.rb', line 46

def ==(other)
  value == other.value
end

#inspectString

Returns readable representation.

Returns:

  • (String)

    readable representation



41
42
43
# File 'lib/yeah/color.rb', line 41

def inspect
  "#{self.class.name}#{value.to_s}"
end

#to_hexString

Returns color value as a hex string.

Returns:

  • (String)

    color value as a hex string



51
52
53
# File 'lib/yeah/color.rb', line 51

def to_hex
  "##{value.map { |v| v.to_s(16).rjust(2, '0') }.join }"
end