Class: ColorScheme

Inherits:
Object show all
Defined in:
lib/color_scheme.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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_blueObject

Returns the value of attribute base_blue.



8
9
10
# File 'lib/color_scheme.rb', line 8

def base_blue
  @base_blue
end

#base_greenObject

Returns the value of attribute base_green.



8
9
10
# File 'lib/color_scheme.rb', line 8

def base_green
  @base_green
end

#base_redObject

Returns the value of attribute base_red.



8
9
10
# File 'lib/color_scheme.rb', line 8

def base_red
  @base_red
end

#modulus_blueObject

Returns the value of attribute modulus_blue.



8
9
10
# File 'lib/color_scheme.rb', line 8

def modulus_blue
  @modulus_blue
end

#modulus_greenObject

Returns the value of attribute modulus_green.



8
9
10
# File 'lib/color_scheme.rb', line 8

def modulus_green
  @modulus_green
end

#modulus_redObject

Returns the value of attribute modulus_red.



8
9
10
# File 'lib/color_scheme.rb', line 8

def modulus_red
  @modulus_red
end

#multiplier_blueObject

Returns the value of attribute multiplier_blue.



8
9
10
# File 'lib/color_scheme.rb', line 8

def multiplier_blue
  @multiplier_blue
end

#multiplier_greenObject

Returns the value of attribute multiplier_green.



8
9
10
# File 'lib/color_scheme.rb', line 8

def multiplier_green
  @multiplier_green
end

#multiplier_redObject

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