簡體   English   中英

R 或 python,colors 的漸變范圍從 color1 到 color2

[英]R or python, gradient of colors ranging from color1 to color2

我試圖在不同的顯微鏡色調上對材料上的化學反應進行分類。 我的比例尺兩端都有參考 colors,它們在所有擴展中都是同質的,所以基本上我只有 RGB 代碼作為啟動器。 顏色 1 是白色和 2 紅色,所以我必須在白色和紅色調之間移動,僅此而已。

我已經讀過 colorrampPalette 可能在 R 中很有用,但它適用於預制的 colors,我只是這方面的新手,我嘗試研究,但沒有什么適合我想要的,你有什么推薦我? 謝謝

ps 也可以與 Python 一起使用

不知道有沒有這樣的function,不過寫一個還是蠻容易的:

def gradients(color1, color2, steps):
    # colors are tuples of ints representing RGB values
    r1, g1, b1 = color1  # break color into separate values
    r2, g2, b2 = color2
    step_r = (r2 - r1) / steps  # given n-steps, define step size for each color change
    step_g = (g2 - g1) / steps
    step_b = (b2 - b1) / steps
    return [(int(r1 + step_r * i), # produce list of color applying respective step size over each iteration
             int(g1 + step_g * i), 
             int(b1 + step_b * i)) for i in range(steps)]

結果:

>>> gradients((0,0,0), (255, 255, 255), 3)
>>> [(0, 0, 0), (85, 85, 85), (170, 170, 170)]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM