class Term::ANSIColor::RGBTriple

Attributes

values[R]

Public Class Methods

[](thing) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 35
def self.[](thing)
  case
  when thing.respond_to?(:to_rgb_triple) then thing
  when thing.respond_to?(:to_ary)        then RGBTriple.from_array(thing.to_ary)
  when thing.respond_to?(:to_str)        then RGBTriple.from_html(thing.to_str.sub(/\Aon_/, '')) # XXX somewhat hacky
  when thing.respond_to?(:to_hash)       then RGBTriple.from_hash(thing.to_hash)
  else raise ArgumentError, "cannot convert #{thing.inspect} into #{self}"
  end
end
from_array(array) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 31
def self.from_array(array)
  new(*array)
end
from_hash(options) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 23
def self.from_hash(options)
  new(
    convert_value(options[:red]),
    convert_value(options[:green]),
    convert_value(options[:blue])
  )
end
from_html(html) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 14
def self.from_html(html)
  case html
  when /\A#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})\z/
    new(*$~.captures.map { |c| convert_value(c.to_i(16)) })
  when /\A#([0-9a-f])([0-9a-f])([0-9a-f])\z/
    new(*$~.captures.map { |c| convert_value(c.to_i(16) << 4) })
  end
end
new(red, green, blue) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 45
def initialize(red, green, blue)
  @values = [ red, green, blue ]
end

Private Class Methods

convert_value(color) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 4
def self.convert_value(color)
  color.nil? and raise ArgumentError, "missing color value"
  color = Integer(color)
  (0..0xff) === color or raise ArgumentError,
    "color value #{color.inspect} not between 0 and 255"
  color
end

Public Instance Methods

==(other) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 78
def ==(other)
  @values == other.values
end
blue() click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 57
def blue
  @values[2]
end
distance_to(other) click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 82
def distance_to(other)
  Math.sqrt(
    ((red - other.red) * 0.299) ** 2 +
    ((green - other.green) * 0.587) ** 2 +
    ((blue - other.blue) * 0.114) ** 2
  )
end
green() click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 53
def green
  @values[1]
end
html() click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 61
def html
  s = '#'
  @values.each { |c| s << '%02x' % c }
  s
end
red() click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 49
def red
  @values[0]
end
to_a() click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 74
def to_a
  @values.dup
end
to_rgb_triple() click to toggle source
# File lib/term/ansicolor/rgb_triple.rb, line 67
def to_rgb_triple
  self
end