Object
A 32 bit RGBA color. Can be created from RGB or RGBA via new, numeric value or hex string via from, or HSV via from_hsv.
Create a new color from a string or integer value. Can take an optional name as well.
# File lib/png.rb, line 215
215: def self.from str, name = nil
216: str = "%08x" % str if Integer === str
217: colors = str.scan(/[\da-f][\da-f]/).map { |n| n.hex }
218: colors << name
219: self.new(*colors)
220: end
Creates a new RGB color from HSV equivalent values.
# File lib/png.rb, line 339
339: def self.from_hsv h, s, v
340: r = g = b = v # gray
341: unless s == 0.0 then
342: h += 255 if h < 0
343: h = h / 255.0 * 6.0
344: s = s / 255.0
345: v = v / 255.0
346: i = h.floor
347: f = h - i
348: p = v * (1 - (s))
349: q = v * (1 - (s * (f)))
350: w = v * (1 - (s * (1-f)))
351: r, g, b = case i
352: when 0,6 then
353: [ v, w, p ]
354: when 1 then
355: [ q, v, p ]
356: when 2 then
357: [ p, v, w ]
358: when 3 then
359: [ p, q, v ]
360: when 4 then
361: [ w, p, v ]
362: when 5 then
363: [ v, p, q ]
364: else
365: raise [h, s, v, i, f, p, q, w].inspect
366: end
367: end
368: self.new((r * 255).round, (g * 255).round, (b * 255).round)
369: end
Creates a new color with values red, green, blue, and alpha.
# File lib/png.rb, line 225
225: def initialize red, green, blue, alpha = MAX, name = nil
226: @values = "%c%c%c%c" % [red, green, blue, alpha]
227: @name = name
228: end
Alpha transparency component
# File lib/png.rb, line 291
291: def a; @values.getbyte 3; end
Blue component
# File lib/png.rb, line 286
286: def b; @values.getbyte 2; end
Blends color into this color returning a new blended color.
# File lib/png.rb, line 296
296: def blend color
297: return Color.new(((r + color.r) / 2), ((g + color.g) / 2),
298: ((b + color.b) / 2), ((a + color.a) / 2))
299: end
Green component
# File lib/png.rb, line 281
281: def g; @values.getbyte 1; end
Returns a new color with an alpha value adjusted by i.
# File lib/png.rb, line 304
304: def intensity i
305: return Color.new(r,g,b,(a*i) >> 8)
306: end
Red component
# File lib/png.rb, line 276
276: def r; @values.getbyte 0; end
Return an array of RGB
# File lib/png.rb, line 269
269: def rgb # TODO: rgba?
270: return r, g, b
271: end
An ASCII representation of this color, almost suitable for making ASCII art!
# File lib/png.rb, line 320
320: def to_ascii
321: return ' ' if a == 0x00
322:
323: brightness = (((r + g + b) / 3) * a) / 0xFF
324:
325: %(.. ,, ++ 00)[brightness / 64]
326: end
Returns HSV equivalent of the current color.
# File lib/png.rb, line 374
374: def to_hsv # errors = 54230 out of 255^3 are off by about 1 on r, g, or b
375: rgb = self.rgb
376: r, g, b = rgb
377: h, s, v = 0, 0, rgb.max
378:
379: return h, s, v if v == 0
380:
381: range = v - rgb.min
382: s = 255 * range / v
383:
384: return h, s, v if s == 0
385:
386: h = case v
387: when r then
388: 0x00 + 43 * (g - b) / range # 43 = 1/4 of 360 scaled to 255
389: when g then
390: 0x55 + 43 * (b - r) / range
391: else
392: 0xAA + 43 * (r - g) / range
393: end
394:
395: return h.round, s.round, v.round
396: end
“Bitwise or” as applied to colors. Background color is considered false.
# File lib/png.rb, line 258
258: def | o
259: self == Background ? o : self
260: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.