Class: PNG::Color
- Inherits:
-
Object
- Object
- PNG::Color
- Defined in:
- lib/png.rb
Overview
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.
Constant Summary collapse
- MAX =
255
- Background =
Transparent white
Color.from 0x00000000, "Transparent"
- Black =
Color.from 0x000000FF, "Black"
- Blue =
Color.from 0x0000FFFF, "Blue"
- Brown =
Color.from 0x996633FF, "Brown"
- Bubblegum =
Color.from 0xFF66FFFF, "Bubblegum"
- Cyan =
Color.from 0x00FFFFFF, "Cyan"
- Gray =
Color.from 0x7F7F7FFF, "Gray"
- Green =
Color.from 0x00FF00FF, "Green"
- Magenta =
Color.from 0xFF00FFFF, "Magenta"
- Orange =
Color.from 0xFF7F00FF, "Orange"
- Purple =
Color.from 0x7F007FFF, "Purple"
- Red =
Color.from 0xFF0000FF, "Red"
- White =
Color.from 0xFFFFFFFF, "White"
- Yellow =
Color.from 0xFFFF00FF, "Yellow"
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
-
.from(str, name = nil) ⇒ Object
Create a new color from a string or integer value.
-
.from_hsv(h, s, v) ⇒ Object
Creates a new RGB color from HSV equivalent values.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
:nodoc:.
-
#a ⇒ Object
Alpha transparency component.
-
#b ⇒ Object
Blue component.
-
#blend(color) ⇒ Object
Blends
color
into this color returning a new blended color. -
#g ⇒ Object
Green component.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(red, green, blue, alpha = MAX, name = nil) ⇒ Color
constructor
Creates a new color with values
red
,green
,blue
, andalpha
. -
#inspect ⇒ Object
:nodoc:.
-
#intensity(i) ⇒ Object
Returns a new color with an alpha value adjusted by
i
. -
#r ⇒ Object
Red component.
-
#rgb ⇒ Object
Return an array of RGB.
-
#to_ascii ⇒ Object
An ASCII representation of this color, almost suitable for making ASCII art!.
-
#to_hsv ⇒ Object
Returns HSV equivalent of the current color.
-
#to_s ⇒ Object
:nodoc:.
-
#|(other) ⇒ Object
“Bitwise or” as applied to colors.
Constructor Details
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
209 210 211 |
# File 'lib/png.rb', line 209 def values @values end |
Class Method Details
.from(str, name = nil) ⇒ Object
Create a new color from a string or integer value. Can take an optional name as well.
215 216 217 218 219 220 |
# File 'lib/png.rb', line 215 def self.from str, name = nil str = "%08x" % str if Integer === str colors = str.scan(/[\da-f][\da-f]/i).map(&:hex) colors << name self.new(*colors) end |
.from_hsv(h, s, v) ⇒ Object
Creates a new RGB color from HSV equivalent values.
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/png.rb', line 339 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 |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
:nodoc:
248 249 250 |
# File 'lib/png.rb', line 248 def == other # :nodoc: self.class === other and other.values == values end |
#a ⇒ Object
Alpha transparency component
291 |
# File 'lib/png.rb', line 291 def a; @values.getbyte 3; end |
#b ⇒ Object
Blue component
286 |
# File 'lib/png.rb', line 286 def b; @values.getbyte 2; end |
#blend(color) ⇒ Object
Blends color
into this color returning a new blended color.
296 297 298 299 |
# File 'lib/png.rb', line 296 def blend color Color.new(((r + color.r) / 2), ((g + color.g) / 2), ((b + color.b) / 2), ((a + color.a) / 2)) end |
#g ⇒ Object
Green component
281 |
# File 'lib/png.rb', line 281 def g; @values.getbyte 1; end |
#hash ⇒ Object
:nodoc:
262 263 264 |
# File 'lib/png.rb', line 262 def hash # :nodoc: self.values.hash end |
#inspect ⇒ Object
:nodoc:
308 309 310 311 312 313 314 |
# File 'lib/png.rb', line 308 def inspect # :nodoc: if @name then "#<%s %s>" % [self.class, @name] else "#<%s %02x %02x %02x %02x>" % [self.class, r, g, b, a] end end |
#intensity(i) ⇒ Object
Returns a new color with an alpha value adjusted by i
.
304 305 306 |
# File 'lib/png.rb', line 304 def intensity i Color.new(r, g, b, (a*i) >> 8) end |
#r ⇒ Object
Red component
276 |
# File 'lib/png.rb', line 276 def r; @values.getbyte 0; end |
#rgb ⇒ Object
Return an array of RGB
269 270 271 |
# File 'lib/png.rb', line 269 def rgb # TODO: rgba? return r, g, b end |
#to_ascii ⇒ Object
An ASCII representation of this color, almost suitable for making ASCII art!
320 321 322 323 324 325 326 |
# File 'lib/png.rb', line 320 def to_ascii return " " if a == 0x00 brightness = (((r + g + b) / 3) * a) / 0xFF %w[.. ,, ++ 00][brightness / 64] end |
#to_hsv ⇒ Object
Returns HSV equivalent of the current color.
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/png.rb', line 374 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 |
#to_s ⇒ Object
:nodoc:
328 329 330 331 332 333 334 |
# File 'lib/png.rb', line 328 def to_s # :nodoc: if @name then @name else super end end |
#|(other) ⇒ Object
“Bitwise or” as applied to colors. Background color is considered false.
258 259 260 |
# File 'lib/png.rb', line 258 def | other self == Background ? other : self end |