Module: HeatmapBuilder::ColorHelpers

Defined in:
lib/heatmap_builder/color_helpers.rb

Class Method Summary collapse

Class Method Details

.adjust_lightness(hex_color, factor:) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/heatmap_builder/color_helpers.rb', line 12

def adjust_lightness(hex_color, factor:)
  rgb = hex_to_rgb(hex_color)
  oklch = rgb_to_oklch(*rgb)

  darker_oklch = [oklch[0] * factor, oklch[1], oklch[2]]
  darker_rgb = oklch_to_rgb(*darker_oklch)

  rgb_to_hex(*darker_rgb)
end

.generate_color_palette(from_color, to_color, steps) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/heatmap_builder/color_helpers.rb', line 36

def generate_color_palette(from_color, to_color, steps)
  from_rgb = hex_to_rgb(from_color)
  to_rgb = hex_to_rgb(to_color)

  from_oklch = rgb_to_oklch(*from_rgb)
  to_oklch = rgb_to_oklch(*to_rgb)

  colors = []
  (0...steps).each do |i|
    ratio = i.to_f / (steps - 1)
    interpolated_oklch = interpolate_oklch(from_oklch, to_oklch, ratio)
    interpolated_rgb = oklch_to_rgb(*interpolated_oklch)
    colors << rgb_to_hex(*interpolated_rgb)
  end

  colors
end

.make_color_inactive(hex_color) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/heatmap_builder/color_helpers.rb', line 22

def make_color_inactive(hex_color)
  rgb = hex_to_rgb(hex_color)
  oklch = rgb_to_oklch(*rgb)

  inactive_oklch = [
    oklch[0] * 0.85, # Slightly reduce lightness
    oklch[1] * 0.4,  # Significantly reduce chroma (saturation)
    oklch[2]         # Keep hue unchanged
  ]

  inactive_rgb = oklch_to_rgb(*inactive_oklch)
  rgb_to_hex(*inactive_rgb)
end

.score_to_color(score, colors:) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/heatmap_builder/color_helpers.rb', line 4

def score_to_color(score, colors:)
  return colors.first if score == 0

  max_color_index = colors.length - 1
  color_index = 1 + (score - 1) % max_color_index
  colors[color_index]
end