Class: ColorFun::Gradient

Inherits:
Object
  • Object
show all
Defined in:
lib/color_fun/gradient.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#resolutionObject

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_sizeObject



23
24
25
# File 'lib/color_fun/gradient.rb', line 23

def color_window_size
  @resolution / differences.size
end

#colorsObject



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

#differencesObject



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

#pointsObject



27
28
29
# File 'lib/color_fun/gradient.rb', line 27

def points
  @resolution.times.map { |i| self.at(i) }
end