代码之家  ›  专栏  ›  技术社区  ›  captcoma

R将颜色转换为柔和色

  •  1
  • captcoma  · 技术社区  · 8 年前

    我有一个手工设计的调色板 pal 我用它来做一个情节 ggplot2

     pal <- c("red","red1","red2","red3","grey","darkgreen","skyblue","blue","magenta","magenta4","yellow","orange","pink","pink","black")
    

    我想将这些颜色转换成柔和的颜色(即hsv颜色空间中的高值和低饱和度)

    有什么简单的方法可以自动完成吗?

    我发现了 pastel.colors RPMG 但是,此函数不转换给定的颜色。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Roman    8 年前

    因为我不是一个色彩专家,你可以试着从下面开始。这个想法是基于我在某处发现的一个观点,即粉彩1)在hsv颜色空间中具有高值和低至中等饱和度,或2)需要一些亮度值。所以我把颜色的名字转换成颜色 hcl 通过 col2rgb 和; rgb2hsv .

    a <-c("red","red1","red2","red3","grey","darkgreen","skyblue","blue","magenta","magenta4","yellow","orange","pink","pink","black")
    par(mfrow=c(3,1))
    barplot(seq_along(a), col=a, main="original")
    # transform to rgb
    a1 <- col2rgb(a)
    # transform to HSV space
    a2 <- rgb2hsv(a1)
    # you can try different scaling values e.g. between 0.3 - 0.6
    n <- 0.4
    barplot(seq_along(a), col=hsv(a2[1,], a2[2,]*n, a2[3,]), main="Pastel_hsv")
    # calculate hue for HCl
    hue <- a2["h",]*360
    # create color with suitable chroma and luminance to get pastel color
    a3 <- hcl(hue, 35, 85)
    barplot(seq_along(a), col=a3, main="Pastel_hcl")
    

    enter image description here