Class: Yeah::Color
- Inherits:
-
Object
- Object
- Yeah::Color
- Defined in:
- lib/yeah/color.rb
Overview
A Color represents a color.
C is an alias for Color.
Instance Attribute Summary collapse
-
#value ⇒ Array
readonly
Color value in RGB format.
Class Method Summary collapse
-
.[](*args) ⇒ Vector
Alias for ::new.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Whether self matches other color.
-
#initialize(*args) ⇒ Color
constructor
A new instance of Color.
-
#inspect ⇒ String
Readable representation.
-
#to_hex ⇒ String
Color value as a hex string.
Constructor Details
#initialize(red, green, blue) ⇒ Color #initialize(hex_string) ⇒ Color
Returns a new instance of Color.
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
#value ⇒ Array (readonly)
Returns 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.
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.
46 47 48 |
# File 'lib/yeah/color.rb', line 46 def ==(other) value == other.value end |
#inspect ⇒ String
Returns readable representation.
41 42 43 |
# File 'lib/yeah/color.rb', line 41 def inspect "#{self.class.name}#{value.to_s}" end |
#to_hex ⇒ String
Returns 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 |