Class: Cairo::Color::RGB

Inherits:
Base
  • Object
show all
Defined in:
lib/cairo/color.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#alpha

Instance Method Summary collapse

Constructor Details

#initialize(r, g, b, a = 1.0) ⇒ RGB

Returns a new instance of RGB.



81
82
83
84
85
86
87
88
89
# File 'lib/cairo/color.rb', line 81

def initialize(r, g, b, a=1.0)
  super(a)
  assert_in_range(r, "red")
  assert_in_range(g, "green")
  assert_in_range(b, "blue")
  @red = r
  @green = g
  @blue = b
end

Instance Attribute Details

#blueObject Also known as: b

Returns the value of attribute blue.



72
73
74
# File 'lib/cairo/color.rb', line 72

def blue
  @blue
end

#greenObject Also known as: g

Returns the value of attribute green.



72
73
74
# File 'lib/cairo/color.rb', line 72

def green
  @green
end

#redObject Also known as: r

Returns the value of attribute red.



72
73
74
# File 'lib/cairo/color.rb', line 72

def red
  @red
end

Instance Method Details

#==(other) ⇒ Object



99
100
101
# File 'lib/cairo/color.rb', line 99

def ==(other)
  other.is_a?(self.class) and other.to_s == to_s
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/cairo/color.rb', line 95

def eql?(other)
  self == other
end

#hashObject



91
92
93
# File 'lib/cairo/color.rb', line 91

def hash
  to_s.hash
end

#to_aObject Also known as: to_ary



103
104
105
# File 'lib/cairo/color.rb', line 103

def to_a
  [@red, @green, @blue, @alpha]
end

#to_cmykObject



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cairo/color.rb', line 116

def to_cmyk
  cmy = [1.0 - @red, 1.0 - @green, 1.0 - @blue]
  key_plate = cmy.min
  if key_plate < 1.0
    one_k = 1.0 - key_plate
    cmyk = cmy.collect {|value| (value - key_plate) / one_k} + [key_plate]
  else
    cmyk = [0, 0, 0, key_plate]
  end
  cmyka = cmyk + [@alpha]
  CMYK.new(*cmyka)
end

#to_hsvObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cairo/color.rb', line 129

def to_hsv
  max = [@red, @blue, @green].max
  if max > 0
    min = [@red, @blue, @green].min
    max_min = max - min
    case max
    when @red
      numerator = @green - @blue
      angle = 0
    when @green
      numerator = @blue - @red
      angle = 120
    when @blue
      numerator = @red - @green
      angle = 240
    end
    h = max_min > 0 ? 60 * numerator / max_min + angle : 0.0
    s = max_min / max
  else
    h = 0.0
    s = 0.0
  end
  v = max
  HSV.new(h, s, v, @alpha)
end

#to_rgbObject



112
113
114
# File 'lib/cairo/color.rb', line 112

def to_rgb
  clone
end

#to_sObject



108
109
110
# File 'lib/cairo/color.rb', line 108

def to_s
  "#%02X%02X%02X%02X" % to_a.collect {|v| (v * 255).ceil}
end