R中是否有浅色或暗色功能? [英] Is there a is light or is dark color function in R?

查看:126
本文介绍了R中是否有浅色或暗色功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张ggplot2情节,并且百分比标签为白色,但有时候条形图的颜色太浅,白色标签变得难以辨认。是否有一个函数给出一个颜色值将返回,无论它是否是是昏暗的还是轻的?然后我可以将标签颜色设置为白色或黑色......

根据@MrFlick提供的(第二个)链接中的强度等级,黑色与白色之间的关系。 博客引用了W3C出版物:用于计算颜色感知亮度的标准公式,该算法使用RGB编码颜色的算法:

((红色值X 299)+(绿色值X 587)+(蓝色值X 114))/ 1000
<$ p
$ b> code>

col2rgb 函数提供了一个3行矩阵,我乘以该网页提供的因素。我使用了一个红色作为背景颜色的例子,所选文本将是白色。

),白)[1+(sum(col2rgb(red)* c(299,587,114))/ 1000 123)]
[1]white

 

(b)(b)(b)(b)(b)(b)(c)(b)


I have a ggplot2 plot and have percent labels in white but some times the color of the bar chart is too light and the white label becomes illegible. Is there a function that given a color value will return whether it is e.g. isDark or isLight? and then I can set the label color to white or black accordingly ...

解决方案

Here's a strategy to implement picking a text color of black vs white based on the intensity scale in the (second) link provided by @MrFlick.

The blog cited a W3C publication: a standard formula for calculating the perceived brightness of a color that used an algorithm for RGB encoded colors:

 ((Red value X 299) + (Green value X 587) + (Blue value X 114)) / 1000

The col2rgb function delivers a 3-row matrix which I multiply by the factors offered in that webpage. I used an example of "red" as a background color and the chosen text would then be "white"

 c( "black", "white")[  1+(sum( col2rgb("red") *c(299, 587,114))/1000 < 123) ]
[1] "white"

Implemented as a function:

isDark <- function(colr) { (sum( col2rgb(colr) * c(299, 587,114))/1000 < 123) }
isDark("red")
[1] TRUE

这篇关于R中是否有浅色或暗色功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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