2016-12-03 165 views
3

我想知道如何獲取顏色,取決於[0-100]之間的值,其中0是紅色,1是綠色,我希望它們之間的值是插入顏色紅色和綠色之間(橙色, 黃色...,..)。我在Blender 3D中使用python。代碼可以生成彩虹色的線條如下所示here,但我不知道如何可以將其設置爲僅之間的紅,黃,綠,以及如何控制我的色彩範圍(0-100)顏色範圍Python

import colorsys 
(r, g, b) = colorsys.hsv_to_rgb(float(depth)/maxd, 1.0, 1.0) 
R, G, B = int(255 * r), int(255 * g), int(255 * b) 
+0

參見[我的答案]( http://stackoverflow.com/a/10902473/355230)到一個相關的問題。 – martineau

回答

1

易。在colorsys使用的0-1比例尺上,紅色的色調爲0,綠色的色調爲1/3,所以您只需將[0 - 100]範圍內的數字減少到[0 - 1/3 ]間隔分開。

像這樣:

import colorsys 

for i in range(101): 
    rgb = colorsys.hsv_to_rgb(i/300., 1.0, 1.0) 
    print(i, [round(255*x) for x in rgb]) 

輸出

0 [255, 0, 0] 
1 [255, 5, 0] 
2 [255, 10, 0] 
3 [255, 15, 0] 
4 [255, 20, 0] 
5 [255, 25, 0] 
6 [255, 31, 0] 
7 [255, 36, 0] 
8 [255, 41, 0] 
9 [255, 46, 0] 
10 [255, 51, 0] 
11 [255, 56, 0] 
12 [255, 61, 0] 
13 [255, 66, 0] 
14 [255, 71, 0] 
15 [255, 77, 0] 
16 [255, 82, 0] 
17 [255, 87, 0] 
18 [255, 92, 0] 
19 [255, 97, 0] 
20 [255, 102, 0] 
21 [255, 107, 0] 
22 [255, 112, 0] 
23 [255, 117, 0] 
24 [255, 122, 0] 
25 [255, 128, 0] 
26 [255, 133, 0] 
27 [255, 138, 0] 
28 [255, 143, 0] 
29 [255, 148, 0] 
30 [255, 153, 0] 
31 [255, 158, 0] 
32 [255, 163, 0] 
33 [255, 168, 0] 
34 [255, 173, 0] 
35 [255, 178, 0] 
36 [255, 184, 0] 
37 [255, 189, 0] 
38 [255, 194, 0] 
39 [255, 199, 0] 
40 [255, 204, 0] 
41 [255, 209, 0] 
42 [255, 214, 0] 
43 [255, 219, 0] 
44 [255, 224, 0] 
45 [255, 229, 0] 
46 [255, 235, 0] 
47 [255, 240, 0] 
48 [255, 245, 0] 
49 [255, 250, 0] 
50 [255, 255, 0] 
51 [250, 255, 0] 
52 [245, 255, 0] 
53 [240, 255, 0] 
54 [235, 255, 0] 
55 [230, 255, 0] 
56 [224, 255, 0] 
57 [219, 255, 0] 
58 [214, 255, 0] 
59 [209, 255, 0] 
60 [204, 255, 0] 
61 [199, 255, 0] 
62 [194, 255, 0] 
63 [189, 255, 0] 
64 [184, 255, 0] 
65 [178, 255, 0] 
66 [173, 255, 0] 
67 [168, 255, 0] 
68 [163, 255, 0] 
69 [158, 255, 0] 
70 [153, 255, 0] 
71 [148, 255, 0] 
72 [143, 255, 0] 
73 [138, 255, 0] 
74 [133, 255, 0] 
75 [128, 255, 0] 
76 [122, 255, 0] 
77 [117, 255, 0] 
78 [112, 255, 0] 
79 [107, 255, 0] 
80 [102, 255, 0] 
81 [97, 255, 0] 
82 [92, 255, 0] 
83 [87, 255, 0] 
84 [82, 255, 0] 
85 [77, 255, 0] 
86 [71, 255, 0] 
87 [66, 255, 0] 
88 [61, 255, 0] 
89 [56, 255, 0] 
90 [51, 255, 0] 
91 [46, 255, 0] 
92 [41, 255, 0] 
93 [36, 255, 0] 
94 [31, 255, 0] 
95 [26, 255, 0] 
96 [20, 255, 0] 
97 [15, 255, 0] 
98 [10, 255, 0] 
99 [5, 255, 0] 
100 [0, 255, 0] 
+1

@Tak如果您想使用範圍[0-1]而不是[0-100],那麼只需更改分割:'rgb = colorsys.hsv_to_rgb(i/3,1.0,1.0)'。兩個版本都處理'i'的浮點值。您應該記住,使用24位顏色時,從紅色到綠色最多有512步。 –

+0

如果我想切換紅色和綠色,該怎麼辦?所以100會是紅色的,0會是綠色的? – Tak

+1

@Tak只需從100中減去'i':'rgb = colorsys.hsv_to_rgb((100 - i)/ 300.,1.0,1.0)' –

1

的HSV和HSL色彩空間,您可以獨立於其他參數的選擇色調。如果你知道你是哪的hues連續範圍後,您可以輕鬆地映射[0,1]對集:

def get_color(red_to_green): 
    assert 0 <= red_to_green <= 1 
    # in HSV, red is 0 deg and green is 120 deg (out of 360); 
    # divide red_to_green with 3 to map [0, 1] to [0, 1./3.] 
    hue = red_to_green/3.0 
    r, g, b = colorsys.hsv_to_rgb(hue, 1, 1) 
    return map(lambda x: int(255 * x), (r, g, b)) 

結果很容易測試:

>>> get_color(0) 
[255, 0, 0] 
>>> get_color(0.2) 
[255, 102, 0] 
>>> get_color(0.5) 
[255, 255, 0] 
>>> get_color(0.7) 
[153, 255, 0] 
>>> get_color(1) 
[0, 255, 0] 
+0

所以如果我想改變我的範圍從0-1到0-100,我會做hue = red_to_green/300.0而不是? – Tak

+0

@Tak當然。在這種情況下,不要忘記更新'assert'。 :) – user4815162342

+0

非常感謝。我想知道你爲什麼使用assert? – Tak