Class: ColorCode

Inherits:
Object
  • Object
show all
Defined in:
lib/number_to_color.rb

Overview

Class to generate a hex color code based on a value and domain.

Author:

Instance Method Summary collapse

Constructor Details

#initialize(value:, domain: nil, middle_color: [255, 255, 255], end_color: [14, 165, 233], start_color: [248, 113, 113]) ⇒ ColorCode

Returns a new instance of ColorCode.

Parameters:

  • value (Number)

    The table cell's numerical value

  • domain (Array) (defaults to: nil)
    • Two or three-element arrays (e.g., [0, 20], [-20, 0, 20])
  • middle_color (String|Array) (defaults to: [255, 255, 255])
    • The hex or rgb code to use for the middle color (default white).
  • end_color (String|Array) (defaults to: [14, 165, 233])
    • The hex or rgb code to use for the end color (default blue).
  • start_color (String|Array) (defaults to: [248, 113, 113])
    • The hex or rgb code to use for the start color (default red).


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/number_to_color.rb', line 14

def initialize(
  value:,
  domain: nil,
  middle_color: [255, 255, 255],
  end_color: [14, 165, 233],
  start_color: [248, 113, 113]
)
  @value = value.to_f
  @domain = domain
  @middle_color = middle_color
  @start_color = start_color
  @end_color = end_color

  set_colors
end

Instance Method Details

#hex_colorString

The public method to return the hex code.

Returns:

  • (String)

    .



32
33
34
# File 'lib/number_to_color.rb', line 32

def hex_color
  rgb_to_hex(red: final_rgb[:r], green: final_rgb[:g], blue: final_rgb[:b])
end