r - 如何计算R中多维颜色的渐变

标签 r graphics colors ggplot2 palette

这与this question有关但也许是一个更简单的例子。我很好奇是否有一种合理的方法来计算给定三或四种任意颜色的多维颜色渐变,就像 r rgb()功能有红、绿、蓝吗?一维梯度很容易(图 1)但是我不清楚如何计算二维梯度(图 2)三角形内 .边缘很容易。这才是最重要的

# one dimensional color gradient
one.dimensions <- colorRampPalette( c( "orange" , "blue" ) )( 100 )

plot( 1:100 , rep( 1 , 100 ) , col = one.dimensions , cex = 3 , pch = 16 , main  = 'one dimensional gradient' )

enter image description here
# here are the edges of a three-colored triangle
dimensions13 <- colorRampPalette( c( "orange" , "blue" ) )( 100 )
dimensions12 <- colorRampPalette( c( "orange" , "red" ) )( 100 )
dimensions23 <- colorRampPalette( c( "blue" , "red" ) )( 100 )

plot( 1:100 , c( 1:50 , 50:1 ) , type = 'n' , main = 'two dimensional gradient' )
points( 1:100 , rep( 1 , 100 ) , col = dimensions12 , cex = 3 , pch = 16 )
points( seq( 1 , 50 , length = 100 ) , seq( 1 , 50 , length = 100 ) , col = dimensions13 , cex = 3 , pch = 16 )
points( seq( 50 , 100 , length = 100 ) , seq( 50 , 1 , length = 100 ) , col = dimensions23 , cex = 3 , pch = 16 )

enter image description here

最佳答案

您可以考虑三种基本的颜色混合策略:

1- 减法,使用 R 图形的 alpha 透明度混合。基本上,用自己的渐变叠加多个图层。

library(grid)

grid.newpage()
grid.raster(scales::alpha(colorRampPalette(c("white","blue"))(10), 0.3),
            width=1,height=1)
grid.raster(t(scales::alpha(colorRampPalette(c("white","red"))(10), 0.3)),
            width=1,height=1)

一个缺点是最终颜色取决于层的顺序。

enter image description here

CMYK colour model可能是另一个灵感来源。

2-添加剂。我想出了一个简单的实现,如下所示。考虑你的 N 种基本颜色(比如黄色、绿色、橙色)。为它们分配可见光谱的波长(570nm、520nm、600nm)。根据三角形中的位置为每种颜色赋予一个权重(想想 N 个具有可调强度的激光器)。现在要获得与 N 个激光源混合相关的颜色,您需要与 CIE colour matching functions 进行卷积。 .这是一种物理声音混合,将数字映射到视觉感知。但是,显然存在唯一性问题:几种组合可能会产生相同的颜色。毕竟眼睛只有三种不同类型的受体,所以 N>3 永远不会导致双射。

3- 像素化 ( halftoning )。将图像划分为相邻的小区域,例如 LCD 屏幕,每个像素分为 N 个子像素,每个子像素都有自己的颜色。从远处和/或足够的屏幕/打印分辨率看,眼睛将看不到细节,并且会为您模糊相邻的颜色。

关于r - 如何计算R中多维颜色的渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26571483/

相关文章:

javascript - 具有透明度、多边形、读取图像像素的简单图形 API?

ios - 一个项目可以使用哪种图形框架?

java - 如何在Java中绘制绝对自定义的形状?

image - 压缩 JPEG 图像的 L、a、b 空间的不确定性

java - 为背景创建状态列表可绘制 XML - Android

html - 如何获得半透明的边框图像以显示其背后的边框颜色?

r - 获取该月的第二个和第四个星期六

r - ggplot2 中 y 刻度标签的良好 K/M/G 缩写

javascript - 存储 session 详细信息以供将来数据上传到 opencpu 后重用

r - 从R中的向量中提取交替序列