Class: StyleTrain::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/style_train/color.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color, opts = {}) ⇒ Color

Constructor



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/style_train/color.rb', line 7

def initialize( color, opts={} ) 
  opts = Gnash.new(opts).merge(:color => color) 
  background_opts = opts.delete(:background) 
  self.background = background_opts if background_opts
  if color.is_a?(Color)
    self.delegate = color.delegate.dup
    self.alpha = opts[:alpha] if opts[:alpha]
    self.background = color.background if color.background_set
  else  
    color_types.each do |klass|
      if instance = klass.make(opts)
        self.delegate = instance
        break
      end
    end 
  end     
end

Instance Attribute Details

#background_setObject

Returns the value of attribute background_set.



4
5
6
# File 'lib/style_train/color.rb', line 4

def background_set
  @background_set
end

#delegateObject

Returns the value of attribute delegate.



4
5
6
# File 'lib/style_train/color.rb', line 4

def delegate
  @delegate
end

Class Method Details

.color_typesObject



25
26
27
# File 'lib/style_train/color.rb', line 25

def self.color_types
  @color_type ||= ColorType.color_types
end

Instance Method Details

#==(color) ⇒ Object



58
59
60
# File 'lib/style_train/color.rb', line 58

def ==( color )
  color.is_a?( Color ) && self =~ color && self.background == color.background
end

#=~(color) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/style_train/color.rb', line 50

def =~( color )
  if color.is_a?(Color)
    self.delegate =~ color.delegate
  elsif color.is_a?( ColorType )
    self.delegate =~ color
  end    
end

#alpha=(value) ⇒ Object



42
43
44
# File 'lib/style_train/color.rb', line 42

def alpha=( value )
  self.delegate.alpha = value
end

#backgroundObject

rendering



63
64
65
# File 'lib/style_train/color.rb', line 63

def background
  @background ||= Color.new(:white).to(:hex)
end

#background=(opts) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/style_train/color.rb', line 67

def background=( opts )
  color = opts.is_a?(Hash) && (opts[:color] || opts['color'])

  @background = if color
    opts.delete(:alpha)
    Color.new(color, opts)
  elsif opts.is_a? Color
    color = opts.dup # so that the original color's alpha isn't affected
    color.alpha = 1.0
    color
  else  
    Color.new(opts)
  end
  raise ArgumentError, "Background with color: #{color} and options: #{opts} not found" unless @background 
  raise ArgumentError, "Background color #{@background.inspect} has no delegate color" unless @background.delegate

  self.background_set = true
  @background  
end

#color_typesObject



29
30
31
# File 'lib/style_train/color.rb', line 29

def color_types
  self.class.color_types
end

#inspectObject



107
108
109
# File 'lib/style_train/color.rb', line 107

def inspect 
  "<Color @delegate=#{self.delegate.to_s} @background=#{self.background}>"
end

#render(render_style = nil) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/style_train/color.rb', line 99

def render(render_style=nil) 
  if render_style && render_style.to_sym == :ie
    self.background.to(:hex).layer(self.delegate).render
  else  
    self.delegate.render
  end   
end

#step(end_color, steps = 10) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/style_train/color.rb', line 87

def step(end_color, steps=10)
  start_color = self.delegate
  end_color = end_color.delegate
  array = [start_color]
  (2..(steps-1)).each do |number|
    ratio = 1 - (steps-number)/steps.to_f
    array << start_color.mix(end_color, ratio)
  end
  array << end_color
  array
end

#to(key) ⇒ Object



46
47
48
# File 'lib/style_train/color.rb', line 46

def to(key)
  self.delegate.to(key)
end

#to_s(render_style = nil) ⇒ Object



111
112
113
# File 'lib/style_train/color.rb', line 111

def to_s(render_style=nil)
  render(render_style)
end