Class: Gradient

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

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination) ⇒ Gradient

Returns a new instance of Gradient.



2
3
4
5
# File 'lib/gradient.rb', line 2

def initialize(origin, destination)
    @origin = origin
    @destination = destination
end

Instance Method Details

#colour_at(distance_percent) ⇒ Object



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

def colour_at(distance_percent)
    c = @origin.class.to_s.downcase

    origin_rgb = @origin.to_rgb
    destination_rgb = @destination.to_rgb

    delta_r = destination_rgb.r - origin_rgb.r
    delta_g = destination_rgb.g - origin_rgb.g
    delta_b = destination_rgb.b - origin_rgb.b

    approximation = RGB.new(delta_r * distance_percent + origin_rgb.r,
                            delta_g * distance_percent + origin_rgb.g,
                            delta_b * distance_percent + origin_rgb.b)
    approximation.send("to_" + c)
end