Module: Spectra::Components
- Defined in:
- lib/spectra/models/components.rb
Class Attribute Summary collapse
-
.valid ⇒ Object
Returns the value of attribute valid.
Class Method Summary collapse
- .componentize(attributes) ⇒ Object
-
.componentize_hex(hex) ⇒ Object
Helpers.
- .componentize_white(white) ⇒ Object
- .hexify(components) ⇒ Object
- .map_attributes(attributes) ⇒ Object
-
.normalize(number) ⇒ Object
Helpers.
- .valid?(name) ⇒ Boolean
Class Attribute Details
.valid ⇒ Object
Returns the value of attribute valid.
6 7 8 |
# File 'lib/spectra/models/components.rb', line 6 def valid @valid end |
Class Method Details
.componentize(attributes) ⇒ Object
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/spectra/models/components.rb', line 11 def self.componentize(attributes) components = self.map_attributes(attributes).pick(*self.valid) hex, white = attributes[:hex], components[:white] components[:alpha] ||= 1.0 components.merge!(self.componentize_hex(hex)) if hex components.merge!(self.componentize_white(white)) if white components.each { |key, value| components[key] = self.normalize(value) } end |
.componentize_hex(hex) ⇒ Object
Helpers
34 35 36 |
# File 'lib/spectra/models/components.rb', line 34 def self.componentize_hex(hex) { red: (hex & 0xFF0000) >> 16, green: (hex & 0x00FF00) >> 8, blue: (hex & 0x0000FF) } end |
.componentize_white(white) ⇒ Object
38 39 40 |
# File 'lib/spectra/models/components.rb', line 38 def self.componentize_white(white) { red: white, green: white, blue: white } end |
.hexify(components) ⇒ Object
22 23 24 |
# File 'lib/spectra/models/components.rb', line 22 def self.hexify(components) (components[:red] * 255 << 16) | (components[:green] * 255 << 8) | (components[:blue] * 255) end |
.map_attributes(attributes) ⇒ Object
52 53 54 55 |
# File 'lib/spectra/models/components.rb', line 52 def self.map_attributes(attributes) key_map = { r: :red, g: :green, b: :blue, w: :white, a: :alpha } return Hash[attributes.map { |key, value| [ key_map[key] || key, value ] }] end |
.normalize(number) ⇒ Object
Helpers
46 47 48 49 50 |
# File 'lib/spectra/models/components.rb', line 46 def self.normalize(number) number = number / 255.0 if number.is_a?(Fixnum) raise "component #{number} is not in a legible format" unless number.is_a?(Float) number.limit(0.0..1.0) end |
.valid?(name) ⇒ Boolean
26 27 28 |
# File 'lib/spectra/models/components.rb', line 26 def self.valid?(name) self.valid.include?(name) end |