Class: Painter::Color
- Inherits:
-
Object
- Object
- Painter::Color
- Defined in:
- lib/painter/color.rb
Instance Attribute Summary collapse
-
#rgba ⇒ Object
(also: #to_i)
readonly
Returns the value of attribute rgba.
Class Method Summary collapse
-
.normalize(value, max, component = nil) ⇒ Object
:nodoc:.
-
.pack(r, g, b, a) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#alpha ⇒ Object
(also: #a)
Return the alpha component of this color (0..ALPHA_MAX).
-
#alpha=(value) ⇒ Object
(also: #a=)
Modify the alpha component of this color.
-
#blue ⇒ Object
(also: #b)
Return the blue component of this color (0..RGB_MAX).
-
#green ⇒ Object
(also: #g)
Return the green component of this color (0..RGB_MAX).
-
#init_with_rgba(rgba, index = nil, palette = nil) ⇒ Object
:nodoc:.
-
#initialize(r, g, b, a = ALPHA_OPAQUE) ⇒ Color
constructor
A new instance of Color.
-
#red ⇒ Object
(also: #r)
Return the red component of this color (0..RGB_MAX).
- #to_s ⇒ Object
Constructor Details
#initialize(r, g, b, a = ALPHA_OPAQUE) ⇒ Color
Returns a new instance of Color.
9 10 11 12 13 14 15 16 |
# File 'lib/painter/color.rb', line 9 def initialize(r, g, b, a = ALPHA_OPAQUE) r = self.class.normalize(r, RGB_MAX, :red) g = self.class.normalize(g, RGB_MAX, :green) b = self.class.normalize(b, RGB_MAX, :blue) a = self.class.normalize(a, ALPHA_MAX, :alpha) init_with_rgba(self.class.pack(r, g, b, a)) end |
Instance Attribute Details
#rgba ⇒ Object (readonly) Also known as: to_i
Returns the value of attribute rgba.
3 4 5 |
# File 'lib/painter/color.rb', line 3 def rgba @rgba end |
Class Method Details
.normalize(value, max, component = nil) ⇒ Object
:nodoc:
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/painter/color.rb', line 39 def self.normalize(value, max, component = nil) #:nodoc: case value when Integer (value < 0) ? 0 : (value > max) ? max : value when Float normalize((value * max).round, max, component) when Color value.send(component) else return normalize(value.to_i, max) if value.respond_to?(:to_i) raise TypeError end end |
.pack(r, g, b, a) ⇒ Object
:nodoc:
25 26 27 |
# File 'lib/painter/color.rb', line 25 def self.pack(r, g, b, a) #:nodoc: (a << 24) + (r << 16) + (g << 8) + b end |
Instance Method Details
#alpha ⇒ Object Also known as: a
Return the alpha component of this color (0..ALPHA_MAX).
95 96 97 |
# File 'lib/painter/color.rb', line 95 def alpha (rgba & 0x7F000000) >> 24 end |
#alpha=(value) ⇒ Object Also known as: a=
Modify the alpha component of this color. If this color is associated with a palette entry, this also modifies the palette.
102 103 104 105 |
# File 'lib/painter/color.rb', line 102 def alpha=(value) self.rgba = (rgba & ~0xFF000000) | (self.class.normalize(value, ALPHA_MAX, :alpha) << 24) end |
#blue ⇒ Object Also known as: b
Return the blue component of this color (0..RGB_MAX).
81 82 83 |
# File 'lib/painter/color.rb', line 81 def blue rgba & 0x0000FF end |
#green ⇒ Object Also known as: g
Return the green component of this color (0..RGB_MAX).
67 68 69 |
# File 'lib/painter/color.rb', line 67 def green (rgba & 0x00FF00) >> 8 end |
#init_with_rgba(rgba, index = nil, palette = nil) ⇒ Object
:nodoc:
18 19 20 21 22 23 |
# File 'lib/painter/color.rb', line 18 def init_with_rgba(rgba, index = nil, palette = nil) #:nodoc: @rgba = rgba @index = index @palette = palette self end |
#red ⇒ Object Also known as: r
Return the red component of this color (0..RGB_MAX).
53 54 55 |
# File 'lib/painter/color.rb', line 53 def red (rgba & 0xFF0000) >> 16 end |
#to_s ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/painter/color.rb', line 29 def to_s s = 'RGB' s += "A" if alpha != ALPHA_OPAQUE s += "[#{@index}]" if @index s += '#' + [red, green, blue].map { |e| '%02X' % e }.join('') s += '%02X' % alpha if alpha != ALPHA_OPAQUE s end |