改变RGB颜色的色调 [英] Changing hue of rgba color

查看:189
本文介绍了改变RGB颜色的色调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RGBA颜色在对数刻度上绘制一堆数据,作为matplotlib中的散点图(以防万一介质是相关的)。我想做的是一旦绘制了所有内容,我想挑选出单个散点并将其色相更改为某些RGB颜色的色相,但保留旧的alpha值。我目前的操作方式是:

I'm plotting a bunch of data on a logscale as a scatter plot in matplotlib (just in case the medium is relevant) using RGBA colors. What I would like to be able to do is once I plotted everything, I want to pick out individual scatter points and change their hue to the hue of some RGB color, but preserving the old alpha value. The way I currently do it is this:

oldHSV = rgb_to_hsv(oldRGBA[:3])
newHSV = rgb_to_hsv(newRGB)
oldHSV[0] = newHSV[0]
newRGBA = hsv_to_rgb(oldHSV).tolist() + [oldRGBA[3]]

即我将旧RGBA值的RGB部分取为原色,将其更改为HSV,对新的预期RGB颜色执行相同操作,然后替换色相,将其恢复为RGB并添加旧的alpha值。

i.e. I take the RGB part of the old RGBA value, turn it to HSV, do the same for the new intended RGB color, then replace the hue, turn it back to RGB and add on the old alpha value.

由于我已经做过数千次,所以花费的时间可能比我在这里花费的时间长得多。一种可能的解决方法是深入研究RGB和HSV之间的转换,并一目了然地知道如何进行转换,但是我希望知道如何处理颜色的人(我真的不知道)已经找到了简单而有效的方法

Since I'm doing this thousands of times, this can take significantly longer than I would like to spend here. One possible fix would be to dig into the conversion between RGB and HSV and figure out how to do this in one go, but I was hoping that folks who know how to handle color (I really don't) have figured out simple and efficient ways to do this.

如何在保留A的alpha值的同时将给定RGBA颜色A的色相更改为给定RGB颜色B的色相?使用不同的颜色模型(例如HSL)是否可以简化任务,如果可以,这会有所帮助吗?

How do I change the hue of a given RGBA color A to that of a given RGB color B while preserving the alpha value of A? Would using a different color model (HSL for example) simplify the task, and if so, which would help?

推荐答案

一次性完成所有替换的解决方案:

Here is the solution to do all the replacement in one go :

import matplotlib.colors as clr
import matplotlib.pyplot as plt
import numpy as np


N = 100000
x = 1.2 + 800.0 * np.random.rand(N)
y = 1.2 + 800.0 * np.random.rand(N)
# Generate random colors of the form (r, g, b, a) where r = 0.0
colors = np.random.rand(4 * N).reshape((N, 4))
colors[:, 0] = 0.0
area = np.pi * (5 * np.random.rand(N))**2

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
pcol = ax.scatter(x, y, s=area, c=colors)
ax.set_xscale('log')
ax.set_yscale('log')

# Save initial figure
plt.savefig("hue.jpg")

oldRGBA = pcol.get_facecolors().reshape((N, 1, 4))
oldRGB = oldRGBA[:, :, :3]
newRGB = oldRGB
newRGB[:, :, 0] = 1.0 # Set red component to 1.0
oldHSV = clr.rgb_to_hsv(oldRGB)
newHSV = clr.rgb_to_hsv(newRGB)
oldHSV[:, :, 0] = newHSV[:, :, 0]
newRGBA = np.copy(oldRGBA)
newRGBA[:, :, :3] = clr.rgb_to_hsv(oldHSV)
pcol.set_facecolors(newRGBA[:, 0, :])

# Save modified figure
plt.savefig("hue_bis.jpg")

plt.close()

如您所见,此代码尝试绘制100000点,实际上它在大约2秒钟内就成功做到了。这是产生的数字:

As you can see, this code attempts to plot 100000 points and in fact it managed to do this in about 2 seconds. Here are the figures produced :

和:

最后两个问题:


如何在保留A的alpha值的同时将给定的RGBA颜色A的色调更改为给定的RGB颜色B的色调?

How do I change the hue of a given RGBA color A to that of a given RGB color B while preserving the alpha value of A ?

和:


将使用其他颜色模型(例如HSL)简化任务,如果是,

Would using a different color model (HSL for example) simplify the task, and if so, which would help

我认为您进行此类修改的方法很可观,它避免了手工计算(请参见 HSL和HSV )。可以使用其他颜色模型,HSL和HSV都可以在不影响其他参数的情况下更改色相,但这只是另一种方法,而不是更好的方法。

I think that your approach to do such a modification is appreciable, it avoids making calculations by hand (see HSL and HSV). Using a different color model is possible, both HSL and HSV allow to change the hue without affecting other parameters, but that is only an other way to do it and not a better one.

希望这会有所帮助。

这篇关于改变RGB颜色的色调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆