【发布时间】:2018-06-28 22:35:07
【问题描述】:
更新:根据答案,我正在研究 ColorBrewer 和 Colorspace R 包。应要求,我正在重写我原来的问题。
问题:如何最好地对单个预定义的十六进制颜色进行排序,以便将相似的颜色组合在一起,但从最暖色开始排序,从浅色到最冷色。我希望将其合并到RanglaPunjab R 包的未来版本中
例如Cynthia Brewer's spectral colors(红色、蓝色、绿色等都在一个流中)说明了这一点。
我要的是逻辑,而不是代码。
到目前为止,我尝试按Hue、Hue + Value、Hue * Value 排序,基于这篇文章https://www.alanzucconi.com/2015/09/30/colour-sorting/
这个问题介于 R 和色彩理论之间。
本文末尾的代码按Hue、Hue + Value、Hue * Value 对颜色进行排序(取决于您评论或取消评论的内容)。
请告诉我(或提示)如何获得与 Cynthia Brewer 的光谱颜色相似的结果
Unordered Colors:
根据Hue排序:
根据Hue + Value排序:
根据Hue * Value排序:
供参考,这里是原始的无序调色板
> oldPal
[1] "#22325f" "#88ce64" "#fbd234" "#b8091f" "#682f4e" "#fdea6e" "#4aec6a" "#fb7894" "#f13111" "#2584a0"
[11] "#6fa42c" "#db3717" "#051a8d" "#ef38a7" "#202c3d"
转换为 HSV,然后转置
> tHSVcol
h s v
[1,] 0.62295082 0.6421053 0.3725490
[2,] 0.27672956 0.5145631 0.8078431
[3,] 0.13232831 0.7928287 0.9843137
[4,] 0.97904762 0.9510870 0.7215686
[5,] 0.90935673 0.5480769 0.4078431
[6,] 0.14452214 0.5652174 0.9921569
[7,] 0.36625514 0.6864407 0.9254902
[8,] 0.96437659 0.5219124 0.9843137
[9,] 0.02380952 0.9294606 0.9450980
[10,] 0.53794038 0.7687500 0.6274510
[11,] 0.24027778 0.7317073 0.6431373
[12,] 0.02721088 0.8949772 0.8588235
[13,] 0.64093137 0.9645390 0.5529412
[14,] 0.89890710 0.7656904 0.9372549
[15,] 0.59770115 0.4754098 0.2392157
R 代码
library(RanglaPunjab)
RenderPal <- function(x,name){
if ((missing(x)) || (missing(name))){
stop("Internal error, please troubleshoot")
}
n <- length(x)
old <- graphics::par(mar = c(0.5, 0.5, 0.5, 0.5))
on.exit(graphics::par(old))
graphics::image(1:n, 1, as.matrix(1:n), col = x,
ylab = "", xaxt = "n", yaxt = "n", bty = "n")
graphics::rect(0, 0.9, n + 1, 1.1, col = grDevices::rgb(1, 1, 1, 0.8), border = NA)
graphics::text((n + 1) / 2, 1, labels = name, cex = 2, family = "serif")
}
i <- NULL
oldPal <- NULL
rankorder <- NULL
orderedPal<- NULL
RGBcol <- NULL
HSVcol <- NULL
tHSVcol <- NULL
orderType <- NULL
# Paint the colors
PaintPalette("Teej","Gidha","Jutti3")
# Store the hex values
oldPal <- MergePalette("Teej","Gidha","Jutti3")
# Print hex values
oldPal
# Convert Hex to RGB
RGBcol <- col2rgb(oldPal)
# Print RGB values
RGBcol
# Convert RGB to HSV
HSVcol <- rgb2hsv(RGBcol)
# Print matrix
HSVcol
# Transpose matrix
tHSVcol <- t(HSVcol)
# Print matrix
tHSVcol
# Uncomment following to order by Hue, then Saturation, then Value
rankorder <- order(tHSVcol[,1],tHSVcol[,2],tHSVcol[,3])
orderType <- "Hue Ordering"
# Uncomment following to order by hANDv = Hue + Value
# hANDv <- apply(tHSVcol[,c(1,3)],1,sum)
# rankorder <- order(hANDv)
# orderType <- "Hue + Value Ordering"
# Uncomment following to order by hPRODv = Hue * Value
# hPRODv <- apply(tHSVcol[,c(1,3)],1,prod)
# rankorder <- order(hPRODv)
# orderType <- "Hue * Value Ordering"
rankorder
for (i in 1:length(rankorder)){
orderedPal[i] <- oldPal[rankorder[i]]
}
# Print old, unordered palette
oldPal
# Print new, ordered palette
orderedPal
RenderPal(oldPal, "Unordered Palette")
RenderPal(orderedPal, orderType)
【问题讨论】:
-
您可能对RColorBrewer 包感兴趣
-
@DanHall 会检查这个。顺便说一句,我修改了问题以包含更多颜色,如果有帮助的话。
-
我不清楚你在问什么,尤其是在查看了(非常有趣的)链接文章之后。它提供了很多选项,并以“排序颜色很痛苦。没有一个神奇的功能可以很好地排序它们”,但你问按色调排序是否“可以接受”。但是你没有说你这样做的目的是什么,或者“可接受”对你意味着什么。所以我不知道你在寻找什么样的答案,链接的文章还没有给出。
-
我仍然不清楚你在问什么。您已成功转换为 HSV 并按 Hue 排序。您是否尝试按值排序?它通常与“亮度”相关联。如何按 H+V 或 HV 排序以合并两者?您链接的链接文章中是否有任何排序方案需要帮助实施?和 Aaron 一样,我发现像“*仅基于色调可接受的排序”这样的行令人困惑——谁可以接受?好像你不觉得它可以接受,所以你在这里问什么?删除该部分并专注于您的目标会使这个问题更清晰、更好。
-
@Artie:谢谢,但这并没有真正帮助。例如,“如何做到最好……?”您是否不同意链接的文章并认为有一种最好的方法来做到这一点?如果没有,请参阅 Gregor 询问您喜欢的方案的问题。
标签: r color-theory