将极坐标映射到颜色(RGB等)

时间:2018-01-12 02:04:58

标签: python colors rgb polar-coordinates color-theory

假设我有80个(或n)极坐标,它们在圆形区域内均匀分布。我希望每个极坐标都有一种独特的颜色。

如果你想象一个像这样的色轮(如果你愿意,它可能是一个不同的转换),我喜欢它给出极坐标的一种颜色。

color wheel

起初我没有使用实际的极坐标,只是通过一些甚至步幅缩放其中一个通道,如RGB (255, i * stride, 255)。但是现在我喜欢不同频谱的不同颜色(或者至少有一种以上的色调)。

我想过只使用一个色轮图像然后对其进行采样,但这似乎有点弱。是不是有一个公式我可以用来将极坐标转换为某个假设/生成的RGB,HSV或CMYK空间?

我在Python 3工作,但我最感兴趣的是公式/算法。我没有使用任何特定的绘图API。

1 个答案:

答案 0 :(得分:0)

您可以使用从HSV或HSL到RGB的转换,许多包,例如Colour(Numpy Vectorised)或python-colormath(Vanilla Python)都有实现:

颜色,假设您有Numpy以及tsplittstack定义:

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