module Textbringer::Color

Constants

ADDITIONAL_COLORS
BASIC_COLORS
RGBColor

Public Class Methods

[](name) click to toggle source
# File lib/textbringer/color.rb, line 40
def self.[](name)
  n = find_color_number(name)
  if n < Window.colors
    n
  else
    -1
  end
end
find_color_number(name) click to toggle source
# File lib/textbringer/color.rb, line 49
def self.find_color_number(name)
  if name.is_a?(Integer)
    return name
  end
  case name
  when /\A\#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})\z/
    r = $1.to_i(16)
    g = $2.to_i(16)
    b = $3.to_i(16)
    ADDITIONAL_COLORS.sort_by { |c|
      (r - c.r) ** 2 + (g - c.g) ** 2 + (b - c.b) ** 2
    }.first.number
  else
    unless BASIC_COLORS.key?(name)
      raise EditorError, "No such color: #{name}"
    end
    BASIC_COLORS[name]
  end
end