In [1]:
import numpy as np
import matplotlib.pyplot as plt
from math import sin, pi

def bgr_to_hsv(bgr):
    v = np.amax(bgr)
    c = v - np.amin(bgr)
    s = 0 if v == 0 else c / v
    if c == 0:
        h = 0
    else:
        b, g, r = bgr
        if v == r:
            h = 0 + (g - b) / c
        elif v == g:
            h = 2 + (b - r) / c
        else:
            h = 4 + (r - g) / c
        h /= 6
    return h, s, v
In [2]:
x = np.linspace(0, 2 * pi, 100)
r = [(sin(1 * i) + 1) / 2 for i in x]
g = [(sin(2 * i) + 1) / 2 for i in x]
b = [(sin(3 * i) + 1) / 2 for i in x]
bgr = list(zip(b, g, r))
hsv = np.array([bgr_to_hsv(a) for a in bgr])

plt.figure(figsize=(16, 8))
plt.subplot(121)
plt.plot(x, r, color='r')
plt.plot(x, g, color='g')
plt.plot(x, b, color='b')
plt.legend(('r', 'g', 'b'))
plt.subplot(122)
plt.plot(x, hsv[:, 0], linewidth=3)
plt.plot(x, hsv[:, 1], linestyle=':')
plt.plot(x, hsv[:, 2], linestyle=':')
plt.legend(('hue', 'sat', 'val'))
plt.show()