class PNG::Color
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.
Constants
- Background
-
Transparent white
- Black
- Blue
- Brown
- Bubblegum
- Cyan
- Gray
- Green
- MAX
- Magenta
- Orange
- Purple
- Red
- White
- Yellow
Attributes
Public Class Methods
Source
# File lib/png.rb, line 199 def self.from str, name = nil str = "%08x" % str if Integer === str colors = str.scan(/\h\h/i).map(&:hex) colors << name self.new(*colors) end
Create a new color from a string or integer value. Can take an optional name as well.
Source
# File lib/png.rb, line 325 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 /= 255.0 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
Creates a new RGB color from HSV equivalent values.
Source
# File lib/png.rb, line 209 def initialize red, green, blue, alpha = MAX, name = nil @values = "%c%c%c%c".b % [red, green, blue, alpha].map(&:chr) @name = name end
Creates a new color with values red
, green
, blue
, and alpha
.
Public Instance Methods
Source
# File lib/png.rb, line 242 def | other self == Background ? other : self end
“Bitwise or” as applied to colors. Background
color is considered false.
Source
# File lib/png.rb, line 280 def blend color Color.new(((r + color.r) / 2), ((g + color.g) / 2), ((b + color.b) / 2), ((a + color.a) / 2)) end
Blends color
into this color returning a new blended color.
Source
# File lib/png.rb, line 290 def intensity i Color.new(r, g, b, (a*i) >> 8) end
Returns a new color with an alpha value adjusted by i
.
Source
# File lib/png.rb, line 253 def rgb # TODO: rgba? return r, g, b end
Return an array of RGB
Source
# File lib/png.rb, line 306 def to_ascii return " " if a == 0x00 brightness = (((r + g + b) / 3) * a) / 0xFF %w[.. ,, ++ 00][brightness / 64] end
An ASCII representation of this color, almost suitable for making ASCII art!
Source
# File lib/png.rb, line 360 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
Returns HSV equivalent of the current color.