Class: Rabbit::Renderer::Color
- Inherits:
-
Struct
- Object
- Struct
- Rabbit::Renderer::Color
- Defined in:
- lib/rabbit/renderer/color.rb,
lib/rabbit/renderer/color.rb
Constant Summary collapse
- GDK_COLOR_NORMALIZE =
65535.0
- HEX =
"(?i:[a-z0-9])"
Instance Attribute Summary collapse
-
#alpha ⇒ Object
Returns the value of attribute alpha.
-
#blue ⇒ Object
Returns the value of attribute blue.
-
#green ⇒ Object
Returns the value of attribute green.
-
#have_alpha ⇒ Object
(also: #have_alpha?)
Returns the value of attribute have_alpha.
-
#red ⇒ Object
Returns the value of attribute red.
Class Method Summary collapse
- .new_from_gdk_color(color, have_alpha = false) ⇒ Object
- .normalize(r, g, b, a, max) ⇒ Object
- .parse(spec) ⇒ Object
Instance Method Summary collapse
- #to_a ⇒ Object
- #to_gdk_color ⇒ Object
- #to_gdk_format ⇒ Object
- #to_gdk_rgb ⇒ Object
- #to_gdk_rgba ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#alpha ⇒ Object
Returns the value of attribute alpha
7 8 9 |
# File 'lib/rabbit/renderer/color.rb', line 7 def alpha @alpha end |
#blue ⇒ Object
Returns the value of attribute blue
7 8 9 |
# File 'lib/rabbit/renderer/color.rb', line 7 def blue @blue end |
#green ⇒ Object
Returns the value of attribute green
7 8 9 |
# File 'lib/rabbit/renderer/color.rb', line 7 def green @green end |
#have_alpha ⇒ Object Also known as: have_alpha?
Returns the value of attribute have_alpha
7 8 9 |
# File 'lib/rabbit/renderer/color.rb', line 7 def have_alpha @have_alpha end |
#red ⇒ Object
Returns the value of attribute red
7 8 9 |
# File 'lib/rabbit/renderer/color.rb', line 7 def red @red end |
Class Method Details
.new_from_gdk_color(color, have_alpha = false) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/rabbit/renderer/color.rb', line 12 def new_from_gdk_color(color, have_alpha=false) red = color.red / GDK_COLOR_NORMALIZE green = color.green / GDK_COLOR_NORMALIZE blue = color.blue / GDK_COLOR_NORMALIZE alpha = 1.0 new(red, green, blue, alpha, have_alpha) end |
.normalize(r, g, b, a, max) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/rabbit/renderer/color.rb', line 36 def normalize(r, g, b, a, max) red = r.hex / max.to_f green = g.hex / max.to_f blue = b.hex / max.to_f have_alpha = !a.nil? alpha = have_alpha ? a.hex / max.to_f : 1.0 [red, green, blue, alpha, have_alpha] end |
.parse(spec) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rabbit/renderer/color.rb', line 21 def parse(spec) case spec when Array new(*spec) when /\A\#(#{HEX})(#{HEX})(#{HEX})(#{HEX})?\z/ new(*normalize($1, $2, $3, $4, 16 ** 1 - 1)) when /\A\#(#{HEX}{2})(#{HEX}{2})(#{HEX}{2})(#{HEX}{2})?\z/ new(*normalize($1, $2, $3, $4, 16 ** 2 - 1)) when /\A\#(#{HEX}{4})(#{HEX}{4})(#{HEX}{4})(#{HEX}{4})?\z/ new(*normalize($1, $2, $3, $4, 16 ** 4 - 1)) else new_from_gdk_color(Gdk::Color.parse(spec)) end end |
Instance Method Details
#to_a ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/rabbit/renderer/color.rb', line 52 def to_a if have_alpha? [red, green, blue, alpha] else [red, green, blue] end end |
#to_gdk_color ⇒ Object
74 75 76 |
# File 'lib/rabbit/renderer/color.rb', line 74 def to_gdk_color Gdk::Color.new(*to_gdk_rgb) end |
#to_gdk_format ⇒ Object
70 71 72 |
# File 'lib/rabbit/renderer/color.rb', line 70 def to_gdk_format to_s.gsub(/[a-z0-9]{4}\z/i, '') end |
#to_gdk_rgb ⇒ Object
60 61 62 |
# File 'lib/rabbit/renderer/color.rb', line 60 def to_gdk_rgb to_gdk_rgba[0..-2] end |
#to_gdk_rgba ⇒ Object
64 65 66 67 68 |
# File 'lib/rabbit/renderer/color.rb', line 64 def to_gdk_rgba [red, green, blue, alpha || 1.0].collect do |color| (color * GDK_COLOR_NORMALIZE).truncate end end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/rabbit/renderer/color.rb', line 48 def to_s "#%04X%04X%04X%04X" % to_gdk_rgba end |