【问题标题】:how to calculate the gradient with multiple dimensions of colors in R如何在R中计算具有多维颜色的渐变
【发布时间】:2014-12-21 16:22:51
【问题描述】:

这与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' )

# 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 )

【问题讨论】:

  • 也许this Q&A 有帮助?
  • 附注:当您写二维和三维时,您是指一维和二维(例如 x 维与 x 和 y 维)吗?
  • @Henrik duh 好点 :)
  • 没问题!也许还可以更改情节标题;)
  • @Henrik 问答非常相关,但我确实想要渐变中心的那些白色 X,看起来回答者都没有提供如何做到这一点? ://

标签: r graphics colors ggplot2 palette


【解决方案1】:

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

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)

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

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

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

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

【讨论】:

  • 不管我喜不喜欢,这可能是正确答案:) 谢谢!!
  • @baptiste 你有使用选项 2 或 3 的示例吗?
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 2014-08-21
  • 2020-08-21
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多