module Spectra::Components
Attributes
valid[RW]
Public Class Methods
componentize(attributes)
click to toggle source
# 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)
click to toggle source
Helpers
# 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)
click to toggle source
# File lib/spectra/models/components.rb, line 38 def self.componentize_white(white) { red: white, green: white, blue: white } end
hexify(components)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
Helpers
# 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)
click to toggle source
# File lib/spectra/models/components.rb, line 26 def self.valid?(name) self.valid.include?(name) end