2

Say I have 80 (or n) polar coordinates that are pretty evenly distributed across a circular area. I want a unique color for each polar coordinate.

If you imagine a color wheel like this (though it could be a different transformation if you like), I'd like one of its colors given a polar coordinate.

color wheel

At first I was not using the actual polar coordinates, and just scaled one of the channels by some even stride, like RGB (255, i * stride, 255). But now I'd like different colors from all over the spectrum (or at least more than a single color tone).

I thought of just using an image of a color wheel and then sampling it, but that seems kind of weak. Isn't there a formula I could use to convert the polar coordinates to some assumed/generated RGB, HSV, or CMYK space?

I'm working in Python 3, but I'm mostly interested in the formulas/algorithm. I'm not using any specific plotting API.

delrocco
  • 453
  • 3
  • 23
  • 2
    You could use HSV or HLS, and use angle for hue, and radius for either saturation or for value / lightness. Check out the `colorsys` module. – PM 2Ring Jan 12 '18 at 02:07
  • Worked like a dream :) I guess it's an easy question if you know anything about HSV color model (which I didn't lol). Post as a more complete answer and I'll accept it. thx! – delrocco Feb 15 '18 at 20:02

1 Answers1

0

You could use a conversion from HSV or HSL to RGB, many packages such as Colour (Numpy Vectorised) or python-colormath (Vanilla Python) have implementations:

From Colour, assuming you have Numpy and the tsplit and tstack definitions:

def RGB_to_HSV(RGB):
    """
    Converts from *RGB* colourspace to *HSV* colourspace.

    Parameters
    ----------
    RGB : array_like
        *RGB* colourspace array.

    Returns
    -------
    ndarray
        *HSV* array.

    Notes
    -----
    -   Input *RGB* colourspace array is in domain [0, 1].
    -   Output *HSV* colourspace array is in range [0, 1].

    References
    ----------
    -   :cite:`EasyRGBj`
    -   :cite:`Smith1978b`
    -   :cite:`Wikipediacg`

    Examples
    --------
    >>> RGB = np.array([0.49019608, 0.98039216, 0.25098039])
    >>> RGB_to_HSV(RGB)  # doctest: +ELLIPSIS
    array([ 0.2786738...,  0.744     ,  0.98039216])
    """

    maximum = np.amax(RGB, -1)
    delta = np.ptp(RGB, -1)

    V = maximum

    R, G, B = tsplit(RGB)

    S = np.asarray(delta / maximum)
    S[np.asarray(delta == 0)] = 0

    delta_R = (((maximum - R) / 6) + (delta / 2)) / delta
    delta_G = (((maximum - G) / 6) + (delta / 2)) / delta
    delta_B = (((maximum - B) / 6) + (delta / 2)) / delta

    H = delta_B - delta_G
    H = np.where(G == maximum, (1 / 3) + delta_R - delta_B, H)
    H = np.where(B == maximum, (2 / 3) + delta_G - delta_R, H)
    H[np.asarray(H < 0)] += 1
    H[np.asarray(H > 1)] -= 1
    H[np.asarray(delta == 0)] = 0

    HSV = tstack((H, S, V))

    return HSV


def HSV_to_RGB(HSV):
    """
    Converts from *HSV* colourspace to *RGB* colourspace.

    Parameters
    ----------
    HSV : array_like
        *HSV* colourspace array.

    Returns
    -------
    ndarray
        *RGB* colourspace array.

    Notes
    -----
    -   Input *HSV* colourspace array is in domain [0, 1].
    -   Output *RGB* colourspace array is in range [0, 1].

    References
    ----------
    -   :cite:`EasyRGBn`
    -   :cite:`Smith1978b`
    -   :cite:`Wikipediacg`

    Examples
    --------
    >>> HSV = np.array([0.27867384, 0.74400000, 0.98039216])
    >>> HSV_to_RGB(HSV)  # doctest: +ELLIPSIS
    array([ 0.4901960...,  0.9803921...,  0.2509803...])
    """

    H, S, V = tsplit(HSV)

    h = np.asarray(H * 6)
    h[np.asarray(h == 6)] = 0

    i = np.floor(h)
    j = V * (1 - S)
    k = V * (1 - S * (h - i))
    l = V * (1 - S * (1 - (h - i)))  # noqa

    i = tstack((i, i, i)).astype(np.uint8)

    RGB = np.choose(
        i, [
            tstack((V, l, j)),
            tstack((k, V, j)),
            tstack((j, V, l)),
            tstack((j, k, V)),
            tstack((l, j, V)),
            tstack((V, j, k)),
        ],
        mode='clip')

    return RGB


def RGB_to_HSL(RGB):
    """
    Converts from *RGB* colourspace to *HSL* colourspace.

    Parameters
    ----------
    RGB : array_like
        *RGB* colourspace array.

    Returns
    -------
    ndarray
        *HSL* array.

    Notes
    -----
    -   Input *RGB* colourspace array is in domain [0, 1].
    -   Output *HSL* colourspace array is in range [0, 1].

    References
    ----------
    -   :cite:`EasyRGBl`
    -   :cite:`Smith1978b`
    -   :cite:`Wikipediacg`

    Examples
    --------
    >>> RGB = np.array([0.49019608, 0.98039216, 0.25098039])
    >>> RGB_to_HSL(RGB)  # doctest: +ELLIPSIS
    array([ 0.2786738...,  0.9489796...,  0.6156862...])
    """

    minimum = np.amin(RGB, -1)
    maximum = np.amax(RGB, -1)
    delta = np.ptp(RGB, -1)

    R, G, B = tsplit(RGB)

    L = (maximum + minimum) / 2

    S = np.where(L < 0.5, delta / (maximum + minimum),
                delta / (2 - maximum - minimum))
    S[np.asarray(delta == 0)] = 0

    delta_R = (((maximum - R) / 6) + (delta / 2)) / delta
    delta_G = (((maximum - G) / 6) + (delta / 2)) / delta
    delta_B = (((maximum - B) / 6) + (delta / 2)) / delta

    H = delta_B - delta_G
    H = np.where(G == maximum, (1 / 3) + delta_R - delta_B, H)
    H = np.where(B == maximum, (2 / 3) + delta_G - delta_R, H)
    H[np.asarray(H < 0)] += 1
    H[np.asarray(H > 1)] -= 1
    H[np.asarray(delta == 0)] = 0

    HSL = tstack((H, S, L))

    return HSL
Kel Solaar
  • 2,626
  • 1
  • 17
  • 23