Class: CSVPlusPlus::Color

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hex_string) ⇒ Color

Create an instance from a string like “#FFF” or “#FFFFFF”

Parameters:

  • hex_string (String)

    The hex string input to parse



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_hexObject (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_hexObject (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_hexObject (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

Returns:

  • (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

Returns:

  • (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_percentNumeric

The percent (decimal between 0-1) of blue

Returns:

  • (Numeric)


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

def blue_percent
  hex_to_percent(@blue_hex)
end

#green_percentNumeric

The percent (decimal between 0-1) of green

Returns:

  • (Numeric)


39
40
41
# File 'lib/csv_plus_plus/color.rb', line 39

def green_percent
  hex_to_percent(@green_hex)
end

#red_percentNumeric

The percent (decimal between 0-1) of red

Returns:

  • (Numeric)


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 ‘#’)

Returns:

  • (::String)


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

Returns:

  • (::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