module GHI::Formatting::Colors

Constants

ANSI
WEB

Attributes

colorize[RW]

Public Class Methods

colorize?() click to toggle source
# File lib/ghi/formatting/colors.rb, line 6
def colorize?
  return @colorize if defined? @colorize
  @colorize = STDOUT.tty?
end

Public Instance Methods

bg(color, &block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 20
def bg color, &block
  fg(offset(color)) { escape color, 4, &block }
end
bright(&block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 24
def bright &block
  escape :bright, &block
end
colorize?() click to toggle source
# File lib/ghi/formatting/colors.rb, line 12
def colorize?
  Colors.colorize?
end
fg(color, &block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 16
def fg color, &block
  escape color, 3, &block
end
highlight(code_block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 40
def highlight(code_block)
  return code_block unless colorize?
  highlighter.highlight(code_block)
end
inverse(&block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 36
def inverse &block
  escape :inverse, &block
end
no_color() { || ... } click to toggle source
# File lib/ghi/formatting/colors.rb, line 45
def no_color
  old_colorize, Colors.colorize = colorize?, false
  yield
ensure
  Colors.colorize = old_colorize
end
to_hex(string) click to toggle source
# File lib/ghi/formatting/colors.rb, line 52
def to_hex string
  WEB[string] || string.downcase.sub(/^(#|0x)/, '').
    sub(/^([0-f])([0-f])([0-f])$/, '\1\1\2\2\3\3')
end
underline(&block) click to toggle source
# File lib/ghi/formatting/colors.rb, line 28
def underline &block
  escape :underline, &block
end

Private Instance Methods

escape(color = :black, layer = nil) { || ... } click to toggle source
# File lib/ghi/formatting/colors.rb, line 224
def escape color = :black, layer = nil
  return yield unless color && colorize?
  previous_escape = Thread.current[:escape] || "\e[0m"
  escape = Thread.current[:escape] = "\e[%s%sm" % [
    layer, ANSI[color] || escape_256(color)
  ]
  [escape, yield, previous_escape].join
ensure
  Thread.current[:escape] = previous_escape
end
escape_256(color) click to toggle source
# File lib/ghi/formatting/colors.rb, line 235
def escape_256 color
  "8;5;#{to_256(*to_rgb(color))}" if supports_256_colors?
end
highlighter() click to toggle source
# File lib/ghi/formatting/colors.rb, line 307
def highlighter
  @highlighter ||= begin
    raise unless supports_256_colors?
    require 'pygments'
    Pygmentizer.new
  rescue StandardError, LoadError
    FakePygmentizer.new
  end
end
hsl_to_rgb(hsl) click to toggle source
# File lib/ghi/formatting/colors.rb, line 279
def hsl_to_rgb hsl
  h, s, l = hsl
  h /= 360.0
  s /= 100.0
  l /= 100.0
  m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s
  m1 = l * 2 - m2
  rgb = [[m1, m2, h + 1.0 / 3], [m1, m2, h], [m1, m2, h - 1.0 / 3]]
  rgb.map { |c|
    m1, m2, h = c
    h += 1 if h < 0
    h -= 1 if h > 1
    next m1 + (m2 - m1) * h * 6 if h * 6 < 1
    next m2 if h * 2 < 1
    next m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
    m1
  }.map { |c| c * 255 }
end
hue_to_rgb(m1, m2, h) click to toggle source
# File lib/ghi/formatting/colors.rb, line 298
def hue_to_rgb m1, m2, h
  h += 1 if h < 0
  h -= 1 if h > 1
  return m1 + (m2 - m1) * h * 6 if h * 6 < 1
  return m2 if h * 2 < 1
  return m1 + (m2 - m1) * (2.0/3 - h) * 6 if h * 3 < 2
  return m1
end
offset(hex) click to toggle source
# File lib/ghi/formatting/colors.rb, line 254
def offset hex
  h, s, l = rgb_to_hsl(to_rgb(WEB[hex.to_s] || hex))
  l < 55 && !(40..80).include?(h) ? l *= 1.875 : l /= 3
  hsl_to_rgb([h, s, l]).map { |c| '%02x' % c }.join
end
rgb_to_hsl(rgb) click to toggle source
# File lib/ghi/formatting/colors.rb, line 260
def rgb_to_hsl rgb
  r, g, b = rgb.map { |c| c / 255.0 }
  max = [r, g, b].max
  min = [r, g, b].min
  d = max - min
  h = case max
    when min then 0
    when r   then 60 * (g - b) / d
    when g   then 60 * (b - r) / d + 120
    when b   then 60 * (r - g) / d + 240
  end
  l = (max + min) / 2.0
  s = if max == min then 0
    elsif l < 0.5   then d / (2 * l)
    else            d / (2 - 2 * l)
  end
  [h % 360, s * 100, l * 100]
end
supports_256_colors?() click to toggle source
# File lib/ghi/formatting/colors.rb, line 239
def supports_256_colors?
  `tput colors` =~ /256/
end
to_256(r, g, b) click to toggle source
# File lib/ghi/formatting/colors.rb, line 243
def to_256 r, g, b
  r, g, b = [r, g, b].map { |c| c / 10 }
  return 232 + g if r == g && g == b && g != 0 && g != 25
  16 + ((r / 5) * 36) + ((g / 5) * 6) + (b / 5)
end
to_rgb(hex) click to toggle source
# File lib/ghi/formatting/colors.rb, line 249
def to_rgb hex
  n = (WEB[hex.to_s] || hex).to_i(16)
  [2, 1, 0].map { |m| n >> (m << 3) & 0xff }
end