Class: ColorFun::Gradient
- Inherits:
-
Object
- Object
- ColorFun::Gradient
- Defined in:
- lib/color_fun/gradient.rb
Instance Attribute Summary collapse
-
#resolution ⇒ Object
Returns the value of attribute resolution.
Instance Method Summary collapse
- #at(point) ⇒ Object
- #color_window_size ⇒ Object
- #colors ⇒ Object
- #differences ⇒ Object
-
#initialize(*colors) ⇒ Gradient
constructor
A new instance of Gradient.
- #points ⇒ Object
Constructor Details
#initialize(*colors) ⇒ Gradient
Returns a new instance of Gradient.
6 7 8 9 |
# File 'lib/color_fun/gradient.rb', line 6 def initialize(*colors) @colors = colors @resolution = 100 end |
Instance Attribute Details
#resolution ⇒ Object
Returns the value of attribute resolution.
4 5 6 |
# File 'lib/color_fun/gradient.rb', line 4 def resolution @resolution end |
Instance Method Details
#at(point) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/color_fun/gradient.rb', line 31 def at(point) window = point / color_window_size if point == @resolution difference = differences.last position = color_window_size window = differences.size - 1 else difference = differences[window] position = point % color_window_size end new_colors = difference.each_with_index.map do |dc, i| (colors[window][i] + (dc * (position.to_f / color_window_size.to_f))).to_i end ColorFun::RGB.new(*new_colors) end |
#color_window_size ⇒ Object
23 24 25 |
# File 'lib/color_fun/gradient.rb', line 23 def color_window_size @resolution / differences.size end |
#colors ⇒ Object
11 12 13 14 15 |
# File 'lib/color_fun/gradient.rb', line 11 def colors @colors.map do |color| color.scan(/../).map { |c| c.to_i(16) } end end |
#differences ⇒ Object
17 18 19 20 21 |
# File 'lib/color_fun/gradient.rb', line 17 def differences colors[0..-2].each_with_index.map do |color, i| color.dup.each_with_index.map { |c, j| colors[i + 1][j] - c } end end |
#points ⇒ Object
27 28 29 |
# File 'lib/color_fun/gradient.rb', line 27 def points @resolution.times.map { |i| self.at(i) } end |