Class: Rays::Color

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rays/color.rb

Constant Summary collapse

COLORS =
{
  black:   [0, 0, 0],
  red:     [1, 0, 0],
  green:   [0, 1, 0],
  blue:    [0, 0, 1],
  yellow:  [1, 1, 0],
  cyan:    [0, 1, 1],
  magenta: [1, 0, 1],
  white:   [1, 1, 1],
  gray:    [0.5, 0.5, 0.5],
  no:      [0, 0],
  none:    [0, 0],
  nil:     [0, 0],
}.inject({}) {|h, (k, v)| h[k] = Color.new *v; h}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Color



26
27
28
29
30
# File 'lib/rays/color.rb', line 26

def initialize (*args)
  arg0 = args[0]
  args = parse arg0 if arg0 && arg0.kind_of?(String)
  setup *args
end

Class Method Details

.color(*args) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rays/color.rb', line 14

def self.color (*args)
  c = case arg0 = args[0]
  when Color                  then arg0
  when Symbol                 then COLORS[arg0]
  when /^\s*#[\da-fA-F]+\s*$/ then Color.new arg0
  when String                 then COLORS[arg0.intern]
  else                             Color.new *args
  end
  raise ArgumentError, "invalid argument '#{args}.'" unless c
  c
end

Instance Method Details

#<=>(o) ⇒ Object



73
74
75
76
77
78
# File 'lib/rays/color.rb', line 73

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

#[](index) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/rays/color.rb', line 53

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

#[]=(index, val) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/rays/color.rb', line 63

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

#eql?(o) ⇒ Boolean



80
81
82
# File 'lib/rays/color.rb', line 80

def eql? (o)
  self == o
end

#inspectObject



84
85
86
# File 'lib/rays/color.rb', line 84

def inspect ()
  "#<#{self.class.name} #{to_s}>"
end

#opaque?Boolean



32
33
34
# File 'lib/rays/color.rb', line 32

def opaque? ()
  alpha >= 1
end

#to_aObject



45
46
47
# File 'lib/rays/color.rb', line 45

def to_a ()
  [red, green, blue, alpha]
end

#to_sObject



49
50
51
# File 'lib/rays/color.rb', line 49

def to_s ()
  to_a.map {|o| o.to_s}
end

#translucent?Boolean



40
41
42
43
# File 'lib/rays/color.rb', line 40

def translucent? ()
  a = alpha
  0 < a && a < 1
end

#transparent?Boolean



36
37
38
# File 'lib/rays/color.rb', line 36

def transparent? ()
  alpha <= 0
end