# Path: opc/hue.py
# @mwt(timeout=20)
# @timefunc
# def hsvToRgb(h, s=1.0, v=1.0, rainbow=True):
#     """
#     Convert a (h, s, v) value to the (r, g, b) color space. By default,
#     we use a more cpu-intense method to curve the hue ramps above the
#     typical linear, with a view to evening out the visible range of yellow,
#     cyan, and purple.
# 
#     This method takes h, s, and v values in range 0..1 and returns r, g,
#     and b values in range 0..255.
#     """
#     if rainbow:
#         return [sin(c*pi/2)*255 for c in colorsys.hsv_to_rgb(h, s, v)]
# 
#     return [c*255 for c in colorsys.hsv_to_rgb(h, s, v)]


from opc.hue import hsvToRgb
import math
import random
import numpy



HSCALE = 0.01
RESET_INTERVAL = 20


class Art(object):

    description = "Conway's Game of Life"

    def __init__(self, matrix, config):
        self._interval = 300 if math.sqrt(matrix.numpix) < 20 else 75
        self.hue = random.random()
        self._init(matrix)

    def start(self, matrix):
        pass

    def _init(self, matrix):
        self.lifes = numpy.empty([matrix.width, matrix.height])
        self.prior = numpy.copy(self.lifes)

        for y in range(matrix.height):
            for x in range(matrix.width):
                self.lifes[x, y] = bool(random.getrandbits(1))

        self.reset_counter = 0

    def _hue(self, offset):

# Path: opc/hue.py
# @mwt(timeout=20)
# @timefunc
# def hsvToRgb(h, s=1.0, v=1.0, rainbow=True):
#     """
#     Convert a (h, s, v) value to the (r, g, b) color space. By default,
#     we use a more cpu-intense method to curve the hue ramps above the
#     typical linear, with a view to evening out the visible range of yellow,
#     cyan, and purple.
# 
#     This method takes h, s, and v values in range 0..1 and returns r, g,
#     and b values in range 0..255.
#     """
#     if rainbow:
#         return [sin(c*pi/2)*255 for c in colorsys.hsv_to_rgb(h, s, v)]
# 
#     return [c*255 for c in colorsys.hsv_to_rgb(h, s, v)]


# Path: art/life.py
from opc.hue import hsvToRgb
import math
import random
import numpy




HSCALE = 0.01
RESET_INTERVAL = 20


class Art(object):

    description = "Conway's Game of Life"

    def __init__(self, matrix, config):
        self._interval = 300 if math.sqrt(matrix.numpix) < 20 else 75
        self.hue = random.random()
        self._init(matrix)

    def start(self, matrix):
        pass

    def _init(self, matrix):
        self.lifes = numpy.empty([matrix.width, matrix.height])
        self.prior = numpy.copy(self.lifes)

        for y in range(matrix.height):
            for x in range(matrix.width):
                self.lifes[x, y] = bool(random.getrandbits(1))

        self.reset_counter = 0

    def _hue(self, offset):

        return hsvToRgb(self.hue+HSCALE*offset)