Module: Trackplot::ColorScale

Defined in:
lib/trackplot/color_scale.rb

Class Method Summary collapse

Class Method Details

.categorical(hex, count: 8) ⇒ Object

Evenly-spaced hues, preserving base color’s saturation and lightness.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/trackplot/color_scale.rb', line 46

def categorical(hex, count: 8)
  validate_hex!(hex)
  return [] if count == 0

  h, s, l = hex_to_hsl(hex)
  step = 360.0 / count

  (0...count).map do |i|
    hue = (h + step * i) % 360
    hsl_to_hex(hue, s, l)
  end
end

.diverging(hex1, hex2, count: 8) ⇒ Object

Two-color gradient with parabolic lightness curve peaking at 0.95 at midpoint.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/trackplot/color_scale.rb', line 23

def diverging(hex1, hex2, count: 8)
  validate_hex!(hex1)
  validate_hex!(hex2)
  return [] if count == 0

  h1, s1, _ = hex_to_hsl(hex1)
  h2, s2, _ = hex_to_hsl(hex2)

  if count == 1
    return [hsl_to_hex(lerp_hue(h1, h2, 0.5), lerp(s1, s2, 0.5), 0.95)]
  end

  (0...count).map do |i|
    t = i.to_f / (count - 1)
    h = lerp_hue(h1, h2, t)
    s = lerp(s1, s2, t)
    # Parabolic curve: l = 0.95 at t=0.5, dropping to ~0.35 at edges
    l = 0.95 - 2.4 * (t - 0.5)**2
    hsl_to_hex(h, s, l)
  end
end

.sequential(hex, count: 8) ⇒ Object

Light-to-dark palette from a single base color. Extracts hue + saturation, varies lightness from 0.90 to 0.25.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/trackplot/color_scale.rb', line 7

def sequential(hex, count: 8)
  validate_hex!(hex)
  return [] if count == 0

  h, s, _ = hex_to_hsl(hex)
  if count == 1
    return [hsl_to_hex(h, s, 0.5)]
  end

  (0...count).map do |i|
    l = lerp(0.90, 0.25, i.to_f / (count - 1))
    hsl_to_hex(h, s, l)
  end
end