Module: Shoes::Colors

Extended by:
Colors
Included in:
Colors, Drawable
Defined in:
lacci/lib/shoes/colors.rb

Instance Method Summary collapse

Instance Method Details

#gray(darkness = 128, alpha = nil) ⇒ Object



161
162
163
164
# File 'lacci/lib/shoes/colors.rb', line 161

def gray(darkness = 128, alpha = nil)
  alpha ||= (darkness.is_a?(Integer) ? 255 : 1.0)
  [darkness, darkness, darkness, alpha]
end

#rgb(r, g, b, a = nil) ⇒ Object

Shoes allows RGB values to be Floats between 0 and 1 or Integers between 0 and 255



167
168
169
170
171
172
173
174
175
# File 'lacci/lib/shoes/colors.rb', line 167

def rgb(r, g, b, a = nil)
  if r.is_a?(Float)
    [r, g, b, a || 1.0]
  elsif r.is_a?(Integer)
    [r, g, b, a || 255]
  else
    raise("RGB values should be Float or Integer!")
  end
end

#to_rgb(color) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lacci/lib/shoes/colors.rb', line 177

def to_rgb(color)
  case color
  when nil
    nil
  when Array
    color # Already an RGB array
  when Symbol
    if COLORS[color]
      rgb(*COLORS[color])
    else
      raise("Unrecognised color name: #{color}")
    end
  when String
    if color[0] == "#"
      if color.length == 4
        r = color[1].to_i(16)
        g = color[2].to_i(16)
        b = color[3].to_i(16)
        rgb(16 * r, 16 * g, 16 * b)
      elsif color.length == 7
        r = color[1..2].to_i(16)
        g = color[3..4].to_i(16)
        b = color[5..6].to_i(16)
        rgb(r, g, b)
      else
        raise("Don't know how to convert #{color.inspect} to RGB! (wrong number of digits)")
      end
    else
      rgb_value = COLORS[color.to_sym]
      if rgb_value
        rgb(*rgb_value)
      else
        raise("Unrecognised color name: #{color}")
      end
    end
  else
    raise("Don't know how to convert #{color.inspect} to RGB!")
  end
end