Class: CSVPlusPlus::Color
- Inherits:
-
Object
- Object
- CSVPlusPlus::Color
- Extended by:
- T::Sig
- 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
-
.valid_hex_string?(hex_string) ⇒ boolean
Is
hex_string
a valid hexadecimal color code? This function will accept input like the 6-digit format: #FF00FF, 00AABB and the shorter 3-digit format: #FFF, 0FA.
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 ‘#’).
Constructor Details
#initialize(hex_string) ⇒ Color
Create an instance from a string like “#FFF” or “#FFFFFF”
rubocop:disable Metrics/CyclomaticComplexity
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/csv_plus_plus/color.rb', line 41 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 } raise(::CSVPlusPlus::Error::CompilerError, "Invalid color: #{hex_string}") unless red_hex && green_hex && blue_hex @red_hex = ::T.let(red_hex, ::String) @green_hex = ::T.let(green_hex, ::String) @blue_hex = ::T.let(blue_hex, ::String) end |
Instance Attribute Details
#blue_hex ⇒ Object (readonly)
Returns the value of attribute blue_hex.
20 21 22 |
# File 'lib/csv_plus_plus/color.rb', line 20 def blue_hex @blue_hex end |
#green_hex ⇒ Object (readonly)
Returns the value of attribute green_hex.
17 18 19 |
# File 'lib/csv_plus_plus/color.rb', line 17 def green_hex @green_hex end |
#red_hex ⇒ Object (readonly)
Returns the value of attribute red_hex.
14 15 16 |
# File 'lib/csv_plus_plus/color.rb', line 14 def red_hex @red_hex end |
Class Method Details
.valid_hex_string?(hex_string) ⇒ boolean
Is hex_string
a valid hexadecimal color code? This function will accept input like the 6-digit format: #FF00FF, 00AABB and the shorter 3-digit format: #FFF, 0FA.
32 33 34 |
# File 'lib/csv_plus_plus/color.rb', line 32 def self.valid_hex_string?(hex_string) !(hex_string.strip =~ ::CSVPlusPlus::Color::HEX_STRING_REGEXP).nil? end |
Instance Method Details
#==(other) ⇒ boolean
87 88 89 90 91 92 |
# File 'lib/csv_plus_plus/color.rb', line 87 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
73 74 75 |
# File 'lib/csv_plus_plus/color.rb', line 73 def blue_percent hex_to_percent(@blue_hex) end |
#green_percent ⇒ Numeric
The percent (decimal between 0-1) of green
65 66 67 |
# File 'lib/csv_plus_plus/color.rb', line 65 def green_percent hex_to_percent(@green_hex) end |
#red_percent ⇒ Numeric
The percent (decimal between 0-1) of red
57 58 59 |
# File 'lib/csv_plus_plus/color.rb', line 57 def red_percent hex_to_percent(@red_hex) end |
#to_hex ⇒ ::String
Create a hex representation of the color (without a ‘#’)
81 82 83 |
# File 'lib/csv_plus_plus/color.rb', line 81 def to_hex [@red_hex, @green_hex, @blue_hex].join end |