给定颜色的透明等效项 [英] Transparent equivalent of given color

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

问题描述

我已经经历过几次了,所以去吧:我正在绘制一些图,这些图包含带有给定调整参数的参数估计值的曲线。

I've had this a few times, so here goes: I'm making some plots which hold curves with estimates of a parameter given a tuning parameter.

通常,每个估计值都有一个SD,因此我可以在每个估计值周围显示误差线。

Typically, I also have SDs for each estimated value, so I could show error bars around each of them.

但是,我不喜欢误差线,所以我更喜欢一些平滑的版本。这本身没有问题(即我知道如何执行此操作)。但是,我的绘图包含其中一些相似的曲线,每个曲线都有其自己的颜色。因此,我想以与曲线本身的颜色匹配的颜色向每个曲线添加平滑的误差区域。当然,我想这样做是透明的,所以我仍然可以看到错误区域中的其他曲线。

However, I don't like error bars, and would prefer some smoothed version of it. This is in itself no problem (ie I know how to do this). However, my plot contains several of these similar curves, each one in its own color. So I would like to add the 'smoothed errorregion' to each curve in a color that matches the color of the curve itself. Of course, I would like to do this somewhat transparently, so I can still see the other curves through the 'error region'.

所以,我的问题是:给定一个颜色(由数字,名称或rgb值指定-注意前两个会带来额外的问题,但是这种情况经常发生,因为基本的绘图功能会将这些值当作有效的颜色值),如何找到查找具有相同rgb但具有不同(给定)alpha级别(透明度)的匹配颜色。我想要一个像这样的函数:

So, my question is: given a color (specified by either a number, a name or an rgb value --- note the first two pose an extra problem, but this is occurring rather often, as the basic plotting functions take these as valid color values), how can I find find the matching color that has the same rgb but a different (given) alpha level (transparency). I would like a function like:

makeTransparent<-function(someColor, alpha=100)
{
  newColor<-someColor + alpha #I wish
  return(newColor)
}

这应该适用于以下情况:

This should work for things like:

makeTransparent(2)
makeTransparent("red")
makeTransparent(rgb(0,0,1))

编辑当我错过明显的事情时讨厌它,但是@themel指出了它(再次感谢)。这是一个完整的解决方案(注意:矢量化的作品,因此您可以传递多种颜色;不过目前仅支持一种Alpha):

Edit I hate it when I miss something obvious, but @themel pointed me to it (thx again). Here's a full solution (note: works vectorized, so you can pass more than one color; only one alpha is supported at this time though):

#note: always pass alpha on the 0-255 scale
makeTransparent<-function(someColor, alpha=100)
{
  newColor<-col2rgb(someColor)
  apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2],
    blue=curcoldata[3],alpha=alpha, maxColorValue=255)})
}


推荐答案

您看过?rgb


用法:

Usage:

rgb(红色,绿色,蓝色,alpha,名称= NULL,maxColorValue = 1)

rgb(red, green, blue, alpha, names = NULL, maxColorValue = 1)

还可以指定Alpha透明度值(不透明度为
,因此 0表示完全透明,而 max表示不透明)。如果未指定
alpha',则会生成不透明的颜色。

An alpha transparency value can also be specified (as an opacity, so ‘0’ means fully transparent and ‘max’ means opaque). If alpha’ is not specified, an opaque colour is generated.

alpha 参数用于指定透明度。 col2rgb 将以其他方式指定的R颜色拆分为RGB,因此您可以将它们提供给 rgb

The alpha parameter is for specifying transparency. col2rgb splits R colors specified in other ways into RGB so you can feed them to rgb.

这篇关于给定颜色的透明等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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