Module: Termfront::Color

Defined in:
lib/termfront/color.rb

Class Method Summary collapse

Class Method Details

.rgb_to_256(r, g, b) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/termfront/color.rb', line 7

def rgb_to_256(r, g, b)
  if (r - g).abs < 8 && (g - b).abs < 8 && (r - b).abs < 8
    avg = (r + g + b) / 3
    return 16 if avg < 8
    return 231 if avg > 247

    232 + (avg - 8) * 23 / 240
  else
    16 + (r * 5 / 255) * 36 + (g * 5 / 255) * 6 + (b * 5 / 255)
  end
end