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 214 def self.from str, name = nil str = "%08x" % str if Integer === str colors = str.scan(/[\da-f][\da-f]/).map { |n| n.hex } colors << name self.new(*colors) end
Creates a new RGB color from HSV equivalent values.
# File lib/png.rb, line 338 def self.from_hsv h, s, v r = g = b = v # gray unless s == 0.0 then h += 255 if h < 0 h = h / 255.0 * 6.0 s = s / 255.0 v = v / 255.0 i = h.floor f = h - i p = v * (1 - (s)) q = v * (1 - (s * (f))) w = v * (1 - (s * (1-f))) r, g, b = case i when 0,6 then [ v, w, p ] when 1 then [ q, v, p ] when 2 then [ p, v, w ] when 3 then [ p, q, v ] when 4 then [ w, p, v ] when 5 then [ v, p, q ] else raise [h, s, v, i, f, p, q, w].inspect end end self.new((r * 255).round, (g * 255).round, (b * 255).round) end
Alpha transparency component
# File lib/png.rb, line 290 def a; @values.getbyte 3; end
Blends color into this color returning a new blended color.
# File lib/png.rb, line 295 def blend color return Color.new(((r + color.r) / 2), ((g + color.g) / 2), ((b + color.b) / 2), ((a + color.a) / 2)) end
Green component
# File lib/png.rb, line 280 def g; @values.getbyte 1; end
Returns a new color with an alpha value adjusted by i.
# File lib/png.rb, line 303 def intensity i return Color.new(r,g,b,(a*i) >> 8) end
Return an array of RGB
# File lib/png.rb, line 268 def rgb # TODO: rgba? return r, g, b end
An ASCII representation of this color, almost suitable for making ASCII art!
# File lib/png.rb, line 319 def to_ascii return ' ' if a == 0x00 brightness = (((r + g + b) / 3) * a) / 0xFF %(.. ,, ++ 00)[brightness / 64] end
Returns HSV equivalent of the current color.
# File lib/png.rb, line 373 def to_hsv # errors = 54230 out of 255^3 are off by about 1 on r, g, or b rgb = self.rgb r, g, b = rgb h, s, v = 0, 0, rgb.max return h, s, v if v == 0 range = v - rgb.min s = 255 * range / v return h, s, v if s == 0 h = case v when r then 0x00 + 43 * (g - b) / range # 43 = 1/4 of 360 scaled to 255 when g then 0x55 + 43 * (b - r) / range else 0xAA + 43 * (r - g) / range end return h.round, s.round, v.round end
"Bitwise or" as applied to colors. Background color is considered false.
# File lib/png.rb, line 257 def | o self == Background ? o : self end
Generated with the Darkfish Rdoc Generator 2.