Class: EhbGameLib::Palettes::Color
- Inherits:
-
Object
- Object
- EhbGameLib::Palettes::Color
- Defined in:
- lib/ehb_game_lib/palettes/color.rb
Constant Summary collapse
- QUANTUM_MIN =
0
- QUANTUM_MAX =
255
- QUANTUM_RANGE =
(QUANTUM_MIN..QUANTUM_MAX).freeze
Instance Attribute Summary collapse
-
#alpha ⇒ Object
readonly
Returns the value of attribute alpha.
-
#blue ⇒ Object
readonly
Returns the value of attribute blue.
-
#green ⇒ Object
readonly
Returns the value of attribute green.
-
#red ⇒ Object
readonly
Returns the value of attribute red.
Class Method Summary collapse
- .array_from_file(file_path) ⇒ Array<EhbGameLib::Palettes::Color>
- .quantum_to_magick_range(quantum, invert = false) ⇒ Object
- .validate_quantum(quantum) ⇒ Object
Instance Method Summary collapse
- #gosu_color ⇒ Gosu::Color
-
#initialize(red, green, blue, alpha = QUANTUM_MAX) ⇒ Color
constructor
A new instance of Color.
- #magick_pixel ⇒ Magick::Pixel
- #names ⇒ Object
- #rgb_to_s ⇒ Object
- #rgba_to_s ⇒ Object
- #to_a ⇒ Object
- #to_html ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(red, green, blue, alpha = QUANTUM_MAX) ⇒ Color
Returns a new instance of Color.
44 45 46 47 48 49 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 44 def initialize(red, green, blue, alpha = QUANTUM_MAX) @red = self.class.validate_quantum(red) @green = self.class.validate_quantum(green) @blue = self.class.validate_quantum(blue) @alpha = self.class.validate_quantum(alpha) end |
Instance Attribute Details
#alpha ⇒ Object (readonly)
Returns the value of attribute alpha.
42 43 44 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 42 def alpha @alpha end |
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
42 43 44 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 42 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
42 43 44 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 42 def green @green end |
#red ⇒ Object (readonly)
Returns the value of attribute red.
42 43 44 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 42 def red @red end |
Class Method Details
.array_from_file(file_path) ⇒ Array<EhbGameLib::Palettes::Color>
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 29 def array_from_file(file_path) r = [] ::File.open(file_path.to_s) do |file| while (b = file.read(3)) b = b.bytes.to_a r << ::EhbGameLib::Palettes::Color.new(b[0], b[1], b[2]) end end r end |
.quantum_to_magick_range(quantum, invert = false) ⇒ Object
22 23 24 25 26 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 22 def quantum_to_magick_range(quantum, invert = false) r = ((quantum / QUANTUM_MAX.to_f) * ::Magick::QuantumRange).floor r = ::Magick::QuantumRange - r if invert r end |
.validate_quantum(quantum) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 15 def validate_quantum(quantum) raise "Not a integer: #{quantum}" unless quantum.is_a?(::Integer) raise 'Out of bounds' unless QUANTUM_RANGE.include?(quantum) quantum end |
Instance Method Details
#gosu_color ⇒ Gosu::Color
52 53 54 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 52 def gosu_color @gosu_color ||= ::Gosu::Color.rgba(red, green, blue, alpha) end |
#magick_pixel ⇒ Magick::Pixel
57 58 59 60 61 62 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 57 def magick_pixel @magick_pixel ||= ::Magick::Pixel.new( *[red, green, blue].map { |n| self.class.quantum_to_magick_range(n) }, self.class.quantum_to_magick_range(alpha, true) ) end |
#names ⇒ Object
64 65 66 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 64 def names Array(::Colorname.find(red, green, blue)) end |
#rgb_to_s ⇒ Object
68 69 70 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 68 def rgb_to_s values_to_s(red, green, blue) end |
#rgba_to_s ⇒ Object
72 73 74 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 72 def rgba_to_s rgb_to_s + '|' + values_to_s(alpha) end |
#to_a ⇒ Object
76 77 78 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 76 def to_a [red, green, blue, alpha] end |
#to_html ⇒ Object
84 85 86 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 84 def to_html '#' + rgb_to_s end |
#to_s ⇒ Object
80 81 82 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 80 def to_s rgba_to_s end |