"""Utility functions for plotting.

Author: Seth Axen
E-mail: seth.axen@gmail.com"""
from collections import deque

import numpy as np


def rgb_to_hsv(rgb):
    """Convert RGB colors to HSV colors."""
    r, g, b = tuple(map(float, rgb))
    if any([r > 1, g > 1, b > 1]):
        r /= 255.
        g /= 255.
        b /= 255.
    mmax = max(r, g, b)
    mmin = min(r, g, b)
    c = mmax - mmin
    if (c == 0.):
        hp = 0.
    elif (mmax == r):
        hp = ((g - b) / c) % 6
    elif (mmax == g):
        hp = ((b - r) / c) + 2
    elif (mmax == b):
        hp = ((r - g) / c) + 4
    h = 60 * hp
    v = mmax
    if (c == 0):
        s = 0
    else:
        s = c / v
    return (h, s, v)


def hsv_to_rgb(hsv):
    """Convert HSV colors to RGB colors."""
    h, s, v = tuple(map(float, hsv))
    c = v * s
    m = v - c
    hp = h / 60.
    x = c * (1. - abs((hp % 2) - 1.))
    hp = int(hp)
    rgb = deque((c + m, x + m, m))
    if (hp % 2):
        rgb.reverse()
        rgb.rotate((hp - 3) / 2)
    else:
        rgb.rotate(hp / 2)
    return tuple(rgb)


def rgb_to_yuv(rgb):
    """Convert RGB colors to Y'UV colors, useful for comparison."""
    rgbv = np.array(rgb).reshape(3, 1)
    if np.any(rgbv > 1.):
        rgbv = rgbv / 255.
    yuv = np.dot(np.array([[ .299,    .587,    .114],
                           [-.14713, -.28886,  .436],
                           [ .615,   -.51499, -.10001]], dtype=np.double),
                 rgbv)
    return list(yuv)


def yuv_to_rgb(yuv):
    """Convert Y'UV colors to RGB colors."""
    yuvv = np.array(yuv).reshape(3, 1)
    rgb = np.dot(np.array([[1., 0.,      1.13983],
                           [1., -.39465, -.58060],
                           [1., 2.03211, 0.]], dtype=np.double),
                 yuvv)
    return list(rgb)


def compute_yuv_dist(rgb1, rgb2):
    """Compute Euclidean Y'UV distance between RGB colors."""
    yuv1 = rgb_to_yuv(rgb1)
    yuv2 = rgb_to_yuv(rgb2)
    return float(sum((np.array(yuv1) - np.array(yuv2))**2)**.5)


def lighten_rgb(rgb, p=0.):
    """Lighten RGB colors by percentage p of total."""
    h, s, v = rgb_to_hsv(rgb)
    hsv = (h, s, min(1, v + p))
    return hsv_to_rgb(hsv)

[((912, 936), 'collections.deque', 'deque', (['(c + m, x + m, m)'], {}), '((c + m, x + m, m))\n', (917, 936), False, 'from collections import deque\n'), ((1205, 1223), 'numpy.any', 'np.any', (['(rgbv > 1.0)'], {}), '(rgbv > 1.0)\n', (1211, 1223), True, 'import numpy as np\n'), ((1268, 1381), 'numpy.array', 'np.array', (['[[0.299, 0.587, 0.114], [-0.14713, -0.28886, 0.436], [0.615, -0.51499, -\n    0.10001]]'], {'dtype': 'np.double'}), '([[0.299, 0.587, 0.114], [-0.14713, -0.28886, 0.436], [0.615, -\n    0.51499, -0.10001]], dtype=np.double)\n', (1276, 1381), True, 'import numpy as np\n'), ((1602, 1702), 'numpy.array', 'np.array', (['[[1.0, 0.0, 1.13983], [1.0, -0.39465, -0.5806], [1.0, 2.03211, 0.0]]'], {'dtype': 'np.double'}), '([[1.0, 0.0, 1.13983], [1.0, -0.39465, -0.5806], [1.0, 2.03211, 0.0\n    ]], dtype=np.double)\n', (1610, 1702), True, 'import numpy as np\n'), ((1170, 1183), 'numpy.array', 'np.array', (['rgb'], {}), '(rgb)\n', (1178, 1183), True, 'import numpy as np\n'), ((1557, 1570), 'numpy.array', 'np.array', (['yuv'], {}), '(yuv)\n', (1565, 1570), True, 'import numpy as np\n'), ((1972, 1986), 'numpy.array', 'np.array', (['yuv1'], {}), '(yuv1)\n', (1980, 1986), True, 'import numpy as np\n'), ((1989, 2003), 'numpy.array', 'np.array', (['yuv2'], {}), '(yuv2)\n', (1997, 2003), True, 'import numpy as np\n')]