class Rabbit::Renderer::Color

Constants

HEX

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/rabbit/renderer/color.rb, line 14
def initialize(*args)
  case args.collect(&:class)
  when [Gdk::RGBA]
    rgba = args[0]
    super(*rgba.to_a)
  when [String]
    rgba = parse(args[0])
    super(*rgba)
  else
    super(*args)
  end
end
parse(*args) click to toggle source
# File lib/rabbit/renderer/color.rb, line 9
def parse(*args)
  new(*args)
end

Public Instance Methods

to_a() click to toggle source
# File lib/rabbit/renderer/color.rb, line 32
def to_a
  [red, green, blue, alpha]
end
to_css_rgba() click to toggle source
# File lib/rabbit/renderer/color.rb, line 44
def to_css_rgba
  red_percent = (red * 100).ceil
  green_percent = (green * 100).ceil
  blue_percent = (blue * 100).ceil
  a = alpha || 1.0
  "rgba(#{red_percent}%, #{green_percent}%, #{blue_percent}%, #{a})"
end
to_gdk_format() click to toggle source
# File lib/rabbit/renderer/color.rb, line 40
def to_gdk_format
  to_s
end
to_gdk_rgba() click to toggle source
# File lib/rabbit/renderer/color.rb, line 36
def to_gdk_rgba
  Gdk::RGBA.new(*to_a)
end
to_s() click to toggle source
# File lib/rabbit/renderer/color.rb, line 27
def to_s
  values = to_a.collect {|x| (x * 65535).ceil}
  "#%04X%04X%04X%04X" % values
end

Private Instance Methods

normalize_rgba(r, g, b, a, max) click to toggle source
# File lib/rabbit/renderer/color.rb, line 67
def normalize_rgba(r, g, b, a, max)
  red = r.hex / max.to_f
  green = g.hex / max.to_f
  blue = b.hex / max.to_f
  if a
    alpha = a.hex / max.to_f
  else
    alpha = 1.0
  end
  [red, green, blue, alpha]
end
parse(text) click to toggle source
# File lib/rabbit/renderer/color.rb, line 54
def parse(text)
  case text
  when /\A\#(#{HEX})(#{HEX})(#{HEX})(#{HEX})?\z/
    normalize_rgba($1, $2, $3, $4, 16 ** 1 - 1)
  when /\A\#(#{HEX}{2})(#{HEX}{2})(#{HEX}{2})(#{HEX}{2})?\z/
    normalize_rgba($1, $2, $3, $4, 16 ** 2 - 1)
  when /\A\#(#{HEX}{4})(#{HEX}{4})(#{HEX}{4})(#{HEX}{4})?\z/
    normalize_rgba($1, $2, $3, $4, 16 ** 4 - 1)
  else
    Gdk::RGBA.parse(text).to_a
  end
end