# 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 .. _baseclass import ArtBaseClass
from math import fmod
from random import random
from opc.hue import hsvToRgb

    def start(self, matrix):
        matrix.clear()

    def _weightedAverage(self, v0, v1, percent):
        return v0 + (v1-v0)*percent

    def _bilinearInterp(self, gun, px, py):
        xi0 = self._weightedAverage(gun[0], gun[1], px)
        xi1 = self._weightedAverage(gun[2], gun[3], px)

        return self._weightedAverage(xi0, xi1, py)

    def _percent(self, v, max):
        return (v+1.0)/max

    def _interpolate(self, rgb, px, py):
        return tuple([self._bilinearInterp(gun, px, py) for gun in rgb])

    def _rotate(self, array):
        return list(zip(*array[::-1]))

    def refresh(self, matrix):

        """
        For any pair of colors represented as a hue, we need to calculate
        the distance between those two hues for each of the rgb components
        of the color. For the set of four corners, we have to interpolate
        each gun independently.
        """

# 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/baseclasses/bilinear.py
from .. _baseclass import ArtBaseClass
from math import fmod
from random import random
from opc.hue import hsvToRgb


    def start(self, matrix):
        matrix.clear()

    def _weightedAverage(self, v0, v1, percent):
        return v0 + (v1-v0)*percent

    def _bilinearInterp(self, gun, px, py):
        xi0 = self._weightedAverage(gun[0], gun[1], px)
        xi1 = self._weightedAverage(gun[2], gun[3], px)

        return self._weightedAverage(xi0, xi1, py)

    def _percent(self, v, max):
        return (v+1.0)/max

    def _interpolate(self, rgb, px, py):
        return tuple([self._bilinearInterp(gun, px, py) for gun in rgb])

    def _rotate(self, array):
        return list(zip(*array[::-1]))

    def refresh(self, matrix):

        """
        For any pair of colors represented as a hue, we need to calculate
        the distance between those two hues for each of the rgb components
        of the color. For the set of four corners, we have to interpolate
        each gun independently.
        """

        rgbs = self._rotate([hsvToRgb(self.cornerValues[i]) for i in range(4)])