[IDL] Creating a Red-White-Blue Color Table in IDL

12/18/2008 ·

In atmospheric science field, you have many opportunities to see the Red-White-Blue color in the weather maps, and the variable anomaly figures. I got an idea from David W. Fanning's website talking about "Constructing a Color Table in IDL", and write a simply IDL code for the Red-White-Blue color table that can be used in your plotting.
I would like to show you my code here, and tell you how to use it in your plotting in next post. If you got another ideas, you are welcome to tell me how to do it more efficiently.


在大氣裡面,有很多機會看在天氣圖或是變差圖上到紅白藍三色的色表。我把 David W. Fanning's website talking about "Constructing a Color Table in IDL" 上的簡單色表程式稍微改寫了一下,就完成了我們常用的這個紅白藍色表。這一篇告訴你怎麼建立這個色表;下一篇,我再說明怎麼把它用在IDL的繪圖上面。

(1) The RGB array settings:
The first part sets the colors from red to white.
The second part is from white to blue.

(1) 紅白藍矩陣的設定:
第一部份,設定紅色漸層至白色。第二部份則由白色漸深至藍色。

pro color_table_RWB

device, decomposed=0
ncolors = 255
steps = 128
scaleFactor = FINDGEN(steps) / (steps - 1)
; Do first half colors (red to white).
; Red vector: 255 -> 0
R = REPLICATE(ncolors, steps)
; Green vector: 255 -> 0
G = 0 + (0 + ncolors) * scaleFactor
; Blue vector: 0 -> 255
B = 0 + (0 + ncolors) * scaleFactor

; Do second 100 colors (whie to blue).
;Red vector: 0 -> 255
R = [R, reverse(0 + (ncolors + 0) * scaleFactor)]
; Green vector: 0 -> 0
G = [G, reverse(0 + (ncolors - 0) * scaleFactor)]
; Blue vector: 255 -> 0
B = [B, REPLICATE(ncolors, steps)]


(2)Output RGB arrays:
列出RGB三矩陣裡的顏色設定值

k = 0
for i = 0, ncolors-1, 16 do begin
print, i, R(i), G(i), B(i), k
k = k + 1
endfor

The total numbers of color are 255+1 (from 0 to 255). If you don't want to plot so many colors, output the RGB arrays to pick up the values of RGB, respectively. You can choose how many colors that you want to output by changing the interval "16" in this for-loop.

色階總數設定為 255+1 (值從0到255),如果你不想用這麼多色階,可從這個loop 印出所有色階的值供你撿用。
或者,就由設定間隔值,可以直接取得等量色階,然後你就可以自行設定所需的色表。


(3) quick look your Red-White-Blue table on screen:

TVLCT, R, G, B

;display the sample
image = FINDGEN(255) # REPLICATE(1, 60)
WINDOW, XSIZE=255, YSIZE=60
TV, BYTE(image)
end



(4) After running this program, the output from (2) should be:
由(2)的輸出。第一行是色階代碼(從0到255),第二行是紅色,第三行是綠色,第四行是藍色。
最後一行則是顯示這個output所產生的顏色數目。在這裡新的色表,顏色是總數16色。


color# Red Green Blue
0 255.000 0.000000 0.000000 0
16 255.000 32.1260 32.1260 1
32 255.000 64.2520 64.2520 2
48 255.000 96.3780 96.3780 3
64 255.000 128.504 128.504 4
80 255.000 160.630 160.630 5
96 255.000 192.756 192.756 6
112 255.000 224.882 224.882 7
128 255.000 255.000 255.000 8
144 222.874 222.874 255.000 9
160 190.748 190.748 255.000 10
176 158.622 158.622 255.000 11
192 126.496 126.496 255.000 12
208 94.3701 94.3701 255.000 13
224 62.2441 62.2441 255.000 14
240 30.1181 30.1181 255.000 15



(5) The output color table is like:
最後執行這個程式後,你可以在螢幕上看到如下的紅白藍色表。





This is my first try for writing an IDL code on the blog. Hopefully, it can help somebody to deal with this color issue.

0 comments:

There was an error in this gadget

Say Something to mph

My Library Things