Module: Spectra::Components

Defined in:
lib/spectra/components.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.validObject

Returns the value of attribute valid.



7
8
9
# File 'lib/spectra/components.rb', line 7

def valid
  @valid
end

Class Method Details

.componentize(attributes) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/spectra/components.rb', line 12

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(value) ⇒ Object

Helpers



31
32
33
34
35
36
37
38
# File 'lib/spectra/components.rb', line 31

def self.componentize_hex(value)
  hex = value.is_a?(String) ? value.to_i : value
  Hash.new.tap do |hash| 
    hash[:red]   = (hex & 0xFF0000) >> 16
    hash[:green] = (hex & 0x00FF00) >> 8
    hash[:blue]  = (hex & 0x0000FF)
  end
end

.componentize_white(value) ⇒ Object



40
41
42
# File 'lib/spectra/components.rb', line 40

def self.componentize_white(value)
  { red: value, green: value, blue: value }
end

.map_attributes(attributes) ⇒ Object



54
55
56
57
# File 'lib/spectra/components.rb', line 54

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



48
49
50
51
52
# File 'lib/spectra/components.rb', line 48

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

Returns:

  • (Boolean)


23
24
25
# File 'lib/spectra/components.rb', line 23

def self.valid?(name)
  self.valid.include?(name)
end