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.



89
90
91
92
93
94
95
96
97
# File 'lib/cairo/color.rb', line 89

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.



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

def blue
  @blue
end

#greenObject Also known as: g

Returns the value of attribute green.



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

def green
  @green
end

#redObject Also known as: r

Returns the value of attribute red.



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

def red
  @red
end

Instance Method Details

#==(other) ⇒ Object



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject



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

def hash
  to_s.hash
end

#to_aObject Also known as: to_ary



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

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

#to_cmykObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cairo/color.rb', line 124

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/cairo/color.rb', line 137

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



120
121
122
# File 'lib/cairo/color.rb', line 120

def to_rgb
  clone
end

#to_sObject



116
117
118
# File 'lib/cairo/color.rb', line 116

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