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) ⇒ 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.
42 43 44 45 46 47 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 42 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.
40 41 42 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 40 def alpha @alpha end |
#blue ⇒ Object (readonly)
Returns the value of attribute blue.
40 41 42 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 40 def blue @blue end |
#green ⇒ Object (readonly)
Returns the value of attribute green.
40 41 42 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 40 def green @green end |
#red ⇒ Object (readonly)
Returns the value of attribute red.
40 41 42 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 40 def red @red end |
Class Method Details
.array_from_file(file_path) ⇒ Array<EhbGameLib::Palettes::Color>
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 27 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) ⇒ Object
22 23 24 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 22 def quantum_to_magick_range(quantum) ((quantum / QUANTUM_MAX.to_f) * ::Magick::QuantumRange).floor 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
50 51 52 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 50 def gosu_color @gosu_color ||= ::Gosu::Color.rgba(red, green, blue, alpha) end |
#magick_pixel ⇒ Magick::Pixel
55 56 57 58 59 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 55 def magick_pixel @magick_pixel ||= ::Magick::Pixel.new( *[red, green, blue, alpha].map { |n| self.class.quantum_to_magick_range(n) } ) end |
#names ⇒ Object
61 62 63 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 61 def names Array(::Colorname.find(red, green, blue)) end |
#rgb_to_s ⇒ Object
65 66 67 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 65 def rgb_to_s values_to_s(red, green, blue) end |
#rgba_to_s ⇒ Object
69 70 71 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 69 def rgba_to_s rgb_to_s + '|' + values_to_s(alpha) end |
#to_a ⇒ Object
73 74 75 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 73 def to_a [red, green, blue, alpha] end |
#to_html ⇒ Object
81 82 83 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 81 def to_html '#' + rgb_to_s end |
#to_s ⇒ Object
77 78 79 |
# File 'lib/ehb_game_lib/palettes/color.rb', line 77 def to_s rgba_to_s end |