# Path: lib/color_fade.py
# class ColorFade(object):
#     """Represents the fade of one color to another"""
# 
#     def __init__(self, keyframes, steps):
#         """
#         keyframes: a list of 3-element tuples representing the colors to fade between.
#         """
# 
#         self._steps = steps
#         self.keyframes = keyframes
#         self.color_cache = np.zeros(steps + 1, dtype=dtypes.pixel_color)
# 
#         # Warmup the cache
#         for i in range(steps + 1):
#             overall_progress = float(i) * (len(self.keyframes) - 1) / self._steps
#             stage = int(overall_progress)
#             stage_progress = overall_progress - stage # 0 to 1 float
# 
#             # special case stage_progress=0, so if progress=1, we don't get
#             # an IndexError
#             if stage_progress == 0:
#                 color = self.keyframes[stage]
#             else:
#                 frame1 = self.keyframes[stage]
#                 frame1_weight = 1 - stage_progress
# 
#                 frame2 = self.keyframes[stage + 1]
#                 frame2_weight = stage_progress
# 
#                 color = tuple([c1 * frame1_weight + c2 * frame2_weight for c1, c2 in zip(frame1, frame2)])
#             self.color_cache[i] = tuple(color)
# #            print progress, self.color_cache[progress]
# 
#     def get_color(self, progress):
#         """
#         Given a progress value between 0 and steps, returns the color for that
#         progress as a (h, l, s) tuple with float values
#         """
# 
#         progress = clip(0, int(progress), self._steps)
# 
#         return self.color_cache[progress]
# 
# Path: lib/dtypes.py

# 
# Path: lib/buffer_utils.py
# def struct_flat(arr):
#     """
#     Returns a flattened view of a structured array whose structure elements
#     are of a homogeneous type
#     """
#     return arr.view(dtype=arr.dtype[0])


from lib.color_fade import ColorFade
from lib import dtypes
from lib.buffer_utils import struct_flat
import colorsys
import numpy as np
    else: # hacky support for old blend
        l = np.sqrt(np.square(x) + np.square(y)) / 2

    h = np.arctan2(y, x) / (2*np.pi)

    nocolor = (x * y == 0)
    np.where(nocolor, h, 0)
    np.where(nocolor, s, 0)

    np.clip(l, 0, 1, l)

    if out is None:
        out = np.empty_like(start)

    out['hue'] = h
    out['light'] = l
    out['sat'] = s

    return out

def rgb_to_hls(arr):
    """ fast rgb_to_hls using numpy array """
    # XXX: arr is assumed to be an unstructured, multidimensional integer array
    # rather than an array with dtype rgb_color. This is because the only user
    # of this function is ImagePattern and I don't feel like fixing that.

    # adapted from Arnar Flatberg
    # http://www.mail-archive.com/numpy-discussion@scipy.org/msg06147.html

    arr = arr.astype("float32") / 255.0

# Path: lib/color_fade.py
# class ColorFade(object):
#     """Represents the fade of one color to another"""
# 
#     def __init__(self, keyframes, steps):
#         """
#         keyframes: a list of 3-element tuples representing the colors to fade between.
#         """
# 
#         self._steps = steps
#         self.keyframes = keyframes
#         self.color_cache = np.zeros(steps + 1, dtype=dtypes.pixel_color)
# 
#         # Warmup the cache
#         for i in range(steps + 1):
#             overall_progress = float(i) * (len(self.keyframes) - 1) / self._steps
#             stage = int(overall_progress)
#             stage_progress = overall_progress - stage # 0 to 1 float
# 
#             # special case stage_progress=0, so if progress=1, we don't get
#             # an IndexError
#             if stage_progress == 0:
#                 color = self.keyframes[stage]
#             else:
#                 frame1 = self.keyframes[stage]
#                 frame1_weight = 1 - stage_progress
# 
#                 frame2 = self.keyframes[stage + 1]
#                 frame2_weight = stage_progress
# 
#                 color = tuple([c1 * frame1_weight + c2 * frame2_weight for c1, c2 in zip(frame1, frame2)])
#             self.color_cache[i] = tuple(color)
# #            print progress, self.color_cache[progress]
# 
#     def get_color(self, progress):
#         """
#         Given a progress value between 0 and steps, returns the color for that
#         progress as a (h, l, s) tuple with float values
#         """
# 
#         progress = clip(0, int(progress), self._steps)
# 
#         return self.color_cache[progress]
# 
# Path: lib/dtypes.py

# 
# Path: lib/buffer_utils.py
# def struct_flat(arr):
#     """
#     Returns a flattened view of a structured array whose structure elements
#     are of a homogeneous type
#     """
#     return arr.view(dtype=arr.dtype[0])


# Path: lib/colors.py
from lib.color_fade import ColorFade
from lib import dtypes
from lib.buffer_utils import struct_flat
import colorsys
import numpy as np

    else: # hacky support for old blend
        l = np.sqrt(np.square(x) + np.square(y)) / 2

    h = np.arctan2(y, x) / (2*np.pi)

    nocolor = (x * y == 0)
    np.where(nocolor, h, 0)
    np.where(nocolor, s, 0)

    np.clip(l, 0, 1, l)

    if out is None:
        out = np.empty_like(start)

    out['hue'] = h
    out['light'] = l
    out['sat'] = s

    return out

def rgb_to_hls(arr):
    """ fast rgb_to_hls using numpy array """
    # XXX: arr is assumed to be an unstructured, multidimensional integer array
    # rather than an array with dtype rgb_color. This is because the only user
    # of this function is ImagePattern and I don't feel like fixing that.

    # adapted from Arnar Flatberg
    # http://www.mail-archive.com/numpy-discussion@scipy.org/msg06147.html

    arr = arr.astype("float32") / 255.0

    out = np.empty(arr.shape[0], dtype=dtypes.hls_color)