# -*- coding: utf-8 -*- require 'rays/ext' module Rays class Color include Comparable def self.parse (str) Color.new str end def initialize (*args) arg0 = args[0] args = parse arg0 if arg0 && arg0.kind_of?(String) setup *args end def opaque? () alpha >= 1 end def transparent? () alpha <= 0 end def translucent? () a = alpha 0 < a && a < 1 end def to_a () [red, green, blue, alpha] end def [] (index) case index when 0 then red when 1 then green when 2 then blue when 3 then alpha else raise IndexError end end def []= (index, val) case index when 0 then self.red = val when 1 then self.green = val when 2 then self.blue = val when 3 then self.alpha = val else raise IndexError end end def <=> (o) ret = red <=> o.red; return ret if ret != 0 ret = green <=> o.green; return ret if ret != 0 ret = blue <=> o.blue; return ret if ret != 0 alpha <=> o.alpha end def eql? (o) self == o end def inspect () "#" end private def parse (str) case str when /^\s*##{'([\da-fA-F]{1})' * 4}?\s*$/ [$1, $2, $3, $4 || 'f' ].map {|s| s.to_i(16) / 15.0} when /^\s*##{'([\da-fA-F]{2})' * 4}?\s*$/ [$1, $2, $3, $4 || 'ff'].map {|s| s.to_i(16) / 255.0} else raise ArgumentError, "can not parse '#{str}'." end end end# Color end# Rays