从颜色表数组中确定颜色表索引
17955 给本文评分:
暂无评分
从颜色表数组中确定颜色表索引
匿名 2013年8月1日,星期四
今天我想展示一下最近遇到的一个问题,它并非特别重要,但我认为很有趣。
在 IDL 8(又名 New)Graphics 中,RGB_TABLE 属性可以接受一个颜色表索引作为输入,该索引是一个 0-74 之间的整数。在内部,颜色信息会被转换成一个颜色表数组,即一个 3 x 256 的字节数组。例如,这里我使用颜色表 70 将距离图(使用 DIST 函数生成)显示为图像:
IDL> g = image(dist(400), rgb_table=70)
当我检索颜色表时,它已被转换成一个颜色表数组:
IDL> help, g.rgb_table
<Expression> BYTE = Array[3, 256]
如果我想取回颜色表索引 70,该怎么办呢?以下是一个试图解决此问题的程序:
; docformat = 'rst'
;+
; Attempts to determine which built-in color table matches an input [256,3]
; or [3,256] array of colors.
;
; :params:
; rgb_table: in, required, type=byte
; A [256,3] or [3,256] byte array of color table values.
;
; :returns:
; The color table index if matched, else the input is passed through unchanged.
;
; :requires:
; IDL 8.2.1
;
; :author:
; Mark Piper, VIS, 2013
;-
function determine_colortable, rgb_table
compile_opt idl2, hidden
on_error, 2
do_transpose = (size(rgb_table, /dimensions))[0] eq 3
!null = colortable(get_names=all_ct_names)
for ct_index=0, n_elements(all_ct_names)-1 do begin
ct_array = colortable(ct_index, transpose=do_transpose)
if array_equal(rgb_table, ct_array) then return, ct_index
endfor
return, rgb_table ; pass through
end
关键在于使用 IDL 8.2.1 中引入的 COLORTABLE 函数。使用 DETERMINE_COLORTABLE(我想不出更好的名字了)来确定上面图形“g”中使用的颜色表:
IDL> print, determine_colortable(g.rgb_table)
70
这个程序的灵感来自于与 IDL 工程团队的 Eddie Haskell 的讨论。