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:.
-
#|(o) ⇒ Object
“Bitwise or” as applied to colors.
Constructor Details
#initialize(red, green, blue, alpha = MAX, name = nil) ⇒ Color
Creates a new color with values red
, green
, blue
, and alpha
.
224 225 226 227 |
# File 'lib/png.rb', line 224 def initialize red, green, blue, alpha = MAX, name = nil @values = "%c%c%c%c" % [red, green, blue, alpha] @name = name end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
208 209 210 |
# File 'lib/png.rb', line 208 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.
214 215 216 217 218 219 |
# 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]/i).map { |n| n.hex } colors << name self.new(*colors) end |
.from_hsv(h, s, v) ⇒ Object
Creates a new RGB color from HSV equivalent values.
337 338 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 |
# File 'lib/png.rb', line 337 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 |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
:nodoc:
247 248 249 |
# File 'lib/png.rb', line 247 def == other # :nodoc: self.class === other and other.values == values end |
#a ⇒ Object
Alpha transparency component
290 |
# File 'lib/png.rb', line 290 def a; @values[3]; end |
#b ⇒ Object
Blue component
285 |
# File 'lib/png.rb', line 285 def b; @values[2]; end |
#blend(color) ⇒ Object
Blends color
into this color returning a new blended color.
295 296 297 298 |
# 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 |
#g ⇒ Object
Green component
280 |
# File 'lib/png.rb', line 280 def g; @values[1]; end |
#hash ⇒ Object
:nodoc:
261 262 263 |
# File 'lib/png.rb', line 261 def hash # :nodoc: self.values.hash end |
#inspect ⇒ Object
:nodoc:
307 308 309 310 311 312 313 |
# File 'lib/png.rb', line 307 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
.
303 304 305 |
# File 'lib/png.rb', line 303 def intensity i return Color.new(r,g,b,(a*i) >> 8) end |
#r ⇒ Object
Red component
275 |
# File 'lib/png.rb', line 275 def r; @values[0]; end |
#rgb ⇒ Object
Return an array of RGB
268 269 270 |
# File 'lib/png.rb', line 268 def rgb # TODO: rgba? return @values[0], @values[1], @values[2] end |
#to_ascii ⇒ Object
An ASCII representation of this color, almost suitable for making ASCII art!
319 320 321 322 323 324 |
# File 'lib/png.rb', line 319 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.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/png.rb', line 372 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:
326 327 328 329 330 331 332 |
# File 'lib/png.rb', line 326 def to_s # :nodoc: if @name then @name else super end end |
#|(o) ⇒ Object
“Bitwise or” as applied to colors. Background color is considered false.
257 258 259 |
# File 'lib/png.rb', line 257 def | o self == Background ? o : self end |