Class: ColorScheme
Overview
A class that represents a color scheme based on configurable parameters that determine how the RGB values are calculated for a given string segment.
Instance Attribute Summary collapse
-
#base_blue ⇒ Object
Returns the value of attribute base_blue.
-
#base_green ⇒ Object
Returns the value of attribute base_green.
-
#base_red ⇒ Object
Returns the value of attribute base_red.
-
#modulus_blue ⇒ Object
Returns the value of attribute modulus_blue.
-
#modulus_green ⇒ Object
Returns the value of attribute modulus_green.
-
#modulus_red ⇒ Object
Returns the value of attribute modulus_red.
-
#multiplier_blue ⇒ Object
Returns the value of attribute multiplier_blue.
-
#multiplier_green ⇒ Object
Returns the value of attribute multiplier_green.
-
#multiplier_red ⇒ Object
Returns the value of attribute multiplier_red.
Class Method Summary collapse
-
.colorize_path(path) ⇒ String
Applies color codes to each segment of a filesystem path, differentiating the final segment from others using a distinct color scheme.
Instance Method Summary collapse
-
#color_for(segment) ⇒ String
Calculates and returns the ANSI escape code for coloring a string segment based on its hash value.
-
#initialize(base_red, multiplier_red, modulus_red, base_green, multiplier_green, modulus_green, base_blue, multiplier_blue, modulus_blue) ⇒ ColorScheme
constructor
Initializes a new ColorScheme object with base values, multipliers, and moduli for the red, green, and blue components of an RGB color.
Constructor Details
#initialize(base_red, multiplier_red, modulus_red, base_green, multiplier_green, modulus_green, base_blue, multiplier_blue, modulus_blue) ⇒ ColorScheme
Initializes a new ColorScheme object with base values, multipliers, and moduli for the red, green, and blue components of an RGB color.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/color_scheme.rb', line 23 def initialize(base_red, multiplier_red, modulus_red, base_green, multiplier_green, modulus_green, base_blue, multiplier_blue, modulus_blue) @base_red = base_red @multiplier_red = multiplier_red @modulus_red = modulus_red @base_green = base_green @multiplier_green = multiplier_green @modulus_green = modulus_green @base_blue = base_blue @multiplier_blue = multiplier_blue @modulus_blue = modulus_blue end |
Instance Attribute Details
#base_blue ⇒ Object
Returns the value of attribute base_blue.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def base_blue @base_blue end |
#base_green ⇒ Object
Returns the value of attribute base_green.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def base_green @base_green end |
#base_red ⇒ Object
Returns the value of attribute base_red.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def base_red @base_red end |
#modulus_blue ⇒ Object
Returns the value of attribute modulus_blue.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def modulus_blue @modulus_blue end |
#modulus_green ⇒ Object
Returns the value of attribute modulus_green.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def modulus_green @modulus_green end |
#modulus_red ⇒ Object
Returns the value of attribute modulus_red.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def modulus_red @modulus_red end |
#multiplier_blue ⇒ Object
Returns the value of attribute multiplier_blue.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def multiplier_blue @multiplier_blue end |
#multiplier_green ⇒ Object
Returns the value of attribute multiplier_green.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def multiplier_green @multiplier_green end |
#multiplier_red ⇒ Object
Returns the value of attribute multiplier_red.
8 9 10 |
# File 'lib/color_scheme.rb', line 8 def multiplier_red @multiplier_red end |
Class Method Details
.colorize_path(path) ⇒ String
Applies color codes to each segment of a filesystem path, differentiating the final segment from others using a distinct color scheme.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/color_scheme.rb', line 53 def self.colorize_path(path) segments = path.split('/') segments.map.with_index do |segment, index| color_scheme = if index == segments.size - 1 ColorScheme.new(192, 0, 1, 192, 0, 1, 192, 0, 1) else ColorScheme.new(32, 1, 192, 32, 1, 192, 255, 0, 1) end color_scheme.color_for(segment) end.join('/') end |
Instance Method Details
#color_for(segment) ⇒ String
Calculates and returns the ANSI escape code for coloring a string segment based on its hash value.
41 42 43 44 45 46 47 |
# File 'lib/color_scheme.rb', line 41 def color_for(segment) hash_value = segment.each_byte.reduce(0, :+) red = @base_red + (@multiplier_red * (hash_value % @modulus_red)) green = @base_green + (@multiplier_green * (hash_value % @modulus_green)) blue = @base_blue + (@multiplier_blue * (hash_value % @modulus_blue)) "\e[38;2;#{red};#{green};#{blue}m#{segment}\e[0m" end |