Sha256: d92df6a8e7c32f67706dd0001c436c2a9041d42d135b6df89c28a8d9ffc4ecba

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'ws_light/set/color_set'

module WSLight
  module Set
    # Creates semolina with raspberries
    class SemolinaSet < ColorSet
      COLOR_SEMOLINA = Color.new(255, 127, 15)
      COLOR_RASPBERRY = Color.new(255, 7, 15)
      RASPBERRY_SIZE = 10
      RASPBERRY_COUNT = 8

      def init
        @raspberries = []

        while @raspberries.size < RASPBERRY_COUNT
          position = rand(@full_length)
          @raspberries << position unless at_end?(position) || between_strips?(position) || raspberry?(position)
        end
      end

      def between_strips?(position)
        @type == :double && ((@full_length / 2 - 1 - RASPBERRY_SIZE)..(@full_length / 2)).cover?(position)
      end

      def at_end?(position)
        position >= (@full_length - 1 - RASPBERRY_SIZE)
      end

      def frame
        @set ||= create_frame
      end

      def create_frame
        set = []

        @full_length.times do |i|
          set << (raspberry?(i) ? COLOR_RASPBERRY : COLOR_SEMOLINA)
        end

        set
      end

      def raspberry?(pixel)
        @raspberries.each do |raspberry|
          return true if pixel > raspberry && pixel < (raspberry + RASPBERRY_SIZE)
        end
        false
      end

      def pixel(number)
        frame[number]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ws_light-0.4.2 lib/ws_light/set/semolina_set.rb