Class: WSLight::Set::RainSet

Inherits:
ColorSet show all
Defined in:
lib/ws_light/set/weather/rain_set.rb

Overview

Creates a set with all random colors

Constant Summary collapse

FRAMES_PER_DROP =
20
VISIBLE_DROPS =
35
CLOUD =
Color.new(4, 4, 4)

Constants inherited from ColorSet

ColorSet::DEFAULT_LENGTH, ColorSet::DEFAULT_TYPE

Instance Attribute Summary

Attributes inherited from ColorSet

#color, #full_length, #length, #type

Instance Method Summary collapse

Methods inherited from ColorSet

#frame_data, #initialize

Constructor Details

This class inherits a constructor from WSLight::Set::ColorSet

Instance Method Details

#brightness(frame_distance) ⇒ Object



48
49
50
# File 'lib/ws_light/set/weather/rain_set.rb', line 48

def brightness(frame_distance)
  (250 * (1.0 - (frame_distance.to_f / FRAMES_PER_DROP.to_f))).to_i
end

#draw_drop(position, drop_frame) ⇒ Object



39
40
41
42
# File 'lib/ws_light/set/weather/rain_set.rb', line 39

def draw_drop(position, drop_frame)
  blue = brightness(drop_frame.to_f) % 250
  @set[position] = Color.new(4, 4, 4 + blue)
end

#frameObject



25
26
27
28
# File 'lib/ws_light/set/weather/rain_set.rb', line 25

def frame
  next_frame
  @set
end

#generate_frameObject



30
31
32
33
34
35
36
37
# File 'lib/ws_light/set/weather/rain_set.rb', line 30

def generate_frame
  @set = [CLOUD] * @full_length
  start = (@frame_count * VISIBLE_DROPS / FRAMES_PER_DROP) % @max
  VISIBLE_DROPS.times do |i|
    drop_ratio = (FRAMES_PER_DROP.to_f / VISIBLE_DROPS.to_f)
    draw_drop(@drops[start + i], @frame_count - ((start - VISIBLE_DROPS + 1 + i) * drop_ratio).to_i)
  end
end

#initObject



11
12
13
14
15
16
17
# File 'lib/ws_light/set/weather/rain_set.rb', line 11

def init
  @drops = (0..(@full_length - 1)).to_a.shuffle
  @max = @drops.size
  # append the start at the end to ensure same result window when near the % @max
  @drops += @drops[0, VISIBLE_DROPS]
  generate_frame
end

#next_frameObject



19
20
21
22
23
# File 'lib/ws_light/set/weather/rain_set.rb', line 19

def next_frame
  @frame_count += 1
  @frame_count = @frame_count % (@max * FRAMES_PER_DROP)
  generate_frame
end

#pixel(number) ⇒ Object



44
45
46
# File 'lib/ws_light/set/weather/rain_set.rb', line 44

def pixel(number)
  @set[number]
end