Class: CSVPlusPlus::Color
- Inherits:
-
Object
- Object
- CSVPlusPlus::Color
- Defined in:
- lib/csv_plus_plus/color.rb
Overview
A color value parsed into it’s respective components
attr_reader blue_hex [String] The blue value in hex (“FF”, “00”, “AF”, etc) attr_reader green_hex [String] The green value in hex (“FF”, “00”, “AF”, etc) attr_reader red_hex [String] The red value in hex (“FF”, “00”, “AF”, etc)
Constant Summary collapse
- HEX_STRING_REGEXP =
/^#?([0-9a-f]{1,2})([0-9a-f]{1,2})([0-9a-f]{1,2})/i
Instance Attribute Summary collapse
-
#blue_hex ⇒ Object
readonly
Returns the value of attribute blue_hex.
-
#green_hex ⇒ Object
readonly
Returns the value of attribute green_hex.
-
#red_hex ⇒ Object
readonly
Returns the value of attribute red_hex.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ boolean
-
#blue_percent ⇒ Numeric
The percent (decimal between 0-1) of blue.
-
#green_percent ⇒ Numeric
The percent (decimal between 0-1) of green.
-
#initialize(hex_string) ⇒ Color
constructor
Create an instance from a string like “#FFF” or “#FFFFFF”.
-
#red_percent ⇒ Numeric
The percent (decimal between 0-1) of red.
-
#to_hex ⇒ ::String
Create a hex representation of the color (without a ‘#’).
- #to_s ⇒ ::String
Constructor Details
#initialize(hex_string) ⇒ Color
Create an instance from a string like “#FFF” or “#FFFFFF”
23 24 25 26 27 |
# File 'lib/csv_plus_plus/color.rb', line 23 def initialize(hex_string) @red_hex, @green_hex, @blue_hex = hex_string.strip.match(::CSVPlusPlus::Color::HEX_STRING_REGEXP) &.captures &.map { |s| s.length == 1 ? s + s : s } end |
Instance Attribute Details
#blue_hex ⇒ Object (readonly)
Returns the value of attribute blue_hex.
10 11 12 |
# File 'lib/csv_plus_plus/color.rb', line 10 def blue_hex @blue_hex end |
#green_hex ⇒ Object (readonly)
Returns the value of attribute green_hex.
10 11 12 |
# File 'lib/csv_plus_plus/color.rb', line 10 def green_hex @green_hex end |
#red_hex ⇒ Object (readonly)
Returns the value of attribute red_hex.
10 11 12 |
# File 'lib/csv_plus_plus/color.rb', line 10 def red_hex @red_hex end |
Class Method Details
.valid_hex_string?(hex_string) ⇒ boolean
16 17 18 |
# File 'lib/csv_plus_plus/color.rb', line 16 def self.valid_hex_string?(hex_string) !(hex_string.strip =~ ::CSVPlusPlus::Color::HEX_STRING_REGEXP).nil? end |
Instance Method Details
#==(other) ⇒ boolean
63 64 65 66 67 68 |
# File 'lib/csv_plus_plus/color.rb', line 63 def ==(other) other.is_a?(self.class) && other.red_hex == @red_hex && other.green_hex == @green_hex && other.blue_hex == @blue_hex end |
#blue_percent ⇒ Numeric
The percent (decimal between 0-1) of blue
46 47 48 |
# File 'lib/csv_plus_plus/color.rb', line 46 def blue_percent hex_to_percent(@blue_hex) end |
#green_percent ⇒ Numeric
The percent (decimal between 0-1) of green
39 40 41 |
# File 'lib/csv_plus_plus/color.rb', line 39 def green_percent hex_to_percent(@green_hex) end |
#red_percent ⇒ Numeric
The percent (decimal between 0-1) of red
32 33 34 |
# File 'lib/csv_plus_plus/color.rb', line 32 def red_percent hex_to_percent(@red_hex) end |
#to_hex ⇒ ::String
Create a hex representation of the color (without a ‘#’)
53 54 55 |
# File 'lib/csv_plus_plus/color.rb', line 53 def to_hex [@red_hex, @green_hex, @blue_hex].join end |
#to_s ⇒ ::String
58 59 60 |
# File 'lib/csv_plus_plus/color.rb', line 58 def to_s "Color(r: #{@red_hex}, g: #{@green_hex}, b: #{@blue_hex})" end |